3

I am new with the dependency injection pattern. I'm a little confused about few things.

Scenario:

I have a class library project called 'MailCore'. This project has interfaces and classes that perform all email sending stuff. I have an MVC project called 'The site'. It uses the 'MailCore' project to send email. I have Unity in this project and the UnityContainer is registered and things are working fine.

I also have another class library library project called 'SiteRepo'. Sometimes, to perform some specific tasks, I have to send email from this project. Therefore the 'MailCore' project is referenced in this project, too.

Problem:

I have installed Unity from NuGet in the 'SiteRepo' project and it doesn't seem to create any UnityConfig in this class library project. How would I register a UnityContainer here?

Code:

TheSite:

Public class JobController : Controller
{
    private readonly IEmailBuilder mailBuilder;
    public JobController(IEmailBuilder mailBuilder)
    {
        this.mailBuilder = mailBuilder;

    }
    public ActionResult Create(....)
    {
      JobRepo j = new JobRepo();
      j.Create(....);
    }

}

UnityConfig (this is in the web app 'The Site'):

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();

        container.RegisterType<IEmailBuilder, EmailBuilder>();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

SiteRepo:

Public class JobRepo()
{
   Public bool Create(...) 
   {
       //some other code to create a job....

       //have to send email using MailCore !! The problem area in concern..
   }
}
bubbleking
  • 3,329
  • 3
  • 29
  • 49
Reza.Hoque
  • 2,690
  • 10
  • 49
  • 78
  • I believe this question deserves a better answer other than use something other than Unity. – iGanja Apr 24 '20 at 15:52

1 Answers1

6

If you must use a DI Container like Unity (instead of Pure DI), you should install it into your Composition Root, which is 'The site'.

From there, you can reference the library projects and configure your container.

Steven
  • 166,672
  • 24
  • 332
  • 435
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • I already have unity installed in the 'The Site'. And 'The Site' has 'SiteRepo' and 'MailCore' Referenced individually. But i have some situations where, 'The Site' calls a method from 'SiteRepo'. and inside that method 'SiteRepo' calls a method from 'MailCore' to send email. So based on your answer and all the links you provided, does it mean i should move that email sending code out of 'SiteCore' and bring it to 'The Site' ?? – Reza.Hoque Jun 16 '15 at 20:28
  • Since 'The Site' references both libraries, it should be possible to configure Unity to inject a type from MailCore into the class in SiteRepo that hosts the method you care about. – Mark Seemann Jun 16 '15 at 20:41
  • Mark, SiteRepo has a class called JobRepo which has a method called CreateJob. This CreateJob should call a something in MailCore to send email. JobRepo class does not implement any Interface from MailCore. In this case how would I configure unity ?? Isn't it container.RegisterType() ?? Sorry if I sound like stupid :) – Reza.Hoque Jun 17 '15 at 07:48
  • IIRC, with Unity, you don't have to register concrete classes; Unity will just pick them up and create them for you. – Mark Seemann Jun 18 '15 at 08:20
  • Correct, you do not need to register concrete classes if you are ok with the defaults: (transient lifetime, no name, no interception, no property injection). But you can register a concrete type if you want to customize the registration. `container.RegisterType(new ContainerControlledLifetimeManager());` (to register as a singleton for example) – TylerOhlsen Jun 19 '15 at 02:00
  • @MarkSeemann I am working with Unit Test Project(For Integration testing my service layer without controller), which unlike MVC project do not have DependencyResolver, So where should I put my UnitContainer object so that while loading an object in memory it automatically resolves the registered types. – Ashutosh Singh Apr 09 '19 at 10:55
  • 1
    @AshutoshSingh Compose object graphs in your tests https://stackoverflow.com/a/1465896/126014 – Mark Seemann Apr 09 '19 at 14:50
  • What If I don't want a direct reference from my site to my repository? If this is the only way to register my associations, it seems rather limiting. I think the OP has a valid scenario that should have an answer. – iGanja Apr 24 '20 at 15:51