LINQ to SQL的 - 在ASP.NET MVC应用程序的DataContext设计问题和注意事项 [英] Linq to Sql - DataContext design issues and considerations in ASP.NET MVC application

查看:115
本文介绍了LINQ to SQL的 - 在ASP.NET MVC应用程序的DataContext设计问题和注意事项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的方法每个原子操作的单数据上下文,而使用LINQ在ASP.NET MVC应用程序SQL。

I am using "Single data context per atomic operation" approach while using Linq to Sql in an ASP.NET MVC application.

到目前为止,我一直在使用一个单独的datacontext,我学到了很多的问题,所以我重构了code每原子操作使用单一DataContext的。

So far, I have been using a singleton datacontext, which I learnt has many issues, so I refactored the code to use single datacontext per atomic operation.

控制器操作的例子现在如下(重构并没有改变这一点):

An example of a controller action is now as follows (refactoring has not changed this):

	public ActionResult List()
	{
		List<Request> requests = this.repository.AllRequests();
		return View(requests);
	}

库的类型是IRepository的。我想保持这种抽象能够切换到不同的数据层(基于我最近使用LINQ体验到SQL,这可能很快发生:))

repository is of type IRepository. I want to keep this abstraction to be able to switch to a different data layer (based on my recent experience with Linq to Sql, this may happen very soon :))

该LinqRepository实现AllRequests()方法如下:

The LinqRepository implements the AllRequests() method as follows:

	public List<Request> AllRequests()
	{
		using (DataModelDataContext connection = GetContext())
		{
			return connection.Requests.ToList();
		}
	}

(仅供参考,previously DataContext的实例是LinqRepository的领域,LinqRepository举行作为一个单一的静态实例)

(just for reference, previously DataContext instance was a field of the LinqRepository and LinqRepository was held as a single static instance)

在DataContext设置在方法返回之前。

The DataContext is disposed before the method returns.

视图code现在推迟访问属性时抛出异常ObjectDisposed:

The view code now throws ObjectDisposed exceptions when accessing deferred properties:

            <%= Html.Encode(request.Branch.Name) %> //throws

我学到了处置的DataContext可能不需要(此处<一个href=\"http://stackoverflow.com/questions/389822/when-should-i-dispose-of-a-data-context\">http://stackoverflow.com/questions/389822/when-should-i-dispose-of-a-data-context)

当我不处置的DataContext(除去使用),没有的ObjectDisposedException。

When I do not dispose the DataContext (remove the using), there is no ObjectDisposedException.

即:我改变了方法如下:

i.e.: I altered the method as follows:

	public List<Request> AllRequests()
	{
		DataModelDataContext connection = GetContext();
		return connection.Requests.ToList();
	}

但我想知道,什么是在这种情况下?

But I am wondering, what are the implications of not disposing DataContext instance in this scenario?

我知道我应该在DataContext之前阅读从实体实例(包括递延属性)中的所有数据被处置,但我不会向大家介绍另一个抽象(另一个Request类,复制到它的所有属性)

I know I am supposed to read all the data from the entity instance (including deferred properties) before the DataContext is disposed, but I would not like to introduce another abstraction (another Request class, copying all the properties over to it).

我的问题:

请问实体对象持有强引用其父的DataContext,$ P $被GC-ED pventing呢? (我猜是这样,突出部想100%确定)

Does the entity object hold a strong reference to its parent DataContext, preventing it from being GC-ed? (I guess it does, juts want to be 100% sure)

使用LINQ时,SQL,而数据层抽象需要保留你能提供的推荐办法的建议? (包括部分属性更新)

Can you provide advice on recommended approaches when using Linq to Sql while data layer abstraction needs to be retained? (including partial property updates)

有没有使用存储库抽象实施中的LINQ ASP.NET MVC?

Is there an open source project using a repository abstraction implementing Linq to Sql in ASP.NET MVC?

推荐答案

我的解决办法是有库实现IDisposable。然后我会实例化存储库作为在OnActionExecuting构造控制器的一个实例变量。然后,我会处理在OnResultExecuted存储库。到时候你到OnResultExecuted的结果已经变得和任何所需的数据已被消耗掉。资源库将同样实例化的DataContext在它的构造和它处置时的处置。

My solution would be to have the Repository implement IDisposable. Then I would instantiate the Repository as an instance variable in the controller in the constructor in OnActionExecuting. Then I would dispose of the repository in OnResultExecuted. By the time you get to OnResultExecuted the result has already been rendered and any data needed has been consumed. The repository would similarly instantiate the DataContext in it's constructor and dispose of it when it is disposed.

这篇关于LINQ to SQL的 - 在ASP.NET MVC应用程序的DataContext设计问题和注意事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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