[英]Working with two context EF
Im using EF (Entity Framework Code First) and Repository Patern with Unity (Dependency Injection) and Unit Of Work. 我使用EF(首先是实体框架代码)和带有Unity(依赖注入)和工作单元的存储库模式。 I have two context but I want to work with one of them depending of the request.
我有两个上下文,但是我想根据请求使用其中之一。 Something like this:
像这样:
public bool Save(User user, RequestTyoe request)
{
//Here apply some pattern that decide depending of request the context to use
//这里应用一些模式,该模式根据请求上下文来决定
AbstracRepository.Add(user);
AbstracRepository.UnitOfWork.Commit();
}
Any suggestion ?? 有什么建议吗??
PD: Really I dont know what pattern use ... PD:真的我不知道用什么模式...
Thanks 谢谢
If you generic repository and different context, then you can specify the type of context you want to pass. 如果通用存储库和其他上下文,则可以指定要传递的上下文的类型。
public abstract class GenericRepository<C, T> :
IGenericRepository<T> where T : class where C : DbContext, new()
You can use CastleWindsor to easily register multiple Services: http://docs.castleproject.org/Default.aspx?Page=MainPage&NS=Windsor&AspxAutoDetectCookieSupport=1 您可以使用CastleWindsor轻松注册多个服务: http ://docs.castleproject.org/Default.aspx?Page=MainPage&NS=Windsor&AspxAutoDetectCookieSupport=1
NuGet: http://www.nuget.org/packages/castle.windsor NuGet: http ://www.nuget.org/packages/castle.windsor
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());
}
}
Then in your controller you take in the services in your constructor and you can use them however you like. 然后,在控制器中,您可以在构造函数中使用服务,并且可以随意使用它们。
public class MyController : Controller
{
private IUserService1 _userService1;
private IUserService2 _userService2;
public MyController(IUserService1 userService1, IUserService2 userService2)
{
_userService1 = userService1;
_userService2 = userService2;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.