jeudi 26 mars 2015

Partitioned IOC Containers

I'm currently porting a large WIN app so I can extend it out to MVC. One of the principals is that it support multiple DALs which is dynamically bound by fluent config. for example



interface IDataStore
{
}

class SqlRepository :IDataStore
{
//do sql stuff
}

class VistaDBRepository : IDataStore
{
//do vista db stuff
}


and finally



class WCFClientRepository :IDataStore
{
//do WFC stuff
}


to consume this implementation in the business logic it goes like this



public IDataStore GetCurrentStore()
{
get
{
switch(EnterpriseConfiguration.Instance.ActiveProvider)
{
case "MSSQL":
return new SqlRepository();
case "VistaDB":
return new VistaDBRespository();
case "WFC":
return new WCFReposity();
}
}
}

void GetDate()
{
using(IDataStore db = GetCurrentStore())
{
//do something with db
}
}


with that there is a connection listener that will monitor between the primary db provider (SQL) and the mobile provider (WCF) if the connection drops or is not available with the primary provider it will elect the secondary provider (WFC) as primary. this is to support mobile users and works great, user who are inside the LAN go directly to SQL and will auto switch to WFC when they leave.


finally VistaDB is used as a persistance store (plugin info, form metadata, locality, countries etc....)


I want to replace this pattern with a IOC container, my idea would be to build up a container for each defined IDataStore that can bind to a name, in turn get consumes as such



SQLReposity db = Container.Resolve<IDataStore>().ContainerName("MSSQL");
VistaDBReposity db = Container.Resolve<IDataStore>().ContainerName("VistaDB");
WFCReposity db = Container.Resolve<IDataStore>().ContainerName("WCF");


Any ideas on would to build out the container using Unity or StructureMap?


Aucun commentaire:

Enregistrer un commentaire