Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Opinions Wanted: Object pass through vs value assignment

For a product that I am working on in my spare time I looking for feedback from the community on thoughts/ideas/opinions about the pros and cons of how to store DTO values inside a business entity.

Here is the architecture that I am working with:

I have business/domain entities that will be used to hold onto and perform any business/domain logic and these business entities will be populated with a DTO (possible multiple DTO's if the business entity requires it).  When I am building/populating my entity with the given DTO I have one of two choices.

Choice 1) Pass in the DTO and then go through and perform value assignments from the DTO into the entity.

Choice 2) Pass in the DTO and then store that DTO inside the entity and simply have the values of the DTO be accessed via pass throughs.

Choice 3) Is there a choice 3?

 

I have done both ways in the past and each have their own pro's and con's, I am just looking for community feedback as to what way is deemed to be 'best practice'.

 

Thanks for your feedback


Posted 06-06-2007 7:09 AM by Derik Whittaker
Filed under:

[Advertisement]

Comments

Rob Eisenberg wrote re: Opinions Wanted: Object pass through vs value assignment
on 06-06-2007 9:18 AM

Derik,

I would have to go with Choice 1.  Generally, it would be my intent to prevent the Entities from becoming coupled with the DTOs.  I would think about having all of the DTO's interact through a service layer.  If the scenario was significantly complex I would also think about writing some sort of mapping code between DTOs and Entities that would remove the need to write a lot of manual value assignments.

Take it for what its worth :)

Jeff Perrin wrote re: Opinions Wanted: Object pass through vs value assignment
on 06-06-2007 9:27 AM

I'm with Rob. Think about separating the mapping of dto to business objects out into another class if these scenarios start to get more complex. I'll use something like:

new PersonDtoAssembler(person).AssembleDto();

Derik Whittaker wrote re: Opinions Wanted: Object pass through vs value assignment
on 06-06-2007 1:57 PM

Thanks guys for the post.

I was actually leaning towards doing it the way you both suggested.  The only thing I hate about taking this approachin is the mapping of the values back and forth.

Both ways have pros/cons picking one over the other may be personal choice.

joeydotnet wrote re: Opinions Wanted: Object pass through vs value assignment
on 06-07-2007 11:17 AM

I usually lean towards option 1 as well.  Yes, maintaining the mapping layer between DTOs and Entities can get tedious at times, but I think the separation of concerns you gain is usually worth it.  

If your DTOs do nothing more than mirror your domain objects, then that's probably a sign of a problem.  I usually create "screen-specific" DTOs which can vary quite a bit from my actual domain objects.  So a well maintained mapping layer is essential in this case.

And something I experimented with on my current project, was injecting "formatter" (aka, Strategy) classes into my mappers to encapsulate any special logic for calculated fields and changes in data representation.  This makes it super easy to change formatting logic (in a single place) either using decorators or by just changing the existing formatters.  It also makes sure your DTOs are truly "dumb" objects, if that's your goal.  

These little strategy classes can also be re-used if necessary, like an IDateOfBirthFormatter, which can be injected into any mappers that need to populate DTOs that show date of birth values on screens/reports.

As usual, there's always many solutions to the same problem, but for me, adhering to SRP and OCP principles as closely as possible allows me to keep systems much more maintainable and extensible.

Derik Whittaker wrote re: Opinions Wanted: Object pass through vs value assignment
on 06-07-2007 7:20 PM

I am going to try to inject my mapper object fro DTO to entity into the entity at construction time.

This project i am playing around with right now is more for learning/playing then anything else.

I like the formatter idea, may need to use that at some point.  Thanks.

Thanks for the suggestions Joey.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)