简体   繁体   English

MVC3应用程序/服务层/存储库层/ POCO类/ EF4 - 问题!

[英]MVC3 App/Service Layer/Repository Layer/POCO Classes/EF4 - Questions!

I am new to this whole design concept, and in reading for the last few weeks I have gathered a lot of information, but it seems scattered and conflicted. 我是这个整体设计概念的新手,在过去几周的阅读中,我收集了大量信息,但它似乎分散且矛盾。 Terms are mixed, and I am just having a hard time wrapping my mind around this. 条款很复杂,我只是很难将这种想法包围起来。

The pattern I am using is like this and assume the flow as follows: 我使用的模式是这样的,并假设流程如下:

MVC Application MVC应用程序
The controller(s) process the request/response from the client for a given view. 控制器处理来自客户端的给定视图的请求/响应。 Inside the controllers action methods, they contact the services (Service Layer) and either request objects to build the view models, and sen the objects from the view models back. 在控制器操作方法内部,它们联系服务(服务层)并请求对象以构建视图模型,并从视图模型中检索对象。

View Models 查看模型
I am using strongly typed view models to and from the views. 我在视图中使用强类型视图模型。

Are view models DTO's? 是视图模型DTO吗? Should they contain just simple properties like Name, AddressLine1, Address City, etc, or should they contain complex properties, multiple objects, etc. 它们应该只包含Name,AddressLine1,Address City等简单属性,还是应该包含复杂属性,多个对象等。

Is the validation in the view model. 是视图模型中的验证。 If so would it be validation like required fields, field length, etc. Then validation like user name already exists, or where you would need to interact with other objects in the service layer? 如果是这样,它将是必需的字段,字段长度等验证。然后验证用户名已经存在,或者您需要与服务层中的其他对象进行交互?

Can the view models just contain the POCO classes returned from EF, or should I be using the AutoMapper? 视图模型可以只包含从EF返回的POCO类,还是应该使用AutoMapper?

If using AutoMapper and DTO, are DTO's clones of the POCO classes? 如果使用AutoMapper和DTO,DTO是POCO类的克隆吗?

Would you map in the controller, view model, or in the service layer below? 您会在控制器,视图模型或下面的服务层中进行映射吗?

Services 服务
To me, the service(s) are objects that contact the repository(s) to get POCO objects back from the EF. 对我来说,服务是与存储库联系以从EF获取POCO对象的对象。 This is where all of my business logic is. 这就是我所有业务逻辑的所在。 Once the service hands an object back to a repository to be persisted to the EF, they are considered valid objects. 一旦服务将对象传递回存储库以持久保存到EF,它们就被视为有效对象。 Is this correct? 这个对吗?

Repositories
There is no business logic in them, they are just used to transport objects between the service(s) and the EF. 它们中没有业务逻辑,它们仅用于在服务和EF之间传输对象。 Is this correct? 这个对吗? I am implementing Interfaces here with generic repository. 我在这里使用通用存储库实现接口。 Then you could extend the generic repository for special needs? 那么你可以扩展通用存储库以满足特殊需求吗?

Questions About Terminology 关于术语的问题
1) Is a business object equal to a domain object? 1)业务对象是否等于域对象? How much logic should a domain object contain? 域对象应包含多少逻辑?

2) Is the domain model the EF model? 2)域模型是EF模型吗? I am using the Model-First approach. 我正在使用Model-First方法。

3) Dependency Injection - Should I be using this? 3)依赖注入 - 我应该使用它吗? I understand how it works, just don't get the real benefit. 我理解它是如何工作的,只是没有得到真正的好处。 I was playing with Ninject. 我和Ninject一起玩。

I think the community would benefit from some sort of wiki that contained all the best practices with code samples. 我认为社区将受益于某种包含代码示例的所有最佳实践的wiki。 Is there something like that out there? 那里有类似的东西吗? A lot of the samples out there are very simple, and a lot of the Microsoft samples do not use this pattern even when claiming to. 很多样本都非常简单,许多微软样本即使在声称时也不使用这种模式。

Thanks in advance to everyone who has and will help me with this. 提前感谢所有拥有并将帮助我的人。

BTW - I think StackOverflow needs a little, "Buy Me A Beer" button next to the "Accept Answer" checkbox :) 顺便说一句 - 我认为StackOverflow需要一点点,“接受答案”复选框旁边的“买我一个啤酒”按钮:)

Are view models DTO's? 是视图模型DTO吗?

Could be considered a sort of data transfer objects between the controller and the view. 可以被认为是控制器和视图之间的一种数据传输对象。

Should they contain just simple properties like Name, AddressLine1, Address City, etc, or should they contain complex properties, multiple objects, etc. 它们应该只包含Name,AddressLine1,Address City等简单属性,还是应该包含复杂属性,多个对象等。

Ideally simple properties but could also aggregate other view models but no models there (ex: like EF models). 理想的简单属性,但也可以聚合其他视图模型,但没有模型(例如:像EF模型)。

Is the validation in the view model. 是视图模型中的验证。

There are two type of validation logic: business validation (ex. username already exists) which goes into the service layer and UI validation (ex: username is required) which goes into the view model. 有两种类型的验证逻辑:进入服务层的业务验证(例如用户名已存在)和进入视图模型的UI验证(例如:需要用户名)。

Can the view models just contain the POCO classes returned from EF, or should I be using the AutoMapper? 视图模型可以只包含从EF返回的POCO类,还是应该使用AutoMapper?

No EF in view models. 视图模型中没有EF。 View models are POCO classes with simple properties and other complex properties pointing to other view models. 视图模型是具有简单属性的POCO类,其他复杂属性指向其他视图模型。 They could also contain methods in order to properly format the data that will be presented on the particular view those models were intended for. 它们还可以包含方法,以便正确格式化将在这些模型所针对的特定视图上呈现的数据。

If using AutoMapper and DTO, are DTO's clones of the POCO classes? 如果使用AutoMapper和DTO,DTO是POCO类的克隆吗?

Not sure I understand this question. 不确定我理解这个问题。

Would you map in the controller, view model, or in the service layer below? 您会在控制器,视图模型或下面的服务层中进行映射吗?

The controller. 控制器。

To me, the service(s) are objects that contact the repository(s) to get POCO objects back from the EF. 对我来说,服务是与存储库联系以从EF获取POCO对象的对象。 This is where all of my business logic is. 这就是我所有业务逻辑的所在。 Once the service hands an object back to a repository to be persisted to the EF, they are considered valid objects. 一旦服务将对象传递回存储库以持久保存到EF,它们就被视为有效对象。 Is this correct? 这个对吗?

Yes. 是。

Is the domain model the EF model? 域模型是EF模型吗?

If you are using EF Code first approach then yes, otherwise no (if EF pollutes the domain with EF specific attributes and classes). 如果您使用EF Code第一种方法,则为yes,否则为no(如果EF使用EF特定属性和类污染域)。

There is no business logic in them, they are just used to transport objects between the service(s) and the EF. 它们中没有业务逻辑,它们仅用于在服务和EF之间传输对象。 Is this correct? 这个对吗?

Yes. 是。

I am implementing Interfaces here with generic repository. 我在这里使用通用存储库实现接口。 Then you could extend the generic repository for special needs? 那么你可以扩展通用存储库以满足特殊需求吗?

Yes, but don't get too fancy. 是的,但不要太花哨。 Normally Repositories are for CRUD operations. 通常,存储库用于CRUD操作。 It's services that should contain the business logic. 它的服务应该包含业务逻辑。

Is a business object equal to a domain object? 业务对象是否等于域对象?

Yes. 是。

How much logic should a domain object contain? 域对象应包含多少逻辑?

This will depend on the amount of domain logic you have for the particular project you are working and on any existing domain logic you could reuse from older projects you or someone else have worked on. 这取决于您正在使用的特定项目的域逻辑数量,以及您可以从您或其他人工作过的旧项目中重用的任何现有域逻辑。

Dependency Injection - Should I be using this? 依赖注入 - 我应该使用它吗?

Yes, absolutely. 是的,一点没错。

I understand how it works, just don't get the real benefit 我理解它是如何工作的,只是没有得到真正的好处

It provides weaker coupling between the different layers of your application which in turns makes them easier to unit test and reuse in other projects. 它在应用程序的不同层之间提供了较弱的耦合,这使得它们更容易在其他项目中进行单元测试和重用。

I think the community would benefit from some sort of wiki that contained all the best practices with code samples. 我认为社区将受益于某种包含代码示例的所有最佳实践的wiki。

I agree. 我同意。

Is there something like that out there? 那里有类似的东西吗?

I doubt it. 我对此表示怀疑。

BTW - I think StackOverflow needs a little, "Buy Me A Beer" button next to the "Accept Answer" checkbox 顺便说一句 - 我认为StackOverflow需要一个“接受答案”复选框旁边的“买我一个啤酒”按钮

Can't agree more. 不能同意更多。

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

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