ASP.NET MVC Global DB

I'm trying to set the variable DB as global and use anywhere. My problem is that I'm initialising twice the db. In the base controller and in atributes. ie:

public class SettingsAttribute : ActionFilterAttribute {
  public ApplicationDbContext db = new ApplicationDbContext();
  public override void OnActionExecuting(ActionExecutingContext filterContext) {
    ...
}

and

[Settings]
public class BaseController : Controller {
  public ApplicationDbContext db = new ApplicationDbContext();
  protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state) {
    ...
}

I would like to create the var db just once, and access anywhere in the project. How ca I do this?


把它放在另一个类(例如Helper)中,然后像这样访问它:

private ApplicationDbContext db = null;
public ApplicationDbContext Db {
    get {
        if (db == null)
            db = new ApplicationDbContext();
        return db;
    }
}

Consider using of Dependency Injection pattern. For .NET there is for example Unity container, which implements a lightweight, extensible dependency injection container. Check the List of dependency injection containers for .NET, but it's quite old.

In general, Dependency injection is called Inversion of Control (IoC). That means, your classes, which are using for example your DB class, don't need to be dependent on a specific DB class. Instead, they just require a specific interface. DB class implementing this interface is injected from the outside and your class is decoupled from a specific implementation of DB class.

Some places to start with:

  • Dependency Injection on Wikipedia.org
  • Why does one use dependency injection?
  • An Absolute Beginner's Tutorial on Dependency Inversion Principle, Inversion of Control and Dependency Injection
  • Dependency Injection in Windows Communication Foundation

  • Best practice in this situation and (similar situations) is to implement a DI/IoC pattern , as @Dawid-Ferenczy mentioned in his answer. if you never worked with it before , it's a little abstract at the beginning but it's very useful in the long run , especially if your objects got complicated. You can search online for articles to learn more about the pattern itself.

    @Dawid-Ferenczy mentioned the Unity container, but it's not limited to that , you can use any container you would like (Or even create your own if you need to ). I personally use Ninject as it has a has a lot of support documentation and i found it easy to implement.

    In your case (DB context) i would use Ninject and DI/IoC as follow:

    1- Declare an interface for your DB Context DAL:

      public interface IModelDBContext
        {
    //Your methods should go here
        }
    
  • Your concrete class:

    public class ModelDBContext : DbContext,IModelDBContext { //Your methods and other stuff here }


  • Container part

    (Ninject in my case , but you can use any other container)

    public class NinjectDependencyResolver : IDependencyResolver
        {
        //Bunch of initialization and methods 
        Check out this : [Using ninject in MVC][2].
        //Binding piece (Very important)
         private void AddBindings()
                {
                    //Context DB Binding
                    kernel.Bind<IModelDBContext>().To<ModelDBContext>();
                    //Other binding
                }
        }
    

    Implementation part:

    This is the fun and exciting part. anywhere in your application , if you need to implement your specific DB context you inject it with the constructor and it will be available for you to use:

    In your case it will be something like this:

        Public YourConcreteClass
        {
        Private IModelDBContext ModelDB; //Initiate an instance that you will use .
    
    
    //DI by constructor
    public YourConcreteClass(IModelDBContext mDB)
            {
        ModelDB=mDB;
    
            }
    //in the rest of your code you call ModelDB that has access to all of the methods and attributes you might need
    
        }
    
    链接地址: http://www.djcxy.com/p/82252.html

    上一篇: 如何为改变数据的方法编写单元测试?

    下一篇: ASP.NET MVC全局数据库