从基本控制器继承LINQ到SQL数据上下文

从基本控制器继承LINQ到SQL数据上下文

本文介绍了从基本控制器继承LINQ到SQL数据上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基础类, BaseController ,是面向公众的控制器继承访问请求之间共享数据环境使用LINQ到SQL。


  • 是我访问我的数据上下文以有效和安全的方式通过 HttpContext.Current.Items 存放每个HTTP请求?

DataContextHelper

 内部静态类DataContextHelper
{
    公共静态MyDataContext CurrentContext
    {
        得到
        {
            如果(HttpContext.Current.Items [MyDataContext] == NULL)
            {
                MyDataContext语境=新MyDataContext();
                HttpContext.Current.Items [MyDataContext] =背景;
            }
            回报(MyDataContext)HttpContext.Current.Items [MyDataContext];
        }
    }
}

BaseController 类:

 公共类BaseController:控制器
{
    保护MyDataContext分贝
    {
        获得{
            返回DataContextHelper.CurrentContext;
        }
    }
}

的HomeController 类:

  [的HandleError]
公共类HomeController的:BaseController //继承分贝成员
{
    公众的ActionResult SomeAction(字符串ID)
    {
        用户的用户= db.Users.First(U => u.UserId == ID);
        // ... 做东西
        db.SubmitChanges();
    }
}


解决方案

是的,这是所有与两个WebForms和MVC的主要奥姆斯一个共同的模式。

执行每个控制器动作后,我唯一想补充一个明确的处置。只是为了确保一切正确处置。

BaseController.cs

 保护覆盖无效OnActionExecuted(ActionExecutedContext filterContext)
    {
        如果(HttpContext.Current.Items [MyDataContext] == NULL)
            返回;        VAR上下文=(MyDataContext)HttpContext.Current.Items [MyDataContext];        context.Dispose();
    }

My base controller class, BaseController, is inherited by public-facing controllers to access a shared data context between requests with LINQ-to-SQL.

  • Am I accessing my data context in an efficient and safe way by storing it in HttpContext.Current.Items for each HTTP request?

DataContextHelper class

internal static class DataContextHelper
{
    public static MyDataContext CurrentContext
    {
        get
        {
            if (HttpContext.Current.Items["MyDataContext"] == null)
            {
                MyDataContext context = new MyDataContext();
                HttpContext.Current.Items["MyDataContext"] = context;
            }
            return (MyDataContext)HttpContext.Current.Items["MyDataContext"];
        }
    }
}

BaseController class:

public class BaseController : Controller
{
    protected MyDataContext db
    {
        get {
            return DataContextHelper.CurrentContext;
        }
    }
}

HomeController class:

[HandleError]
public class HomeController : BaseController // inherits db member
{
    public ActionResult SomeAction(string id)
    {
        User user = db.Users.First(u => u.UserId == id);
        // ... do stuff
        db.SubmitChanges();
    }
}
解决方案

Yes, this is a common pattern with all the major ORMs with both WebForms and MVC.

The only thing I would add a explicit dispose after each controller action is executed. just to make sure everything is disposed of correctly.

In BaseController.cs:

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (HttpContext.Current.Items["MyDataContext"] == null)
            return;

        var context = (MyDataContext)HttpContext.Current.Items["MyDataContext"];

        context.Dispose();
    }

这篇关于从基本控制器继承LINQ到SQL数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:24