温莎城堡寄存器:无法创建组件“ X.YService”,因为它具有要满足的依赖性

时间:2018-08-19 05:19:38

标签: asp.net-mvc-4 c#-4.0 repository entity-framework-5 castle-windsor

这应该是一个普遍的问题,我已经搜索过,但仍未找到任何解决方案,如果是欺诈行为,请给我指出适当的链接。 因此,我有一个通用的存储库来支持多个实体,有一段时间我像波纹管一样注册了它

控制器:这是我的控制器部分

        public class XController : Controller
            {

                private  ILegendsService _legendsService;
                public LegendsController(ILegendsService legendsService)
                {
                    _legendsService = legendsService;
                }
        } 

服务:这是我的传奇服务部分

         public class LegendsService:ILegendsService
            {
                private readonly IRepositoryFactory<Legends, int> _legends;
                private readonly IUnitOfWork _unitOfWork;
                private readonly IDiObjectMapper _mapper;
                public LegendsService(IRepositoryFactory<Legends, int> 
                    legendsInfo
                    , IUnitOfWork unitOfWork
                    , IDiObjectMapper mapper)
                {
                    _legends = legendsInfo;
                    _unitOfWork = unitOfWork;
                    _mapper = mapper;


                }
        }

RepositoryFactory:这是存储库工厂

    public interface IRepositoryFactory<TEntity, in TKey> : 
      IRepository<TEntity, TKey> where TEntity : class
         {
         }

        public class RepositoryFactory<TEntity, TKey> : Repository<TEntity, 
        TKey>, IRepositoryFactory<TEntity, TKey> where TEntity : class
        {
            public RepositoryFactory(IDatabaseFactory databaseFactory)
                : base(databaseFactory)
            {
            }
        }

UnitOfWork:这是我的unitofWorkSection

    public interface IUnitOfWork : IDisposable
    {
        void Commit();
        Task CommitAsync();
    }

    public class UnitOfWork : IUnitOfWork
    {
        private readonly IDatabaseFactory _databaseFactory;
        private bool _disposed = false;
        private KYCContext _dataContext;

        public UnitOfWork(IDatabaseFactory databaseFactory)
        {
            this._databaseFactory = databaseFactory;
        }

        protected KYCContext DataContext => _dataContext ?? (_dataContext = _databaseFactory.Get());
}

DIObjectMapper:这是我的DIObjectMapper

 public interface IDiObjectMapper
    {
        TDestination Map<TSource, TDestination>(TSource source)
            where TSource : class
            where TDestination : class;

        TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
            where TSource : class
            where TDestination : class;
    }

    public class DiObjectMapper : IDiObjectMapper
    {
        public TDestination Map<TSource, TDestination>(TSource source)
            where TSource : class
            where TDestination : class
        {
            return AutoMapperSetup.Mapper.Map<TSource, TDestination>(source);
        }

        public TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
            where TSource : class
            where TDestination : class
        {
            return AutoMapperSetup.Mapper.Map(source, destination);
        }
    }

温莎城堡:这是温莎城堡集装箱登记处

   container.Register(Component.For<IDisposable> 
    ().ImplementedBy<IDatabaseFactory>().LifestylePerWebRequest());
     container.Register(Component.For<IRepository<Legends, int>> 
   ().ImplementedBy<Repository<Legends, int>> 
   ().LifestylePerWebRequest());
    container.Register(Component.For<ILegendsService> 
   ().ImplementedBy<LegendsService>().LifestylePerWebRequest());
        container.Register(Component.For<IDatabaseFactory> 
   ().ImplementedBy<DatabaseFactory>().LifestylePerWebRequest());
        container.Register(Component.For<IUnitOfWork> 
   ().ImplementedBy<UnitOfWork>().LifestylePerWebRequest());
        container.Register(Component.For<IRepositoryFactory<Legends, int>> 
    ().ImplementedBy<RepositoryFactory<Legends, int>> 
    ().LifestylePerWebRequest());
        container.Register(Component.For<IDiObjectMapper> 
    ().ImplementedBy<DiObjectMapper>().LifestylePerWebRequest());
        container.Register(Component.For<IDisposable> 
    ().ImplementedBy<Disposable>().LifestylePerWebRequest());
        container.Register(Component.For<IRepository<Legends, int>> 
     ().ImplementedBy<IRepositoryFactory<Legends, int>> 
     ().LifestylePerWebRequest());

注意:我没有提到存储库和IRepository,它们实际上只包含相同的通用CRUD操作

1 个答案:

答案 0 :(得分:0)

这解决了我的问题:

         container.Register(Component
           .For(typeof(IRepositoryFactory<,>))
           .ImplementedBy(typeof(RepositoryFactory<,>))
           .LifestylePerWebRequest());
         container.Register(Component
           .For(typeof(IRepository<,>))
           .ImplementedBy(typeof(Repository<,>))
           .LifestylePerWebRequest());