locked
Problem with unity resolving nested constructors. RRS feed

  • Question

  • User442647680 posted

    I am having two interfaces like ICustomerRepository, IBalanceRepository which are implemented by CustomerRepository.cs and BalanceRepository. Now for entry i have customerservice.cs with constuctor accepting  instance of ICustomerRepository. I am calling a method from customerservice.cs to BalanceService.cs which is having constructor accepting instance of IBalancerepository. I registered all classes using unity and resolved customer service, this is resolving ICustomerRespository but not IBalanceRepository. Can we resolve IBalanceRepository repository like this or not? See below code and suggest a solution.

    public interface ICusotmerRepository
        {
           void Add(string strCustomer);
        }
    
    public interface IBalanceRepository
        {
            void Add(decimal decAmount);
        }
    
    class CusotmerRepository : ICusotmerRepository
        {
            public void Add(string strCustomer)
            {
                Console.WriteLine("Added customer "+strCustomer);
            }
        }
    
    class BalanceRepository : IBalanceRepository
        {
            public void Add(decimal decAmount)
            {
                Console.WriteLine("added amount "+decAmount);
            }
        }
    
    public class BalanceService
        {
            private readonly IBalanceRepository _balanceRepository;
    
            public BalanceService(IBalanceRepository balanceRepository)
            {
                _balanceRepository = balanceRepository;
            }
    
            public BalanceService()
            {
            }
    
            public void AddBalance(decimal decBalance)
            {
                _balanceRepository.Add(decBalance);
            }
        }
    
    class CustomerService
        {
            private readonly ICusotmerRepository _cusotmerRepository;
            public CustomerService(ICusotmerRepository cusotmerRepository)
            {
                _cusotmerRepository = cusotmerRepository;
            }
    
            public void Process(string strCustomerName, decimal decBalance)
            {
                _cusotmerRepository.Add(strCustomerName);
                var objBalanceService = new BalanceService();
                objBalanceService.AddBalance(decBalance);
            }
        }
    
    class Program
        {
            static void Main(string[] args)
            {
                var container = new UnityContainer();
                container.RegisterType<IBalanceRepository, BalanceRepository>();
                container.RegisterType<ICusotmerRepository, CusotmerRepository>();
    
                var customerService = container.Resolve<CustomerService>();
                customerService.Process("Ganesha",7);
            }
        }

       Thanks in advance.

    Thursday, December 4, 2014 9:09 AM

Answers

  • User86105595 posted

    Hi,

    To solve this, you cannot use the default constructor to instanciate BalanceService, you need the "UnityContainer" to resolve its dependencies. Like you did in "Main" to resolve the "CustomerService".


    You can add it to "CustomerService" constructor, than the container will resolve it like it did to the "CustomerRepository".

    Att.,

    Douglas Picolotto.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 5, 2014 5:52 AM