autofac - 在非泛型抽象上注册泛型 class
autofac - register generic on non generic abstract class
autofac - 在非泛型抽象上注册泛型 class
我有以下 class 结构..
public class DataContext<TEntityModel> : DataContextBase
where TEntityModel : IEntity
{
public DataContext(string databaseName, string serverName = "localhost") :
base(databaseName, serverName)
{
}
}
public abstract class Repository<TEntityModel> : IRepository<TEntityModel>
where TEntityModel : Entity
{
protected MongoRepositoryBase(DataContextBase ctx)
{
this.DBContext = ctx;
}
}
public class CustomerRepository : Repository<Customer>
{
public CustomerRepository(DataContext<Customer> ctx)
: base(ctx)
{
}
}
我已经尝试了以下但得到 "The type 'Repository2' does not implement the interface 'IRepository
1'."
builder.RegisterAssemblyTypes(typeof(DataContextBase).Assembly).AsClosedTypesOf(typeof(DataContext<>));
如何为 DataContext 注册 IoC?
AsClosedTypesOf
方法不会帮你注册DataContext<>
,顺便帮你注册IRepository<T>
.
而不是手动注册所有存储库
builder.RegisterType<CustomerRepository>().As<IRepository<Customer>>();
builder.RegisterType<InvoiceRepository>().As<IRepository<Invoice>>();
...
您将能够一次注册所有这些:
builder.RegisterAssemblyTypes(typeof(IRepository<>).Assembly).AsClosedTypesOf(typeof(IRepository<>));
要以通用方式注册 DataContext<>
,您可以使用 RegistrationSource
:
public class DataContextRegistrationSource : IRegistrationSource
{
public Boolean IsAdapterForIndividualComponents
{
get { return true; }
}
public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
{
if (service == null)
{
throw new ArgumentNullException("service");
}
if (registrationAccessor == null)
{
throw new ArgumentNullException("registrationAccessor");
}
IServiceWithType ts = service as IServiceWithType;
if (ts == null || !(ts.ServiceType.IsGenericType && ts.ServiceType.GetGenericTypeDefinition() == typeof(DataContext<>)))
{
yield break;
}
yield return RegistrationBuilder.ForType(ts.ServiceType)
.AsSelf()
.WithParameter(new NamedParameter("databaseName", "test"))
.WithParameter(new NamedParameter("serverName", "test2"))
.CreateRegistration();
}
}
然后
builder.RegisterSource(new DataContextRegistrationSource());
您现在可以解析 IRepository<Customer>
autofac - 在非泛型抽象上注册泛型 class
我有以下 class 结构..
public class DataContext<TEntityModel> : DataContextBase
where TEntityModel : IEntity
{
public DataContext(string databaseName, string serverName = "localhost") :
base(databaseName, serverName)
{
}
}
public abstract class Repository<TEntityModel> : IRepository<TEntityModel>
where TEntityModel : Entity
{
protected MongoRepositoryBase(DataContextBase ctx)
{
this.DBContext = ctx;
}
}
public class CustomerRepository : Repository<Customer>
{
public CustomerRepository(DataContext<Customer> ctx)
: base(ctx)
{
}
}
我已经尝试了以下但得到 "The type 'Repository2' does not implement the interface 'IRepository
1'."
builder.RegisterAssemblyTypes(typeof(DataContextBase).Assembly).AsClosedTypesOf(typeof(DataContext<>));
如何为 DataContext 注册 IoC?
AsClosedTypesOf
方法不会帮你注册DataContext<>
,顺便帮你注册IRepository<T>
.
而不是手动注册所有存储库
builder.RegisterType<CustomerRepository>().As<IRepository<Customer>>();
builder.RegisterType<InvoiceRepository>().As<IRepository<Invoice>>();
...
您将能够一次注册所有这些:
builder.RegisterAssemblyTypes(typeof(IRepository<>).Assembly).AsClosedTypesOf(typeof(IRepository<>));
要以通用方式注册 DataContext<>
,您可以使用 RegistrationSource
:
public class DataContextRegistrationSource : IRegistrationSource
{
public Boolean IsAdapterForIndividualComponents
{
get { return true; }
}
public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
{
if (service == null)
{
throw new ArgumentNullException("service");
}
if (registrationAccessor == null)
{
throw new ArgumentNullException("registrationAccessor");
}
IServiceWithType ts = service as IServiceWithType;
if (ts == null || !(ts.ServiceType.IsGenericType && ts.ServiceType.GetGenericTypeDefinition() == typeof(DataContext<>)))
{
yield break;
}
yield return RegistrationBuilder.ForType(ts.ServiceType)
.AsSelf()
.WithParameter(new NamedParameter("databaseName", "test"))
.WithParameter(new NamedParameter("serverName", "test2"))
.CreateRegistration();
}
}
然后
builder.RegisterSource(new DataContextRegistrationSource());
您现在可以解析 IRepository<Customer>