0

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

2 Answers 2

1

The problem is here:

public void Process(string strCustomerName, decimal decBalance)
{
    _cusotmerRepository.Add(strCustomerName);
    var objBalanceService = new BalanceService();    <------------
    objBalanceService.AddBalance(decBalance);
}

You are calling the parameterless constructor directly - there's nowhere for Unity to "inject" the dependency into BalanceService.

I see three possibilities:

  1. Add a constructor parameter to CustomerService for BalanceService and let Unity inject it
  2. Add a constructor parameter to CustomerService for IBalanceRepository and let Unity inject it, passing it in when you construct BalanceService
  3. Add IBalanceRepository or BalanceService as a parameter to Process
Sign up to request clarification or add additional context in comments.

2 Comments

If we are passing IBalanceRepository from root then what is the use of unity? It's like passing all dependencies manually. In this example i have only one dependency but in my real project i have so many dependencies which are very hard pass like one class to another. Is there any way so that wherever we use IBalanceRepository, the dependency should resolve to BalanceRepository.
Unity will see that CustomerService requires an IBalanceRepository and resolve it to a concrete class. The way you've designed it CustomerService depends on BalanceService (which depends on IBalanceRepository, so if you want to use DI effectively you're going to have to let unity inject the dependency.
1

You can't resolve BalanceService in this way because Unity will not be able to know when you need to resolve this dependency. You can inject BalanceService into customer service through constructor parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.