I would like to know the best way to transfer the data between layers ( ex: Custom classes, Dataset, DataReader ..).
Let us consider MVP design pattern. I would like to know the best way to transfer data from model layer to presenter layer.
I would like to know the best way to transfer the data between layers ( ex: Custom classes, Dataset, DataReader ..).
Let us consider MVP design pattern. I would like to know the best way to transfer data from model layer to presenter layer.
You can use either
DTO / Business Objects (BO)
They are Plain Old CLR Object (POCO) that usually contains no logic (or maybe minimum to convert a DTO in BO, to override compare or toString methods).
For example if you are creating a new employee, you have a class called EmployeeBO to help transfer data. User Interface captures data from a form, create a BO, and then sends this BO to BLL(Business Logic Layer). This BLL does whatever he thinks is needed and eventually sent BO to Data Access Layer that persist it in DB.
Using BO in BLL Layer helps because you are dealing with real objects here, you can store them in Lists, Dictionaries... you can use LINQ to access it, even eventually have an ORM framework or Microsoft Entity Framework.
DataSets
Similar to previous one. Its advantage it is the main disadvantage. It is highly coupled with db. Don't use it.
DataReader
If you ever consider using DataReaders, go for DataSets :-)
Parameters
You pass all parameters way down from IU layer to BLL and DAL. This is not practical.
The best one are DTOs/Business Objects. Ideally use a DTO to communicate from UI layer to BLL and make BLL to use their own BO objects. If you fail to do so (I did), then your UI is coupled with Data Access Layer making changes in backend complicated. Splitting it between DTOs and BOs give you the advantage of not being coupled. Usually you have a method somewhere that converts a DTO into its BO.
best way to transfer data are the DTO objects.
these objects only contains the instance variables (with setters and getters) as the data which you want to transfer!
there should be no behaviours in this class
for example if you want to pass a student data , do as follows
class StudentBean
{
private String name;
private String age;
public void setName(String n)
{
name=n;
}
public String getName()
{
return name;
}
public void setAge(int n)
{
age=n;
}
public int getAge()
{
return age;
}
}
you can now create the StudentBean class, populate data in its instance variables and then pass this object as parameter to other layer!
I guess that the most convenient way depends on how your presentation layer is implemented (WPF, ASP.NET, ASP.NET MVC, WinForms...).
Having a bunch of DTOs is a typical and usually good implementation, but you can also use anonymous types. I have already provided a similar answer here: Is it good practice to pass TempData and/or ViewData to services?.
The example in that answer shows how business objects are mapped to anonymous types which are then used in the presentation layer. Of course, these anonymous types act as DTOs, with difference that you don't have to explicitly declare them. Additionally, it makes an assumption that you have a model with business objects which represent your application state.