简体   繁体   English

Unity容器注册并通过继承解析通用接口

[英]Unity Container Register and resolve for generic Interface with inheritance

I have the following code : 我有以下代码:

 public interface IGenericDao<TEntity> where TEntity : IEntity {    }

 public interface IEntity { }

 public abstract class AbstractEntity : IEntity {}

 public interface IMasterEntity : IEntity {}

 public interface IDynamicEntity : IEntity {}

 public class Client : AbstractEntity , IMasterEntity {} 

 public class MasterEntityHandler<TEntity> : IGenericDao<TEntity>, IDisposable where TEntity : IMasterEntity {}

 public class DynamicEntityHandler<TEntity> : IGenericDao<TEntity>, IDisposable where TEntity : IDynamicEntity {}

In the unity container, I made the registration as : 在统一容器中,我注册为:

container.RegisterType<IGenericDao<IMasterEntity>, MasterEntityHandler<IMasterEntity>>(new ContainerControlledLifetimeManager());
container.RegisterType<IGenericDao<IDynamicEntity>, MasterEntityHandler<IDynamicEntity>>(new ContainerControlledLifetimeManager());

When trying to resolve with Client class with 尝试使用Client类解析时

container.Resolve<IGenericDao<Client>>();

I am getting an error that 我收到一个错误

---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException:调用的目标引发了异常。 ---> Unity.ResolutionFailedException: The current type, Interface.IGenericDao`1[Client], is an interface and cannot be constructed. ---> Unity.ResolutionFailedException:当前类型Interface.IGenericDao`1 [Client]是一个接口,无法构造。 Are you missing a type mapping? 您是否缺少类型映射?

Tried this registration also, but still same error : 也尝试过此注册,但仍然存在相同的错误:

container.RegisterType(typeof(IGenericDao<IMasterEntity>),typeof(MasterEntityHandler<>)
                , new ContainerControlledLifetimeManager());
container.RegisterType(typeof(IGenericDao<IDynamicEntity>),typeof(DynamicEntityHandler<>)
                    , new ContainerControlledLifetimeManager());

and also : 并且 :

container.RegisterType(typeof(IGenericDao<>),typeof(MasterEntityHandler<IMasterEntity>)
                , new ContainerControlledLifetimeManager());
container.RegisterType(typeof(IGenericDao<>),typeof(DynamicEntityHandler<IDynamicEntity>)
                    , new ContainerControlledLifetimeManager());

Consider narrowing the scope of the interfaces 考虑缩小接口范围

public interface IMasterDao<TEntity> : IGenericDao<TEntity>, IDisposable where TEntity : IMasterEntity { }

public interface IDynamicDao<TEntity>: IGenericDao<TEntity>, IDisposable where TEntity : IDynamicEntity {}

public class MasterEntityHandler<TEntity> : IMasterDao<TEntity>, IDisposable where TEntity : IMasterEntity {}

public class DynamicEntityHandler<TEntity> : IDynamicDao<TEntity>, IDisposable where TEntity : IDynamicEntity {}

Remove the interface from the registration and leave it as an open generic registration. 从注册中删除接口,并将其保留为开放的通用注册。

container.RegisterType(typeof(IMasterDao<>), typeof(MasterEntityHandler<>), new ContainerControlledLifetimeManager());
container.RegisterType(typeof(IDynamicDao<>), typeof(DynamicEntityHandler<>), new ContainerControlledLifetimeManager());

The type constraint on the implementation will handle what classes can be used. 实现上的类型约束将处理可以使用哪些类。

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

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