Entity framework 具有多个组件和不同实体连接的Autofac注册
我们有两个程序集,它们包含自己的实体框架EDMX和Repository对象。这些是在ASP.NET Web应用程序中使用Autofac注册的 这些程序集的体系结构非常相似(但不同于EDMX),我们发现最后注册的EntityConnection是两个程序集中使用的EntityConnection。我们需要将EntityConnection的使用限制为仅由程序集或命名空间的类型使用Entity framework 具有多个组件和不同实体连接的Autofac注册,entity-framework,ioc-container,autofac,Entity Framework,Ioc Container,Autofac,我们有两个程序集,它们包含自己的实体框架EDMX和Repository对象。这些是在ASP.NET Web应用程序中使用Autofac注册的 这些程序集的体系结构非常相似(但不同于EDMX),我们发现最后注册的EntityConnection是两个程序集中使用的EntityConnection。我们需要将EntityConnection的使用限制为仅由程序集或命名空间的类型使用 var assembly = typeof(Activity).Assembly; builder.RegisterA
var assembly = typeof(Activity).Assembly;
builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();
builder.Register(reg => new EntityConnection(ConnectionString));
var assembly = typeof(User).Assembly;
builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();
builder.Register(reg => new EntityConnection(ConnectionString));
builder.Register(reg => new EntityConnection(ConnectionString)).ForNamespace("x");
有没有办法注册EntityConnection并限制EntityConnection的深度?是否将每个EntityConnection限制为它所属的程序集
下面是一个伪代码示例,说明如何注册EntityConnection以仅在程序集或命名空间中使用
var assembly = typeof(Activity).Assembly;
builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();
builder.Register(reg => new EntityConnection(ConnectionString));
var assembly = typeof(User).Assembly;
builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces();
builder.Register(reg => new EntityConnection(ConnectionString));
builder.Register(reg => new EntityConnection(ConnectionString)).ForNamespace("x");
尝试在更高的抽象层次上解决问题。由于您有两个独立的域(一个包含
活动
实体,另一个包含用户
实体),因此在应用程序设计中明确地包含这一点是很方便的。例如,为每个域定义某种工厂:
public interface IActivityDomainContextFactory
{
ObjectContext CreateNew();
}
public interface IPeopleDomainContextFactory
{
ObjectContext CreateNew();
}
现在,您可以轻松地为每个接口创建一个实现,在AutofacContainerBuilder
中注册它们,让您的服务依赖于其中一个接口,而不是依赖于EntityConnection
在这种情况下,您当然仍然依赖于实体框架本身(请参阅如何将其抽象出来),但这使您的配置更容易、更不脆弱、性能更好,并且应用程序代码更易于维护
我希望这会有所帮助。尝试在更高的抽象层次上解决问题。由于您有两个独立的域(一个包含
活动
实体,另一个包含用户
实体),因此在应用程序设计中明确地包含这一点是很方便的。例如,为每个域定义某种工厂:
public interface IActivityDomainContextFactory
{
ObjectContext CreateNew();
}
public interface IPeopleDomainContextFactory
{
ObjectContext CreateNew();
}
现在,您可以轻松地为每个接口创建一个实现,在AutofacContainerBuilder
中注册它们,让您的服务依赖于其中一个接口,而不是依赖于EntityConnection
在这种情况下,您当然仍然依赖于实体框架本身(请参阅如何将其抽象出来),但这使您的配置更容易、更不脆弱、性能更好,并且应用程序代码更易于维护
我希望这能有所帮助。您可能需要命名/键入您的注册。看
我认为这解决了你一半的问题,如何注册类型。另一半在决议中。由于您通过程序集扫描进行自动注册,这可能需要更多的技巧。您可能需要命名/键入您的注册。看
我认为这解决了你一半的问题,如何注册类型。另一半在决议中。由于您通过程序集扫描进行自动注册,这可能需要更多的技巧。关于改进这一点,有很多好的建议,所以只需将我的解决方案记录为在Autofac中如何执行此操作的一般草图 诀窍是为连接使用命名服务,然后为每个程序集中的类型自定义参数解析,以便
EntityConnection
参数获得命名实例:
builder.Register(reg => new EntityConnection(ConnectionString))
.Named<EntityConnection>("activities");
builder.RegisterAssemblyTypes(typeof(Activity).Assembly)
.AsImplementedInterfaces()
.WithParameter(
(pi, c) => pi.ParameterType == typeof(EntityConnection),
(pi, c) => c.ResolveNamed<EntityConnection>("activities"));
builder.Register(reg => new EntityConnection(ConnectionString))
.Named<EntityConnection>("users");
builder.RegisterAssemblyTypes(typeof(User).Assembly)
.AsImplementedInterfaces()
.WithParameter(
(pi, c) => pi.ParameterType == typeof(EntityConnection),
(pi, c) => c.ResolveNamed<EntityConnection>("users"));
builder.Register(reg=>newentityconnection(ConnectionString))
.命名为(“活动”);
builder.RegisterAssemblyTypes(typeof(Activity).Assembly)
.AsImplementedInterfaces()
.带参数(
(pi,c)=>pi.ParameterType==typeof(EntityConnection),
(pi,c)=>c.c(“活动”);
builder.Register(reg=>newentityconnection(ConnectionString))
.指定(“用户”);
builder.RegisterAssemblyTypes(typeof(User.Assembly)
.AsImplementedInterfaces()
.带参数(
(pi,c)=>pi.ParameterType==typeof(EntityConnection),
(pi,c)=>c.c(“用户”);
关于改进这一点,有很多很好的建议,所以只需将我的解决方案记录为在Autofac中如何实现这一点的概述
诀窍是为连接使用命名服务,然后为每个程序集中的类型自定义参数解析,以便EntityConnection
参数获得命名实例:
builder.Register(reg => new EntityConnection(ConnectionString))
.Named<EntityConnection>("activities");
builder.RegisterAssemblyTypes(typeof(Activity).Assembly)
.AsImplementedInterfaces()
.WithParameter(
(pi, c) => pi.ParameterType == typeof(EntityConnection),
(pi, c) => c.ResolveNamed<EntityConnection>("activities"));
builder.Register(reg => new EntityConnection(ConnectionString))
.Named<EntityConnection>("users");
builder.RegisterAssemblyTypes(typeof(User).Assembly)
.AsImplementedInterfaces()
.WithParameter(
(pi, c) => pi.ParameterType == typeof(EntityConnection),
(pi, c) => c.ResolveNamed<EntityConnection>("users"));
builder.Register(reg=>newentityconnection(ConnectionString))
.命名为(“活动”);
builder.RegisterAssemblyTypes(typeof(Activity).Assembly)
.AsImplementedInterfaces()
.带参数(
(pi,c)=>pi.ParameterType==typeof(EntityConnection),
(pi,c)=>c.c(“活动”);
builder.Register(reg=>newentityconnection(ConnectionString))
.指定(“用户”);
builder.RegisterAssemblyTypes(typeof(User.Assembly)
.AsImplementedInterfaces()
.带参数(
(pi,c)=>pi.ParameterType==typeof(EntityConnection),
(pi,c)=>c.c(“用户”);
谢谢你的建议,遗憾的是这对我的问题不起作用。但是,对于我尝试解决的另一个问题,您给出了一个非常棒的答案。在Quartz.NET IJobFactory中命名为Resolved。感谢您的建议,很遗憾,它无法解决我的问题。但是,对于我在Quartz.NET IJobFactory中尝试.resolve时遇到的另一个问题,您给出了一个非常棒的答案。