I am developing a WCF web service that returns information from one of several databases based on string providerCode .
At the highest level, the service calls the StaticBroker class, which checks providerCode and returns the corresponding subclass of DataManager , say MyDataManager . The service then calls MyDataManager.getVehicleFetcherForStop() , which returns an instance of the VehicleInfoFetcher class, which is used to retrieve information.
I am completely new to all of this, and I think that I could fix it incorrectly. Here is the code for how I am doing this right now (simplified):
Service.svc.cs
// Public-facing web service method public string getRealtimeInfo(String stopID, string providerCode = "UK") { DataManager dm = StaticBroker.Instance.dataManager(stopID); return dm.getUpcomingVehicleInfoNow(primaryCode); }
StaticBroker
public sealed class StaticBroker { UKDataManager ukDataManager = null;
UKDataManager
public class UKDataManager : DataManager { public const string DEFAULT_PROVIDER_CODE = "UK"; public string getUpcomingVehicleInfoNow(string stopID) { VehicleInfoFetcher infoFetcher; if ( shouldCheckDB(stopID)) VehicleInfoFetcher infoFetcher = new DatabaseVehicleInfoFetcher("UK"); else fetcher = new UKLiveVehicleInfoFetcher(); return fetcher.getVehicleInfo(primaryCode).Result;
As you can see, I have a Singleton from StaticBroker that stores only one instance of each type of DataManager . Finally, an actual instance of the class is created inside the DataManager that does the real work, SomeVehicleFetcher .
Is this a smart way to do this? Or can these singletones and shared instances cause problems while using the web service? I was worried that creating a ton of new instances could lead to memory problems. As you can see, I really don't understand how the application life cycle / memory cycle works in a web service.
source share