Confusion between DTO (linq2sql) and class objects!

I successfully worked with linq2sql and LINQ DTO (classes created by linq2sql) ....

I am confused, I have the task of updating the old application, and I see that my DTOs will be used as they should be .... to transfer the date

I use the repository template, so I transfer the data from the repository to the service via linq2sql dtos ... as soon as I am at the service level (this is basically my business logic), then I need to pass the class objects ..

these class objects are basically a mirror image (more or less) of dtos - in some place there are some changes, but in general they are the same.

So, back to the question in hand! - It is good practice to use dtos only for transferring data from the repository to the service level ... and once at the service level (business logic) I have to, but MAPPING all my dtos, where there are counters of class objects (of course, using automapper !!)

My other alternative is to continue to use DTOS class objects like objects and pass them from method to method and as return types, etc., but I think this is bad practice and I keep turning around, wondering which method I am should apply?

Any help really appreciated

thanks

+6
c # linq-to-sql repository-pattern dto automapper
source share
4 answers

Here is my opinion: When working with any non-sober application. Using your linq2Sql objects as your domain model is a really bad idea. I see linq2Sql as ORM and nothing else. Databases (which linq2Sql has a direct correspondence to) are data normalization. Classes (in the sense of OOAD) are normalization of behavior (not data).

[these class objects are mostly mirror images] ...

I came across this when creating applications using linq2Sql. It will be realistic .... most areas of business applications are renowned CRUD applications. Therefore, it is possible that a large percentage of your applications will correspond to database tables. I did not want to contact directly with the DTOs that were generated, but at the same time I did not want duplicate classes to clog in my application.

So here is my solution:
I "programmed to interface".

Suppose I have PersonDto (Dto standing for a data transfer object) with the FirstName, LastName, Age properties (which refer directly to the database columns).

I created the IPerson interface and used its PersonDto.

 [Table(Name="Persons")] internal class PersonDto : IPerson { .... }
[Table(Name="Persons")] internal class PersonDto : IPerson { .... } 

And my repository method will accept and retrieve IPerson unlike the Linq2Sql class.

 IPerson somePerson = _repository.Get(someGuid); somePerson.FirstName = "SomeName"; _repository.Save(somePerson);
IPerson somePerson = _repository.Get(someGuid); somePerson.FirstName = "SomeName"; _repository.Save(somePerson); 

This approach worked very well for me. Whenever I feel that I need to deviate from the DTO, I can do this quite easily because of the interface representing my object, not the DTO.

Some general pointers: Create your DTO manually ... I know this sounds crazy, but you will find that it works great with a test-based approach based on experience. DTO objects (linq2Sql) will be extremely lightweight and open to change outside of the .dbml constructor.

Keep your DTO and DataContext internal. There is no reason to publicly publish your dto (given that you have public interfaces for your repositories and domain objects). This will result in a logical separation between your domain model and data access.

Put your entire data access level in a separate project (again to ensure this separation).

Place the interface declarations in a separate project (this ensures that you don’t encounter circular links).

Hope this helps ...

+5
source share

I had a similar question on this topic, although my intentions were somewhat different. The recommendation was to use Linq2SQL classes as domain objects and use the partial classes that others talked about. My main problem was the form of these objects (i.e. Property Names) and the accessibility of classes (e.g. private vs protected, for example).

The shape of objects and even accessibility can be resolved using the t4 templates, where Damien Guard put together a T4 template so that you can control the shape of the classes that Linq2Sql will generate for you. You can see it here T4 template for Linq2SQL .

This is the approach I'm going to study and see if it concerns my problems. In addition, if your service level can accept interfaces to method arguments, you can also control access to the service level through interface wrappers around your Linq2SQL DTOs.

Hope this helps.

+3
source share

This is one of the best discussions of the topic I've seen:

http://blog.wekeroad.com/blog/linqtosql-momma-said-knock-you-out/

In the end, communication and cohesion decisions are situational, and you must decide what is best for your situation.

When your application outgrows LinqToSql, how easy will it be to pull out LinqToSql and insert another ORM into it? This is something you should seriously think about.

In general, minimize your business level knowledge of LinqToSql. LinqToSql should be completely hidden from your user interface level (your business layer is a good piece of this screen). It is very easy to go down the wrong architectural path with LinqToSql, and after that it can be very difficult to get back on the right path.

+2
source share

The classes created by the linq2sql constructor are partial classes, so you can extend them and put your business logic directly in them. The idea is that linq is used to save / restore these objects so that you can avoid the type of display you're talking about.

0
source share

All Articles