c# - StructureMap - different concrete instance based on inheritance of generic type in parent -
i'm not sure if possible. might have make changes want work. maybe need 2 repository classes. i'm open suggestions make work can connect 2 different entity framework data contexts.
i have setup user profiles stored in shared database sub-sites, , other models in site database specific site. there going 2+ sites , use shared entity context shared models.
first have profile model in shared models library
public class profile : coreobject, ishared { //...properties }
for comparison, here object in subsite models library
public class game : coreobject { //...properties }
then have repository interface
public interface irepository<t> : idisposable t : coreobject
and concrete
public class repository<t> : irepository<t> t : coreobject { //system.data.entity.dbcontext private dbcontext context { get; set; } public repository(idbcontextfactory dbcontextfactory) { context = dbcontextfactory.getdbcontext(); } //...other methods accessing dbcontext }
and have factory interface
public interface idbcontextfactory : idisposable
and factory concrete, have 2 classes can define underlying context , objects available. onlything different class library datacontext
located in.
public class dbcontextfactory : idbcontextfactory { private readonly dbcontext _context; public dbcontextfactory(string connectionstring) { // datacontext shared.data.context.datacontext in 1 file // , subsite.data.context.datacontext in another. both // same except concrete initializer _context = new datacontext(connectionstring); } public dbcontext getdbcontext() { return _context; } }
here's datacontext looks like
public class datacontext : dbcontext, idatacontext
dbcontext
system.data.entity. idatacontext interface both datacontexts extend , looks this.
public interface idatacontext { database database { get; } }
what want do, if type t in repository inherits ishared, idbcontextfactory use shared.data.context.dbcontextfactory, otherwise use subsite.data.context.dbcontextfactory
Comments
Post a Comment