正确使用“使用”数据文本语句 [英] Proper use of "Using" statement for datacontext

查看:85
本文介绍了正确使用“使用”数据文本语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用Linq到Entities,我发现很多人推荐使用如下这样的使用语句来包装数据文本:

 使用(DataContext db = new DataContext){
var xx = db.customers;
}

这是有道理的。但是,我不知道如何将这种做法纳入我的模型。
例如:我有一个接口(我们称之为客户),它由一个这样的存储库实现:

  namespace模型
{
public class rCustomer:iCustomer
{

readonly DataContext db = new DataContext();

public customer getCustomer(Guid id)
{
return db.customers.SingleOrDefault(por => por.id == id);
}

public iQueryable< customer> getTopCustomers()
{
return db.customers.Take(10);
}

// ************************************* ********
//更多使用db的方法,包括添加,更新,删除等。
// **************** ***************************

}
}

然后,为了利用使用,我需要将方法改为如下所示:

 命名空间模型
{
public class rCustomer:iCustomer
{
public customer getCustomer(Guid id)
{
using(DataContext db = new DataContext()){
return db.customers.SingleOrDefault(por => por.id == id);
}
}

public iQueryable< customer> getTopCustomers()
{
using(DataContext db = new DataContext()){
return db.customers.Take(10);
}
}

// **************************** *************
//更多使用db的方法
// ********************* **********************

}
}

我的问题是:使用使用的建议真的很好吗?请考虑这个变化将是一个主要的我有大约25个接口/存储库组合,每个都有大约20-25个方法,更不用说完成后需要重新测试一切。
有其他方式吗?



谢谢!



Edgar。

解决方案

您可以实现一个数据库工厂,这将导致您的 DbContext被重用



您可以实现如下:



DatabaseFactory类:

  public class DatabaseFactory:Disposable,IDatabaseFactory 
{
private YourEntities _dataContext;
public YourEntities Get()
{
return _dataContext ?? (_dataContext = new YourEntities());
}
protected override void DisposeCore()
{
if(_dataContext!= null)
_dataContext.Dispose();
}
}

存储库基类的摘录:

  public abstract class Repository< T> :IRepository< T>其中T:class 
{
private YourEntities _dataContext;
private readonly IDbSet< T> _dbset;
protected Repository(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
_dbset = DataContext.Set< T>();
}

protected IDatabaseFactory DatabaseFactory
{
get;
私人集;
}

protected YourEntities DataContext
{
get {return _dataContext ?? (_dataContext = DatabaseFactory.Get());
}

您的表的存储库类:

  public class ApplicationRepository:Repository< YourTable> IYourTableRepository 
{
private YourEntities _dataContext;

保护新的IDatabaseFactory DatabaseFactory
{
get;
私人集;
}

public YourTableRepository(IDatabaseFactory databaseFactory)
:base(databaseFactory)
{
DatabaseFactory = databaseFactory;
}

保护新的YourEntities DataContext
{
get {return _dataContext? (_dataContext = DatabaseFactory.Get()); }
}

}
public interface IYourTableRepository:IRepository< YourTable>
{
}
}

这与AutoFac完美配合构造函数注入。


I’m using Linq to Entities and lately, I found that a lot of folks recommending wrapping the datacontext in a using statement like this:

Using(DataContext db = new DataContext) {
    var xx = db.customers;
}

This makes sense. However, I’m not sure how to incorporate this practice in my model. For example: I have an interface (let’s call it customer) and it is implemented by a repository like this:

namespace Models
{
    public class rCustomer : iCustomer
    {

        readonly DataContext db = new DataContext();

        public customer getCustomer(Guid id)
        {
            return db.customers.SingleOrDefault(por => por.id == id);
        }

        public iQueryable<customer> getTopCustomers()
        {
            return db.customers.Take(10);
        }

        //*******************************************
        //more methods using db, including add, update, delete, etc.
        //*******************************************

    }
}

Then, to take the advantage of using, I will need to change the methods to look like this:

namespace Models
{
    public class rCustomer : iCustomer
    {
        public customer getCustomer(Guid id)
        {
            using(DataContext db = new DataContext()) {       
                return db.customers.SingleOrDefault(por => por.id == id);
            } 
        }

        public iQueryable<customer> getTopCustomers()
        {
            using(DataContext db = new DataContext()) {       
                 return db.customers.Take(10);
            } 
        }

        //*******************************************
        //more methods using db
        //*******************************************

    }
}

My question is: the recommendation of using "Using" is really that good? Please take in consideration that this change will be a major one, I have about 25 interfaces/repository combos, and each has about 20-25 methods, not to mention the need to re-test everything after finish. Is there other way?

Thanks!

Edgar.

解决方案

You can implement a Database factory which will cause your DbContext is being reused.

You can achieve this as follows:

DatabaseFactory class:

public class DatabaseFactory : Disposable, IDatabaseFactory
{
    private YourEntities _dataContext;
    public YourEntities Get()
    {
        return _dataContext ?? (_dataContext = new YourEntities());
    }
    protected override void DisposeCore()
    {
        if (_dataContext != null)
            _dataContext.Dispose();
    }
}

Excerpt of the Repository base class:

 public abstract class Repository<T> : IRepository<T> where T : class
{
    private YourEntities _dataContext;
    private readonly IDbSet<T> _dbset;
    protected Repository(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        _dbset = DataContext.Set<T>();
    }

    protected IDatabaseFactory DatabaseFactory
    {
        get;
        private set;
    }

    protected YourEntities DataContext
    {
        get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
    }

Your table's repository class:

public class ApplicationRepository : Repository<YourTable>, IYourTableRepository
{
    private YourEntities _dataContext;

    protected new IDatabaseFactory DatabaseFactory
    {
        get;
        private set;
    }

    public YourTableRepository(IDatabaseFactory databaseFactory)
        : base(databaseFactory)
    {
        DatabaseFactory = databaseFactory;
    }

    protected new YourEntities DataContext
    {
        get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
    }

   }
    public interface IYourTableRepository : IRepository<YourTable>
   {
   }
}

This works perfectly together with AutoFac constructor injection as well.

这篇关于正确使用“使用”数据文本语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆