Home > front end >  .NET MVC Unity: some basic help needed
.NET MVC Unity: some basic help needed

Time:10-24

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...

CodePudding user response:

Unity Container / Microsoft Dependency Injection

enter image description here

CodePudding user response:

Yes, you have to put every registration of the interface\concrete implementation inside of RegisterComponents(). I assume you defined interface for you DbContext class.(Developers like being complemented, rnt we?;)) 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));
  • Related