What is the best practice for using Unity Mvc and Entity Framework?

Viewed 251
  • What is the best practice to instantiate my DbContext?

I see some people creating an empty interface for the DbContext like this:

public interface IEasyDayContext : IDisposable { }

and then register it in Unity like this (UnityConfig.cs):

container.RegisterType<IEasyDayContext, EasyDayContext>();

I see others using the DbContext as an "interface":

container.RegisterType<DbContext, EasyDayContext>();

I see other people just instantiating the DbContext in the constructor of the service.

  • I am finding it hard to get updated documentation on Unity Mvc, should I even use it today?

  • How do I setup Unity Mvc?

Current documentation (from https://code.msdn.microsoft.com/Dependency-Injection-in-11d54863) suggests that I should put this line of code in my Global.asax.cs file:

UnityConfig.RegisterComponents(); 

However, this method doesn't exist anymore. Should I use this instead?

UnityConfig.RegisterTypes(UnityConfig.Container);
  • Should I use PerRequestLifetimeManager?

I am using

AspNet.Mvc 5.2.6
Unity.Mvc 5.0.15
Unity.Container 5.8.10
0 Answers
Related