我使用EF(实体框架代码优先)和存储库Patern与Unity(依赖注入)和工作单元。我有两个上下文,但我想根据请求使用其中一个。像这样:
public bool Save(User user, RequestTyoe request)
{
//这里应用一些模式,根据请求决定上下文 使用
AbstracRepository.Add(user);
AbstracRepository.UnitOfWork.Commit();
}
有什么建议吗?
PD:真的我不知道使用什么模式...由于
答案 0 :(得分:0)
如果您是通用存储库和不同的上下文,则可以指定要传递的上下文类型。
public abstract class GenericRepository<C, T> :
IGenericRepository<T> where T : class where C : DbContext, new()
答案 1 :(得分:0)
You can use CastleWindsor to easily register multiple Services: http://docs.castleproject.org/Default.aspx?Page=MainPage&NS=Windsor&AspxAutoDetectCookieSupport=1
NuGet: http://www.nuget.org/packages/castle.windsor
>>> inst.__dict__
{'i': 10}
Then in your controller you take in the services in your constructor and you can use them however you like.
public class UserServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<IUserService1>()
.ImplementedBy<UserService1>()
.LifestyleTransient());
container.Register(Component.For<IUserService2>()
.ImplementedBy<UserService2>()
.LifestyleTransient());
}
}