简体   繁体   English

MVC存储库模式:创建模型类

[英]MVC Repository Pattern: Creating Model Classes

Reviewing Conery's storefront, and I dont understand why he used Linqs auto-generated classes (ie Order class) and then he has another Order class defined that is not a partial class. 回顾Conery的店面,我不明白为什么他使用了Linqs自动生成的类(即Order类),然后他定义了另一个不是部分类的Order类。 WHen using repository pattern should one manually create the classes, and disregard Datacontext altogether? 使用存储库模式时应该手动创建类,并完全忽略Datacontext吗?

Rob has answered on this question in one of his show. Rob在他的一个节目中回答了这个问题。

He Using POCO classes to be aware from all dataaccess classes. 他使用POCO类来了解所有数据访问类。 For example when he change LINQ-to-SQL to NHibernate all he will need to do i change his "mappings" in his filters, and he will not have to make any changes in bussiness logic. 例如,当他将LINQ-to-SQL更改为NHibernate时,他需要在他的过滤器中更改他的“映射”,并且他不必对业务逻辑进行任何更改。

If you don't decouple your front end from the linq classes using an intermediary class, you can't control with the data context gets garbage collected. 如果不使用中间类将前端与linq类分离,则无法使用数据上下文控制垃圾收集。 Typically with data context types of instances you want to rid of them as soon as you're done using them. 通常使用数据上下文类型的实例,您希望在使用它们后立即将其删除。 Here's how you might want to do this with the linq to sql context: 以下是使用linq to sql上下文执行此操作的方法:

using (MyDataContext data = new MyDataContext())
{
    SomeThing thing = data.Things(t => t.ID == 1);
    return thing;
}
... the MyDataContext instance is gone

With the "using" block, you're disposing of the instance of MYDataContext at the last "}". 使用“using”块,您将在最后一个“}”处理MYDataContext的实例。 However, if you do this you'll get an error then trying to use "thing" because the data context instance is gone. 但是,如果你这样做,你会得到一个错误,然后尝试使用“东西”,因为数据上下文实例已经消失。 If you don't dispose of the data context, it's left hanging around until it's eventually garbage collected. 如果你处理数据上下文,它会一直闲逛,直到它最终被垃圾收集。

If you introduce an intermediary class to decouple the linq to sql code from the calling app you can still get rid of your data context instance and return the same data (just in a different object): 如果您引入一个中间类来将linq与sql代码从调用应用程序分离,您仍然可以摆脱数据上下文实例并返回相同的数据(仅在不同的对象中):

using (MyDataContext data = new MyDataContext())
{
    SomeThing thing = data.Things(t => t.ID == 1);
    SometThingElse otherThing = ConvertSomethingToSomethingElse(thing);
    return otherThing;
}
... the MyDataContext instance is gone

Hope that helps. 希望有所帮助。

He said in one of his recent videos he doesn't like the way LINQ to SQL does mapping. 他在最近的一个视频中说,他不喜欢LINQ to SQL映射的方式。 I agree though I think it is complete overkill. 我同意,但我认为这是完全矫枉过正的。

I'd say you're not breaking any major design patterns as long as you're sticking to the repository pattern itself. 我会说,只要您坚持使用存储库模式本身,就不会破坏任何主要的设计模式。 I think it's a matter of choice to have 2 sets of classesa, allbeit a bad one, still a choice. 我认为选择2套课程是一个选择问题,但仍然是一个选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM