My question is what should I do if I'm using Objects (OOP) and I want to return multiple rows in my object? Logically, my object should always contain only one row from the database right? What should I do if I want to get all or some of Employees if I have an Employee Class. I've searched to many websites but can't seem to find any. Are there any best practices for this? I'm using stored procedures and oop for this.
Thanks.
Loading
Jean PaulPosted Nov 5, 2011, 5:01 AM
IList
If you are using Entity Framework you can call the context to return the IEnumerable
IList
I prefer separating the entity class from the entity retrieval operations. This gives us more flexibility in the long run.
1) It gives decoupling of operations and entity. So we can easily change the db operations class.
For eg: If your current application is supporting Sql Server and another customer wanted Oracle then only the db layer needed to change.
2) It gives decoupling from technology.
For eg: If you are currently using NHibernate and wanted to switch to ADO.NET Entity F/w, you can do that by touching only the db layer.
3) It gives easier Inheritance Chain
For eg: The Employee entity can be extended to a Manager(by inheriting from Employee) adding new properties. So the Manger class which could be in another application will access those unwanted operational methods.
4) Transmission Flexibility
For eg: You can transmit your Employee class over a web service. The consumer of the service do not have to deal with the GetById() methods. Thus a separate DA class makes the Entity clean from the cross cutting functionalities.
5) More Meaning
For eg: To get an employee by Id we have to do the following;
Employee employee = new Employee();
employee.GetById(1); // This will load the same entity object
Employee employee = new Employee();
IList
// Here we will end up with an unwanted employee object
Jirr MeynorPosted Nov 5, 2011, 5:31 AM
I'm looking forward for your article. Thanks again!
Jirr
Jean PaulPosted Nov 5, 2011, 5:20 AM
I tried to search a little and found the following: (refer Persondao)
http://cgeers.com/2009/03/14/data-access-objects-with-the-entity-framework/
In future I would like to write some articles on entity and Data Access Layers.
Then I could give more ellaborate examples.
Regards,
Jean Paul
Jirr MeynorPosted Nov 5, 2011, 5:08 AM
One last thing.. Do you know where to find an example for this? I mean this complex kind of class? Like the one you said. I want to fully see what it looks like in separating the entity class and db operations. If only you can show me an example. With all of these List and DB separated.
Thanks Jean.
Jirr MeynorPosted Nov 5, 2011, 4:48 AM
Is it ok if I'm using my Employee Class with the function GetByID? or do I really need to separate my db operations with my entity class? And also, can you teach me how to use this IList?
Thanks for helping.
Jirr
Jean PaulPosted Nov 5, 2011, 4:30 AM
I can suggest some of the ideas according to my knowledge.
You can use IList
or
Array of Employee Employe[] array;
The best practice would be:
Declare an Employee class with the properties
public class Employee
{
public int Id;
public string Name;
}
Create an EmployeeDA class that operates on the db and returns employee or employeees
Employee GetById(int employeeId)
IList
Regards,
JP