Контейнер Unity Зарегистрируйте и определите универсальный интерфейс с наследованием - PullRequest
1 голос
/ 03 июля 2019

У меня есть следующий код:

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

В контейнере Unity я сделал регистрацию как:

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

При попытке решить с помощью класса Client с помощью

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

Я получаю сообщение об ошибке

---> System.Reflection.TargetInvocationException: Исключение было сгенерировано целью вызова. ---> Unity.ResolutionFailedException: текущий тип, Interface.IGenericDao`1 [Клиент] является интерфейсом и не может быть построен Вы пропустили тип отображение

Пробовал и эту регистрацию тоже, но все та же ошибка:

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

а также:

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

1 Ответ

0 голосов
/ 03 июля 2019

Рассмотрите возможность сужения области действия интерфейсов.

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

Удалите интерфейс из регистрации и оставьте его в качестве открытой общей регистрации.

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

Ограничение типа для реализации будетопределить, какие классы можно использовать.

...