I was asked to build different WCF services where each do other work against sql.
We have 5 db’s. All db’s+connection string are in 1 xml file. ( file-system file)
The services are hosted under WAS iis 7.5.
since each service should read from db , each service references a DAL dll file.
So here are our components :

I would like to read the xml data to CACHE ( at the first request) and from now on – read from cache. (reading the file each reqeust is out of the question).
- idea #1 = the dll , in his ctor , at first request will read the xml file and load it to its cache.
so the dal will look like this :

so now each service can access the DLL’s cache object via property. ( one advantage is when dealing with cache dependency on a single file – so when it is changing , we should reload only to one location).
- idea #2 = when service is up , load the xml into its
cache.
so now , each service will look like this :
Service #1 :

Service #2 :

..
the downside is many cache dependencies on the same file
Question :
By the best practice experience and by design pattern POV : which is the preferred way ?
p.s. the xml file change frequency 1/(1 month)
First of all, when it comes to file system, on Windows Server OS, there’s a built in cache layer above the disk. So you probably won’t feel much difference regarding disk reads. Of course, parsing the same input again and again is not a good practice, so the parsed (tokenized) xml should be cached.
The design needs more clarifications:
Is there only a single instance of a DAL class, shared among the 5
services? Or maybe the property described in idea 1 is static?
In idea 2: when the file changes and, say, connection string 4 is
changed (and everything else remains the same) – only service 4
should be reloaded?
If a specific service is reloaded – does it cause some kind of
inconsistency with other (non fresh) services?
Update:
I’m still not sure I fully understand the scenario, but here’s what I’d do as far as I understand:
The DAL should expose an interface for all data related operations.
Let’s say it’s
IDataGatewayNow, each service has should have a reference to an instance that implements
IDataGateway. The service should not be aware of the caching mechanism at all. It just consumes data from the interface.So all of the caching is done outside the service, in terms of classes and code organization.
Now, the caching layer, in turn, implements
IDataGateway, and also consumes a non cached instance ofIDataGateway. That’s called Decorator pattern. The non cached instance is to be injected in the constructor.Now, I suggest each service has its own instance of a cached
IDataGateway. It’s simpler than a singleton (to me, at least). And since data is not shared between services, then we’re cool. If, however, data is shared between the services, than a single instance should be used.Back to those 5 instances, and to the xml file.
We want to monitor this file once it changes, right? We could easily write our own file monitor, or use the one that comes with the framework, or we could see the source code of the CacheDependency class.
The simplest way to do it is to have 5 monitors watching the same file. That’s not much of a performance penalty, since timers are quite “cheap”.
If, however, you’d like to reduce the resources being used by your system, then you could use a single monitor, having it raise its event of
FileChangedor something like that. Each of the 5 cached implementations (those 5 instances) ofIDataGatewayshould have this monitor injected in its constructor, and wire up its own event listener to theFileChangedevent.Once this event is triggered, all of the 5 cached instances of
IDataGatewaywould invalidate their inner cache, thus they should clear their in-memory entries.On the next call, the cached implementation of
IDataGatewaywould try to take the non existing data from its in-memory cache, but obviously nothing would be there, so it should go on executing the same method in the non-cached implementation ofIDataGateway, and populate its cache.That’s my design, HTH…