I am looking for some advice on a good design for loading lookup tables such as state codes using Prism Unity? My view libraries are domain focused and I have modules that pass in an IUnityContainer. In the initialize section I register with the container the RegisterType such as IStateCode, StateCode.
Should I registerType, then load the state object and then use RegisterInstance? Should this be done in each domain (dll) or should I centrally load the tables once, and where? I thought about loading the lookup tables in the Load of the mainwindow, but I would have to reference all of the lookup classes in that module. If I use a central location to load the lookup tables, I don't have to worry about lookup tables being null and it's located in one area. How say yee?
The approach that I have taken with this sort of thing is to create a central project in the solution; that could be called Core.UI (or whatever you like). There, I create a class that I register with the container as a singleton, which loads the data it needs at application start up (via the Initialize call; see code). This is commonly referred to as a service.
You could make when the data is loaded as flexible as you like. At application load time, or the first time the property is accessed. I do mine up front, since the data is not huge, and it doesn't change that often. You may even want to consider some sort of caching mechanism here, too.
I have done something similar for products, too. Below is for US state codes.
public class StateListService : IStateListService // The interface to pass around
{
IServiceFactory _service_factory;
const string country = "United States";
public StateListService(IServiceFactory service_factory)
{
_service_factory = service_factory;
Initialize();
}
private void Initialize()
{
// I am using WCF services for data
// Get my WCF client from service factory
var address_service = _service_factory.CreateClient<IAddressService>();
using (address_service)
{
try
{
// Fetch the data I need
var prod_list = address_service.GetStateListByCountry(country);
StateList = prod_list;
}
catch
{
StateList = new List<AddressPostal>();
}
}
}
// Access the data from this property when needed
public List<AddressPostal> StateList { get; private set; }
}
EDIT:
To register the above as a singleton in Prism 6, add this line of code to the method you use to initialize your container. Usually in the bootstapper.
RegisterTypeIfMissing(typeof(IStateListService), typeof(StateListService), true);