使用 ViewModel 时保持 Linq to SQL 活动 (ASP.NET MVC)
我最近开始使用自定义 ViewModel(例如,CustomerViewModel)
public class CustomerViewModel
{
public IList<Customer> Customers{ get; set; }
public int ProductId{ get; set; }
public CustomerViewModel(IList<Customer> customers, int productId)
{
this.Customers= customers;
this.ProductId= productId;
}
public CustomerViewModel()
{
}
}
...现在将它们传递到我的视图而不是集合本身(例如,var Custs =repository.getAllCusts(id)),因为这似乎是很好的做法所以。
我遇到的问题是使用 ViewModels 时;当它到达视图时,我已经失去了向客户延迟加载的能力。在使用它们之前我不认为是这种情况。
是否可以在仍然使用 ViewModel 的同时保留延迟加载的能力? 或者我必须使用这种方法急切加载吗?
谢谢, 科汉。
I have recently started using custom ViewModels (For example, CustomerViewModel)
public class CustomerViewModel
{
public IList<Customer> Customers{ get; set; }
public int ProductId{ get; set; }
public CustomerViewModel(IList<Customer> customers, int productId)
{
this.Customers= customers;
this.ProductId= productId;
}
public CustomerViewModel()
{
}
}
... and am now passing them to my view instead of the collections themselves (for example, var Custs = repository.getAllCusts(id) ) as it seems good practice to do so.
The problem i have encountered is that when using ViewModels; by the time it has got to the the view i have lost the ability to lazy load on customers. I do not believe this was the case before using them.
Is it possible to retain the ability of Lazy Loading while still using ViewModels?
Or do i have to eager load using this method?
Thanks,
Kohan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想阻止您的 L2S 客户延迟加载其属性的原因是您的 DataContext 是由您的存储库处置的。
一种解决方案是不处置 DataContext 直到请求得到处理 - 互联网上有很多处理此问题的模式。
另一种选择是不使用延迟加载。由于您拥有一批客户,因此我想说 立即加载您需要的任何内容,否则您将每个客户访问数据库一次
I imagine that what's stopping your L2S Customer from lazy loading it's properties is that your DataContext is disposed by your repository.
One solution is to not dispose your DataContexts until the request has been dealt with - there's quite a few patterns on the internet for dealing with this.
Another option is to not use lazy loading. Since you've got a collection of customers, I'd say that it would be a really good idea to eagerly load whatever it is you need, otherwise you'll be hitting the database once per customer
从技术上讲,是的,可以通过
IEnumerable
属性在视图模型上公开延迟加载的集合。只需公开 getter 并让 getter 执行 Linq-to-sql 查询。不过,我自己在这方面并没有取得太大成功,因为我更喜欢通过 ReadOnlyObservableCollection 在视图模型上公开集合。直接使用该集合必须立即加载数据。我可以想象创建一个像
ReadOnlyObservableCollection<>
这样的集合,它会作弊,并且仅在用户调用需要它的内容(例如,索引到集合中)时才会进行急切加载,但我从不关心足以实际实现这样的事情。Technically, yes, it's possible to expose the lazy loaded collection on the viewmodel via an
IEnumerable<>
property. Just expose the getter and have the getter perform the Linq-to-sql query.I've not had much success with it myself, though, because I prefer to expose collections on viewmodels via a
ReadOnlyObservableCollection<>
. Using that collection directly must load the data eagerly. I can imagine creating a collection like theReadOnlyObservableCollection<>
that cheats and only does an eager load if the user calls something that requires it (e.g., indexing into the collection), but I've never cared enough to actually implement such a thing.