1

I am kind of new to Unity and DI terminology, hence trying to understand how does it work. I have following code which implements DI using Unity container.

public class DashboardService: IDashboardService
{
    private readonly IRepository<USERROLE> repoUserRole;
    private readonly IRepository<INSTITUTION> repoInstitution;

    public DashboardService(
        IRepository<USERROLE> repoUserRole, IRepository<INSTITUTION> repoInstitution)
    {
        this.repoUserRole = repoUserRole;
        this.repoInstitution = repoInstitution;
    }

    public List<USERROLE> GET(List<string> Id)
    {
        // Use repoUserRole object to get data from database
    }
}

This service is being called by the controller:

public class DashboardController : ApiController
{
    private readonly IDashboardService dashboardService;

    public DashboardController(IDashboardService dashboardService)
    {
        this.dashboardService = dashboardService;
        this.mapper = mapper;
    }

    //Action method which uses dashboardService object
}

Here is the Unity configuration:

var container = new UnityContainer();

container.RegisterType(typeof(IDashboardService), typeof(DashboardService))
.RegisterType(typeof(IRepository<>), typeof(Repository<>));

return container;

Questions:

  1. As of now, my code works fine, but if I comment out the DashboardService constructor, I am getting the null repository objects.
  2. I am resolving the dependencies in Unity for repository interfaces, so why I am getting null there?
  3. Is there any way to pass the repository dependancy without using the constructor pattern?
3
  • "ApiController". Please list which version of the dotNetFramework you are using....Please list germane nuget packages. And please list the namespaces you are using. Commented Sep 29, 2018 at 10:54
  • This article is probably what you're looking for : learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/… Please use constructor based injection. Commented Sep 29, 2018 at 10:56
  • I strongly recommend a copy of ISBN-13: 978-1935182504 ISBN-10: 1935182501 . Used ones of edition-1 are like $15. The book as a second edition on the horizon as well. This will be the best money you might ever spend on writing better code. Commented Sep 29, 2018 at 10:59

1 Answer 1

3

if I comment out the DashboardService constructor, I am getting the null repository objects.

When you don't add a constructor to a class, C# will generate a public parameterless constructor for you during compilation. This causes Unity to call that 'invisible' parameterless constructor, and that's why none of your private fields are initialized.

To prevent these kinds of accidental programming errors, always make sure you enable "treat all warnings as errors" in your project's properties build tab. This will make sure the compiler stops compiling because it detects these uninitialized fields.

Is there any way to pass the repository dependancy without using the constructor pattern?

Yes there is, but every other method you could use leads to either a code smell or an anti-pattern. Constructor injection is in almost all cases the best solution.

Sign up to request clarification or add additional context in comments.

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.