Challenge Recently I started working on a new project, with a few other team members. This project involves a really good amount of ERD, means there are a bunch of entities involved in the Database, with a lot of relationships in-between.
I am personally very fond of working with ORMs and familiar with Entity Framework 4.1 and nHibernate which was an obvious choice here but other people involved in this project suggested not to use this as they had some bad experiences with “Entity Framework”.
I guess you might have come across same situation like this. So I thought to write down my own ORM using T4, Which was absolutely not the right choice as per the tight deadlines.
So I thought of another solution which involves writing a base manager class which will serve the same purpose.
- Supports stored procedures
- Enables use of “BaseEntity” class which is missing in Entity Framework Database first Approach
- Ability to work with business entities without manually filling business objects from data tables
- Single function is able to fill all the Complex/Navigation properties of a particular entity, up to N Level whether it is “One to One”, “One to Many” or “Many to Many” relationship. And same behavior is required in the case of saving the data
- Ability to perform basic data operations without writing repetitive code
- Ability to use eager loading as well as lazy loading
- Create all the tables in database along with their relationship. Make sure every table has a column “ID” in it as a primary Key
- Create Stored Procs for all the data manipulation tasks. Make sure that the entire stored procs name will have a special naming format “prefix + Table” Name like “Get+Table Name” and “Save+Table Name”
- Create a simple DAL, using plain ADO.NET, that has the ability to execute stored procedures, and return DataTable objects
- Create a base entity class for common properties in all the tables
- Create a class for every entity of database. Make sure that all its properties of entity matches with all the columns of a certain Stored Procs result set. Also all the entity must inherit from the base entity class
- Include “Base Manager” class which has basic data interaction methods. You need to inherit all manager classes from this base manager class. Now you can work with all the business data as objects and not Data-tables
Here is a snap of my base manager class.
Here is how fast and easy to create your manager class. No method required for basic database operations.
If you have complex Entity which has a complex or Navigation properties with different kind of relationship in between, You can specify relationship by “ChildEntityInfo” object. In this case Manager class would look like below code.
Now you can use manager class as a normal manager class. Like below
This obviously isn’t the best solution to this case, and obviously isn’t something ground-breaking either. I just tried a simple solution that helps a lot when it comes to trying to deal with such circumstances. Hope this will be helpful for others as well.