Confusion between DTOs (linq2sql) and Class objects!(DTO (linq2sql) 和 Class 对象之间的混淆!)
问题描述
我已经成功地使用了 linq2sql 和 linq DTO(由 linq2sql 创建的类)....
我很困惑,我有更新旧应用程序的任务,我可以看到我的 DTO 将如何使用它们......传输日期
我正在使用存储库模式,所以我通过 linq2sql dtos 将数据从存储库传递到服务......一旦我进入服务层(这基本上是我的业务逻辑)然后我需要传递类对象..
这些类对象基本上是 dtos 的镜像(或多或少) - 在某些地方有一些变化,但大体相同..
那么回到手头的问题!-- 仅使用 dtos 将数据从存储库传输到服务层是一种很好的做法吗……一旦进入服务层(业务逻辑),我应该将我所有的 dtos 映射到那里的类对象计数器部分(当然使用 automapper!!)
我的另一种选择是继续使用 DTOS 之类的类对象,并将它们从一个方法传递到另一个方法,并作为返回类型等传递,但我觉得这是不好的做法,我一直在绕圈子想知道我应该应用哪种方法?
非常感谢任何帮助
谢谢
以下是我的看法:在处理任何重要的应用程序时.使用 linq2Sql 对象作为域模型是一个非常糟糕的主意.我将 linq2Sql 视为 ORM,仅此而已.数据库(与 linq2Sql 有直接对应关系)是数据的规范化.类(在 OOAD 意义上)是行为(不是数据)的规范化.
<块引用>[这些类对象基本上是镜像]...
我在使用 linq2Sql 构建应用程序时遇到了这个问题.让我们现实一点......大多数业务应用程序都是美化的 CRUD 应用程序.因此,您的应用程序实体的很大一部分将直接对应于数据库表,这并不是不可能的.我不想直接绑定到生成的 DTO,但同时我不想在我的应用程序中散布重复的类.
所以这是我的解决方案:
我编程为接口".
假设我有一个 我创建了一个 我的存储库方法将接收和检索 PersonDto(Dto 代表数据传输对象),其属性为
FirstName、LastName、Age(直接与数据库列相关).>
IPerson
接口并让我的 PersonD 来实现它.IPerson
,而不是 Linq2Sql 类.
这种方法对我来说非常有效.每当我觉得我需要偏离 DTO 时,我可以很容易地这样做,因为接口代表我的对象而不是 DTO.
一些一般性的提示:手动构建您的 DTO...我知道这听起来很疯狂,但是您会发现它与自上而下的测试驱动开发方法非常有效.您的 DTO (linq2Sql) 对象将非常轻巧,并且可以在 .dbml 设计器之外进行更改.
保持您的 DTO 和 DataContext 的内部.您的 dto 没有理由公开公开(假设您有存储库和域对象的公共接口).这样做将强制在您的域模型和数据访问之间进行逻辑分离.
将所有数据访问层放在一个单独的项目中(再次强制这种分离).
将您的接口声明放在一个单独的项目中(这将确保您不会遇到任何循环引用).
希望这有帮助...
i have been successfully working with linq2sql and the linq DTOs (the classes that are created by linq2sql) ....
I am confused, i have the task of updating an old application and i can see that my DTOs will be used how they should be .... to transport date
I am using the repository pattern so i am passing data from the repository to the service via the linq2sql dtos... once i am in the service layer (this is basically my business logic) then I need to pass around class objects ..
these class objects are basicaly a mirror image (more or less) of the dtos - there are some changes in some place but generally the same..
So getting back to the question in hand! -- is this good practice to use dtos only to transport data from repository to service layer ... and once in the service layer(business logic) i should but MAPPING all my dtos to there class object counter parts (of course using automapper!!)
My other alternative is to continue to use the DTOS like class objects and pass them around from method to method and as return types etc but i feel this is bad practice and i keep going round in circles wondering which method i should apply?
Any help really appreciated
thanks
Here is my opinion: When dealing with any non-trival application. Using your linq2Sql objects as your domain model is a really bad idea. I see linq2Sql as an ORM and nothing more. Databases (which linq2Sql has a direct correspondance to) is a normalization of data. Classes (in the OOAD sense) are a normalization of behavior (not data).
[these class objects are basicaly a mirror image]...
I encountered this when building applications with linq2Sql. Lets be realistic....most line of business applications are glorified CRUD applications. So it isn't out of the question that a large percentage of your application's entities will correspond directly to database tables. I didn't want to be bound directly to the DTO's that were generated, but at the same time I didn't want duplicated classes littered across my application.
So here is my solution:
I "programmed to an interface".
Lets say I have a PersonDto
(Dto standing for Data Transfer Object) with properties of FirstName, LastName, Age
(which relate directly to database columns).
I created an IPerson
interface and had my PersonDto implement it.
[Table(Name="Persons")]
internal class PersonDto : IPerson
{
....
}
And my repository method would take in and retrieve IPerson
as opposed to the Linq2Sql class.
IPerson somePerson = _repository.Get(someGuid);
somePerson.FirstName = "SomeName";
_repository.Save(somePerson);
This approach has worked really well for me. Whenever I feel I need to deviate from the DTO, I can do so fairly easily because of the interface representing my object as opposed to the DTO.
Some general pointers: Build your DTO's by hand...I know it sounds crazy, but you'll find that it works really well with a top down, test driven development approach. Your DTO's (linq2Sql) objects will be extremely light and will be open to changes outside of the .dbml designer.
Keep your DTO's and DataContext's internal. There is no reason for your dto's to be exposed publicly (given that you have public interfaces for you repositories and domain objects). Doing this will force a logical separation between your domain model and data access.
Put all of your data access layer in a separate project (again to enforce this separation).
Put your interface declarations in a separate project (this will ensure you don't run into any circular references).
Hope this helps...
这篇关于DTO (linq2sql) 和 Class 对象之间的混淆!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:DTO (linq2sql) 和 Class 对象之间的混淆!
基础教程推荐
- 当键值未知时反序列化 JSON 2022-01-01
- 创建属性设置器委托 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01