开发者

Suggest your thoughts on this design

I am working on a business layer functionality.

There are two layers :- 1) Entity Data Model Layer which is the logical layer

This logical layer maps to a storage layer.

There are different classes in each of this layer.

The data storage layer has subject, test and result as objects The logical layer has entity as objects.

I am writing a code which accepts an object of logical layer and converts it to storage layer objects. It does so by reading the schema of the logical layer.

The schema of the logical layer is stored in an xml file and it has attributes which map to physical layer.

My code will interpret this schema and convert to appropriate physical layer objects.

The class which I have written is as follows :-

DataModelToStorageTranslator
{
 IStorageLayerObject TranslateToStorageLayer(ObjectOfLogicalLayer);
}

The different classes of the storage layer are derived from 开发者_运维知识库the IStorageLayerObject.

The client will check the type of object at runtime.

Is there a better way to achieve the same ?

My implementation is in C#

Thanks, Vivek


Unless there is a very good reason for using XML, I would avoid it. If you have a fixed number of objects that will need conversion from logical layer to storage layer I would suggest to create a DataModelToStorageTranslator facade like this:

DataModelToStorageTranslator{
  SubjectStore translate(SubjectLogical subject);
  TestStore translate(TestLogical test);
  ResultStore translate(ResultLogical result);
}

That would give you more type safety as you will not need to check object types and cast. You will need to extend your interface anytime you want to add new objects (which I think is a good way of doing it for smaller projects).


To just copy Properties from the business layer to the DTO you can have a simple reflection based copy algorithm. There are ready-to-go implementation. One suitable should be AutoMapper. There are similar tools around.

Do not map over XML, that is just time consuming and will slow the reactioness of your application.

If you worry about type safety and speed issues in the conversion process consider using automatically created code using T4.

Just my thoughts.


I did something similar to this but used extensions methods on the objects, i just found that way a but more fluent and easy to follow, so i'd have calls like this.

ClientEntity.ToSaveableEntity()

which would return an SaveableEntity object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜