简体   繁体   English

autofac-在非通用抽象类上注册通用

[英]autofac - register generic on non generic abstract class

autofac - register generic on non generic abstract class autofac-在非通用抽象类上注册通用

I have the following class structures.. 我有以下课程结构..

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)
        {
        }
}

I have tried the following but getting "The type 'Repository 2' does not implement the interface 'IRepository 1'." 我尝试了以下操作,但得到“类型'Repository 2' does not implement the interface 'IRepository 1'”。

builder.RegisterAssemblyTypes(typeof(DataContextBase).Assembly).AsClosedTypesOf(typeof(DataContext<>));

how do i do the IoC registration for DataContext ? 如何为DataContext进行IoC注册?

AsClosedTypesOf method won't help you register DataContext<> , by the way it will help you register IRepository<T> . AsClosedTypesOf方法无法帮助您注册DataContext<> ,它可以帮助您注册IRepository<T>

Instead of registering all your repository manually 无需手动注册所有存储库

    builder.RegisterType<CustomerRepository>().As<IRepository<Customer>>();
    builder.RegisterType<InvoiceRepository>().As<IRepository<Invoice>>();
    ...

You will be able to register all of them once : 您将可以全部注册一次:

    builder.RegisterAssemblyTypes(typeof(IRepository<>).Assembly).AsClosedTypesOf(typeof(IRepository<>)); 

To register DataContext<> in a generic manner you can use a RegistrationSource : 要以通用方式注册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();

        }
    }

Then 然后

    builder.RegisterSource(new DataContextRegistrationSource());

You can now resolve IRepository<Customer> 您现在可以解析IRepository<Customer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM