问题描述
我已经成功地与LINQ2SQL和LINQ的DTO(由LINQ2SQL创建的类)......
i have been successfully working with linq2sql and the linq DTOs (the classes that are created by linq2sql) ....
我很困惑,我有任务更新的旧应用程序,我可以看到我的DTO将用于它们应该如何....运送日期
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
我使用存储库模式,所以我通过LINQ2SQL DTOS从储备库将数据传递到服务......一旦我在服务层(这基本上是我的业务逻辑),那么我需要传阅类对象。
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 ..
这些类对象是basicaly镜像(或多或少)的DTOS的 - 有一些变化在一些地方,但通常是相同的。
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..
于是又回到了这个问题在手! - 是只使用传输数据DTOS从存储库服务层这一好的做法......一旦在服务层(业务逻辑)我应该,但我所有的DTOS映射到那里类对象计数器部分(当然使用automapper! !)
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!!)
我的另一种方法是继续使用像类对象的DTOS并通过他们周围从方法到方法和返回类型等,但我觉得这是不好的做法而我一直转圈圈想知道我应该申请哪种方法?
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?
任何帮助非常感谢
感谢
推荐答案
下面是我的意见:
当使用任何非繁琐的程序处理。使用您LINQ2SQL对象作为你的域模型是一个非常糟糕的主意。我看到LINQ2SQL作为一个ORM,仅此而已。数据库(其中LINQ2SQL有直接对应关系来)是数据的正常化。类(在OOAD感)的行为(没有数据)的正常化。
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).
[这些类对象是basicaly镜像] ...
与LINQ2SQL构建应用程序时,我遇到了这一点。让我们面对现实....商业应用最行是荣耀的CRUD应用程序。因此,它是不出来,你的应用程序的实体有很大比例会直接对应到数据库表的问题。 我不想直接绑定到DTO的生成的,但在同一时间,我不希望在我的应用程序重复散落类。
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".
可以说我有一个<$ C。 $ C> PersonDto 与属性(DTO站立数据传输对象)名字,姓氏,年龄
(这直接涉及到数据库列)。
Lets say I have a PersonDto
(Dto standing for Data Transfer Object) with properties of FirstName, LastName, Age
(which relate directly to database columns).
我创建了一个 IPerson
接口,并有我的PersonDto实现它。
I created an IPerson
interface and had my PersonDto implement it.
[Table(Name="Persons")]
internal class PersonDto : IPerson
{
....
}
和我的库法将采取并检索 IPerson
相对于LINQ2SQL类。
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);
这方法已经工作对我很好。每当我觉得我需要从DTO偏离,我能做的,因为代表相对于DTO我对象的接口,所以很容易
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.
有一般指示:
。通过手工建立你的DTO的......我知道这听起来很疯狂,但你会发现,它的作品真的很好用自上而下,测试驱动开发方法。您的DTO的(LINQ2SQL)对象将是非常轻,将开放给设计师的.dbml之外的变化。
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.
请您DTO的和DataContext的内部。没有理由你DTO的要公开曝光(因为你有你的存储库和域对象的公共接口)。这样做会迫使你的域模型和数据访问之间的逻辑分离。
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).
希望这有助于...
这篇关于的DTO(LINQ2SQL)和类对象之间的混乱!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!