0
Reply

Can abstract base class be used to share a cached dictionary

Suraj Rai

Suraj Rai

Jan 21 2016 6:03 AM
346


Hi,

I have a module where I have to read and write data to a some kind of storage. I have to cache the data once in a dictionary which I will be using it at the time of read.

I want to have two different class for read a write.
Sample:

internal abstract class AbstractBaseReadWrite
    {
        // This dictionary will be used by the inherited ReadData and WriteData class
        // This will be used as in memory cache
        public Dictionary<string, string> cachedData = new Dictionary<string, string>();
    }


internal class WriteData : AbstractBaseReadWrite
    {
        public void Write()
        {
            // write the data to the storage and
            // cache it to a dictionary available in AbstractBaseReadWrite
        }
    }

internal class ReadData : AbstractBaseReadWrite, IReadData
    {
        public Dictionary<string, string> Read()
        {
            // check if the data available in the cached dictionary of AbstractBaseReadWrite
            // if available return from their, if not than only read from the storage
        }
    }

Is it a good way to share the in memory caching. If it would have been only one class with two methods Read and Write than would
have been perfact to keep the dictionary in the same class but in this scenario I am not sure.

Can you please guide me if this is fine or any better approch.

Thanks.
Best regards,
Raj
 


Next Recommended Forum