Search code examples
asp.net-mvc-4c#-4.0repositoryentity-framework-5castle-windsor

Castle Windsor Register: Can't create component 'X.YService' as it has dependencies to be satisfied


This should be a general problem, i've searched but couldn't found any solution yet, if it is a dupe, please point me appropriate link. So, i have a generic repository to support several entites, for a moment i register it like bellow

Controller: This is my controller Section

        public class XController : Controller
            {

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

Service:This is my legends Service Section

         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:This is repository factory

    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:This is my 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: This is my 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);
        }
    }

Castle Windsor: This is the Castle Windsor Container Register area

   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());

Note: I didnot mentioned Repository and IRepository they actually contain same generic CRUD operation only


Solution

  • This solved my problem :

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