简体   繁体   中英

.NET MVC + Unity: some basic help needed

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

Unity Container / Microsoft Dependency Injection

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM