Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Take a typical Line-of-Business Silverlight applicatioin for example.First,We have a Product table in database,which has fields of ID,Name,Price,etc;

Then, on the server side,we should have a Product Entity class in the Data Access Layer,which has properties of ID,Name,Price,etc;

Then, we should have a Product DTO Class with properties of ID,Name,Price,etc; Then, on the client(Silverlight) side, we have the Product DTO class with properties of ID,Name,Price,etc as Data Contract to communicate with the server;

Then, We should have a Product ViewModel Class,which alse has the properties of ID,Name,Price,etc to binding with the View.

Don't these many layers of similar objects violate the Don't Repeat Yourself principle?

What if the Business Domain Model itself faces frequent change? For example, the customer wants to remove/add new properties to the product, which will cause all layers to change its code.

We introduce an addtional DAL layer in the assumption that the underlying database will be changed,but in the real world, the business model itself changes far more frequently than data access or presentation logic.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Doesn't the so many layers of Domain-like Object violate the Don't Repeat Yourself principle?

Yes, it does. That's why you should have code-generators to create the necessary classes automatically from metadata. You just maintain the metadata in one place and create the classes automatically when necessary. C#'s partial classes can greatly aid such tasks for example, so you have a place for customization. Other patterns can sometimes be useful to avoid unecessary repitition: Dynamic proxies or wrappers for example.

Then, We should have a Product ViewModel Class,which alse has the properties of ID,Name,Price,etc to binding with the View.

Well, in a ViewModel you can always just have your entity and then bind directly to it. Strictly speaking, it violates the Law of Demeter but in such cases that is acceptable. Just expose the entity and do not repeat its fields. Be pragmatic.

Then, on the client(Silverlight) side, we have the Product DTO class with properties of ID,Name,Price,etc as Data Contract to communicate with the server;

You usually have the DTO classes in an assembly that both, the server and the client can reference. So you won't always need to duplicate the classes.

Also: Be careful with the term domain-like object. Domain objects are the pure objects of the business domain and not technical auxiliaries of the others layers.

Why so many layers with domainobject like objects in an application?

Finally, to answer the question the heading: Technical necessities. If there's a business logical field, it simply needs to exist in various forms. If it's going to be persisted there needs to be a structure that can hold it permanently. This structure must be defined in one way or another. Maybe other applications are going to work with that data, too. Then it needs to be independent of already defined structures in your application. Same thing for DTOs. They're build to communicate data between (sub-)systems.

share|improve this answer
    
thanks! Can silverlight client reference the DTO assembly which has been referenced by the server side?They runs on different computers and different type of CLR. –  TomCaps Aug 31 '11 at 10:04
    
Put the DTO classes in a separate assembly and have both, the service and the client reference this assembly. If this is not possible supply metadata to generate the DTOs or use a dynamic approach, e.g. just provide an associative array or a generic DTO for each entity if creating DTO becomes too cumbersome and type safety isn't an issue. –  Falcon Aug 31 '11 at 10:08
    
Can you please recommend me some good code-generators in .Net to generate DTO or ViewModel?thanks! –  TomCaps Sep 1 '11 at 6:51
    
@TomCaps: I usually use NVelocity from Castle Windsor, as the Apache Version isn't in such a good shape. But you can also use T4 from codeplex (a visual studio plugin). However, you've got to write some generation logic yourself, as these are template-systems. You can also try commercial tools like codesmith which has quite a good reputation. –  Falcon Sep 1 '11 at 7:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.