Oren posted a pattern for converting Domain entities to DTO’s using a delegate. He made a comment in his post: “I did try to make it into a framework, but even I can’t make it more complex than this.”
I started laughing to myself because I actually tried something similar a few weeks ago, and was going to post, but I didn’t cuz I’m fairly new to this blog stuff and I wussed out… Time to be bold I suppose.
The project I am working on now has a large Service Layer on top of Repositories. You can only write so many services with translation before you start to see the commonalities and look for a way to abstract and reuse. (framework)
So, I thought I’d geek out and get carried away with some Fowler P of EEA. Basically, what I wanted to do is *try* and put some standardization to a Service Layer, which returns DTO’s, which are assembled from a Domain Model, which are accessed from Repository. So DTO <-> Service Layer <-> Assembler <-> Repository <-> Domain Model. Sweet. I am going to use the classic Fowler Album and Artist.
The Repository interface:
The abstract Assembler:
And a base Service:
Now that I have some structure down, I think hammering out the implementations should go fast. I have more or less templated how the objects should interact.
The Domain Entities:
The Repository: (With some hacked data in a List)
And of course the Service: (Super easy now)
I went ahead and added another method using Find, but its nice to see all the standard interactions already there. If I need to extend the Service I can as all the methods defined are virtual and the Repository and Assembler are exposed as properties:
So there it is. I am not sure how I quite feel about it yet. I have a side project going and I think Ill try it out and see how it goes. It was certainly fun to do, and I’m definitely interested in your thoughts.
Though I posted all the code, you can find it here if your interested.