0

MVC application using Identity and EF.

I want to implement dependency injection using Unity. I have installed Unity in the project (both Unity and Unity.mvc5), but now I am lost at how to implement it... As of now, (before changing anything) I instantiate dbcontext in every controller, then in controller constructor I create service instances with new xxxx(db).

My controllers are like this:

public class SomeController : Controller {
    private ApplicationDbContext db = new ApplicationDbContext("DefaultConnection");
    private XxService xxService;

    public SomeController() {
        this.xxService = new XxService(db);
    }

    public ActionResult Index() {
        string name = xxService.Foo(5);
        return View();
    }
}

then I have my Services:

public class XxService {
    private ApplicationDbContext db;
    private YyService yyService;

    public XxService(ApplicationDbContext db) {
        this.db = db;
        this.yyService = new YyService(db);
    }

    public string Foo(int id) {
        Customer customer = yyService.Bar(id);
        return customer.Name;
    }
}

public class YyService {
    private ApplicationDbContext db;
    
    public YyService(ApplicationDbContext db) {
        this.db = db;
    }
    
    public Customer Bar(int id) {
        return db.Customers.Find(id);
    }
}

and the unity config:

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>();
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        //container.RegisterType<XxService>(new Unity.Injection.InjectionConstructor());
    }
}

I dont really understand what and how should I register in Unity config, and/or what interfaces should I create... Should I register the Services? the dbcontext? both? and... how? some code would be great, this is driving me nuts...

patsy2k
  • 471
  • 2
  • 8
  • I think you might find this post worth reading through: https://stackoverflow.com/questions/39173345/unity-with-asp-net-core-and-mvc6-core – John Oct 23 '21 at 19:39

2 Answers2

0

Unity Container / Microsoft Dependency Injection

https://github.com/unitycontainer/microsoft-dependency-injection

enter image description here

John
  • 116
  • 1
  • 4
0

Yes, you have to put every registration of the interface\concrete implementation pair inside of RegisterComponents(). I assume you defined interface for you DbContext class. So you have done pretty much everything except registering services:), like this:

var container = new UnityContainer();

container.RegisterType<IService1, Service1>(new PerRequestLifetimeManager());
container.RegisterType<IService2, Service2>(new SingletonLifetimeManager());

container.RegisterType<IMyDbContext, MyDbContext>(new PerRequestLifetimeManager(),
    new InjectionConstructor("name=MyDbConnection");
  
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
fatherOfWine
  • 1,191
  • 16
  • 39