简体   繁体   English

ASP.NET MVC实体,DTO,业务对象,ViewModels? 我该如何管理?

[英]ASP.NET MVC Entities, DTO, Business objects, ViewModels? How do i manage this?

I'm very busy with the architecture of a new MVC application, but i'm getting very confused about how to manage different type of objects. 我非常忙于新MVC应用程序的架构,但我对如何管理不同类型的对象感到非常困惑。 The confusion is about the relation between entities, business objects and viewmodels. 混淆是关于实体,业务对象和视图模型之间的关系。 I 'm going to describe my confusion with an example: 我将用一个例子描述我的困惑:

I've set up my web application with different projects: MVC frontend, BLL, DAL, Common things etc. 我已经使用不同的项目设置我的Web应用程序:MVC前端,BLL,DAL,常见事物等。

Let's say i have a view with a List of bikes. 假设我有一个自行车列表的视图。 I want to display the bike details like color, size, manufacturer. 我想显示自行车的细节,如颜色,尺寸,制造商。 But in my database, the Bike and Manufacturer are two different tables, so in my Entity Framework context, these are also two different classes. 但在我的数据库中,Bike和Manufacturer是两个不同的表,因此在我的Entity Framework上下文中,这些也是两个不同的类。

So i have these two entities Bike and Manufacturer. 所以我有两个实体自行车和制造商。 But in my business needs, i think they need to be a single object, which i can manipulate or use in business logic. 但在我的业务需求中,我认为他们需要成为一个单独的对象,我可以在业务逻辑中操作或使用它。 Then there is my view, which needs a (View)Model. 然后有我的观点,需要一个(视图)模型。 That should also be a combined ViewModel with properties from different tables. 这也应该是具有来自不同表的属性的组合ViewModel。

How do i handle this? 我该如何处理? Do i need to get the Bike and Manufacturer object from my DAL, and create a business object from it in my BLL, and after doing some business logic, should i create a ViewModel from it in my controller? 我是否需要从我的DAL获取Bike和Manufacturer对象,并在我的BLL中创建一个业务对象,在做了一些业务逻辑之后,我应该在我的控制器中创建一个ViewModel吗? Or does my DAL need to return a combined business object? 或者我的DAL是否需要返回合并的业务对象? Or can i use the entity object as business classes? 或者我可以将实体对象用作业务类吗? Or can i also use my business object as a ViewModel? 或者我还可以将我的业务对象用作ViewModel吗?

I hope that my problem is clear and that anyone can give me a good advise about which object are needed and how, where and when the different types of objects are created, and in which layer this classes should go... 我希望我的问题很明确,任何人都可以给我一个很好的建议,告诉我需要哪个对象以及创建不同类型对象的方式,地点和时间,以及这些类应该在哪个层...

You can have a custom business entities, that will be referenced in your views. 您可以拥有自定义业务实体,这些实体将在您的视图中引用。

In you business implementation you can have some mapper that will map your Entity Framework entities to your custom business entities, you can use Automapper to achieve this. 在您的业务实现中,您可以使用一些映射器将您的Entity Framework实体映射到您的自定义业务实体,您可以使用Automapper来实现此目的。

I hope this will help. 我希望这将有所帮助。

The answer to your question is easy. 你的问题的答案很简单。 There is NO relationship between your different layers of models. 您的不同模型层之间没有任何关系。 They are completely isolated, and do not refer to each other. 它们是完全孤立的,不互相引用。 Not at all. 一点也不。 As such, there is nothing to be confused about. 因此,没有什么可以混淆的。

You have code in different parts of your layer that maps between two layers UI->Business and Business->Data, but that should be the extent of any interaction between them. 您的图层的不同部分中的代码可以在两个UI-> Business和Business-> Data之间进行映射,但这应该是它们之间任何交互的程度。

You should have some common functionality where you'll have all your mapping for your Business and EF entities. 您应该拥有一些常用功能,您将拥有Business和EF实体的所有映射。

and in your implementation you will map just ask your mapper to provide actual entities. 在您的实现中,您将映射只需要求您的映射器提供实际实体。

There should be some common class that will create mappings for you. 应该有一些通用的类可以为你创建映射。

Something like this 像这样的东西

public static void CreateTestMapping() { Mapper.CreateMap<DataAccess.Entities.Test, Business.Entities>() .ForMember(s => s.Col1, d => d.MapFrom(t => t.Colabc)) .ForMember(s => s.Col2, d => d.MapFrom(t => t.ReferenceTable.RefTableCol1)); }

and in your business implementation you'll use this mapping to convert Business.Entities to EF.Entities and visa-versa 在您的业务实现中,您将使用此映射将Business.Entities转换为EF.Entities,反之亦然

Mapper.Map<Business.Entities.Test, EF.Entities.Test>(source, destination);

What i would do is: 我会做的是:

Your DAL returns a List<Bike> and a List<Manufacturer> . 您的DAL返回List<Bike>List<Manufacturer>

Then your Business Layer should manipulate these and return to asp.net MVC an appropriate object. 然后你的业务层应该操纵这些并返回到asp.net MVC一个合适的对象。

For example a List<Manufacturer> which contains for every item a List<Bike> . 例如, List<Manufacturer> ,其中包含List<Bike>每个项目。

Create the appropriate ViewModels and add Controller logic to manipulate them BUT DO NOT do any core business manipulation there, just UI manipulation to suit your views. 创建适当的ViewModel并添加Controller逻辑来操作它们但是不要在那里进行任何核心业务操作,只需要UI操作以适合您的视图。

Also i would recommend NOT to bind your UI with your DAL. 另外,我建议不要将您的UI与DAL绑定。

Keep your UI project referenceing the common libraries + Business layer project. 保持UI项目引用公共库+业务层项目。

Let the business communicate with the DAL. 让业务与DAL进行通信。

IMHO, part of confusion comes from the fact that you "can't" break all links between your projects even if you want for "design" and separation of concern reasons. 恕我直言,混淆的一部分来自于你“不能”打破你的项目之间的所有链接,即使你想要“设计”和分离关注原因。

Well, in fact you can (and may be should) break, but the cost is, at least: lost intelisense and/or marshalling coding. 嗯,实际上你可以(并且可能应该)打破,但成本至少是:失去了知识分子和/或编组编码。

At the end, your projects are linked at least by a convention. 最后,您的项目至少通过约定进行链接。 One project expects a behavior from the other. 一个项目期望来自另一个项目的行为。 If you publish weather data, you expect your DAL to provide weather data and not financial data, even if it should be wise to handle the case. 如果您发布天气数据,您希望您的DAL提供天气数据而不是财务数据,即使应该明智地处理此案例。

At least one project must expose an interface/DTO, and the other must implement this interface. 至少有一个项目必须公开接口/ DTO,另一个项目必须实现此接口。

Usually and humbly, I try to make the business logic independent : business can be built without reference to any other project (PLEASE: note I speak of project not of layer). 通常和谦卑地,我试图使业务逻辑独立:可以在不参考任何其他项目的情况下构建业务(请注意:我说的是项目不是图层)。 So My abstract classes or interfaces are in the business or domain project. 所以我的抽象类或接口在业务或域项目中。 Why: the most probable changes are GUI technology changing or persistence technology changing, so if you want to interact with me here are the contracts (false friend here). 原因:最可能的变化是GUI技术的变化或持久性技术的变化,所以如果你想与我进行互动,这里是合同(这里是假朋友)。

So the web site (or any GUI) project references the business project and the DAL project, the DAL project references the business project. 因此,网站(或任何GUI)项目引用业务项目和DAL项目,DAL项目引用业务项目。

  • The controller get the data from the DAL and provide it to the domain/business logic 控制器从DAL获取数据并将其提供给域/业务逻辑
  • The controller then provide the unattached result to razor, and/or persist the result by the DAL. 然后,控制器将未附着的结果提供给剃刀,和/或通过DAL保持结果。

BUT imho, evil starts when you stop mastering the scope of your context (DbContext, ObjectContext,...). 但是当你停止掌握你的上下文的范围(DbContext,ObjectContext,...)时,邪恶开始了。 Other saying try to avoid providing attached object to Razor. 其他说法试图避免向Razor提供附加物体。

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

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