views:

122

answers:

4

I have recently joined a company that using typed datasets as their 'Dto'. I think they are really rubbish and want to change it to something a little more modern and user friendly. So, I am trying to update the code so that the data layer is more generic, i.e. using interfaces etc, the other guy does not know what a Dto is and we are having a slight disagreement about how it should be done.

Without trying to sway people to my way of thinking, I would like to get impartial answers from you people as to what layers the Dto can be present in. All layers; DAL, BL and Presentation or a small sub set within these layers only.

Also, whether IList objects should or should not be present in the DAL.

Thanks.

+1  A: 

One thing is DataSets are poor to achieve interoperability. Even typed datasets are also not so compatible when it comes to consuming typed datasets from a non .net client. Refer this link. If you have to achieve interoperability then fight hard for DTOs otherwise try to make your team understand DTOs over a period of time because datasets are not so bad after all.

On part of interfaces, yes you should be exposing interfaces. For example - If you are returning List<T> from DAL, instead you should return IList<T>. Some people go to extent of returning only IEnumerable<T> because all you need is capability to enumerate. But then while doing it don't become astronaut architect.

In my applications I have figured out that returning IList<T> instead of List<T> pollutes my code base with codes like this:

//consider personCollection as IList<Person>
(personCollection as List<Person>).ForEach(//Do Something)

So I personally try to maintain a balance between returning interface or concrete object. If you ask me what I am doing right now, then I will tell you I am returning List<T>. I am influenced to not to become astronaut architect.

Pradeep
Thanks Pradeep (and for the edits too).
Monkieboy
There is a reason for not returning a generic list http://msdn.microsoft.com/en-US/library/ms182142(v=VS.80).aspx. It depends if the object returning the List is public or private. As long as your are not exposing the List on a public interface it doesn't matter because only your are consuming it.
Bronumski
Yes, I agree if public means to be consumed by somebody outside your application. Public should not be inferred as access specifier and no meaning of exposing public return type as interface if it is always going to be consumed by some layer in your project which will never see light outside one application.
Pradeep
+1  A: 

I always use DTOs, never DataTable. But I only use them to transfer from BL to DL and the other way around. My presentation layers are often only aware of the business and service layers in case of service oriented.

The benefits I can see to use DTOs rather than datatables:

  • easy refactoring
  • easy diagram production
  • cleaner more readable code, especially in the DAL's unit tests
vc 74
Thanks vc - straight to the point of my question, any opinion of bundling the Dto into an IEnumerable or similar?
Monkieboy
Do you mean for instance allow a GetCustomers method in the DAL returning an IList or IEnumerable of CustomerDTO? If so, I don't see any reason why you should not allow it.
vc 74
Thanks vc - that was exactly what I meant
Monkieboy
+1  A: 

By definition a DTO is a data transfer object, used to (wait for it) transfer data from one layer to another.

DTOs can be used across all layers and I have used them well with web services.

Burt
Thanks Burt, I like the way you 'reveal' what a DTO is :)
Monkieboy
No worries, have a look at domain driven design as DTOs are featured in it. There are some excellent examples out there of how to architect a solution with complex business rules using DDD.
Burt
Here are a couple of links for you http://dddpds.codeplex.com/ http://ncommon.codeplex.com/
Burt
+3  A: 

It really depends on your architecture.

For the most point you should try to code to interfaces then it doesn't really matter what your implementation is. If you return ISomething it could be your SomethingEntity or your SomethingDTO but your consuming code doesn't care less as long as it implements the interface.

You should be returning an IList/ICollection/IEnumerable over a concrete collection or array.

What you should try to do first is separate your code and make it loosely coupled by inserting some interfaces between your layers such as a repository for your DataAccess layer. Your repository then returns your entities encapsulated by an interface. This will make your code more testable and allow you to mock more easily. Once you have your tests in place you can then start to change the implementations with less risk.

If you do start to use interfaces I would suggest integrating an IoC such as Windsor sooner rather than later. If you do it from the get go it will make things easier later on.

Bronumski
This is more of the route I was looking to go down, especially the IoC and dependency injection techniques, and in fact this is what I was trying to explain the other developer in the office.
Monkieboy