I had this idea and my first reaction after having it was “That’s a great idea, but why have I never seen/heard it talked about before??” So I’m hoping you can tell me if there is some framework out there that already does this, or if there is some reason why I should avoid it.
The idea is to create a base business object class with methods to dynamically write CRUD sql queries based on the name of the derived class and its properties (or aliases specified in attributes).
My thinking is that once I do this I could just create a new class like so:
class Customer: BusinessObjectBase
{
int Id {get;set;}
string Name {get;set;}
string Phone {get;set;}
}
and my Customer class, having access to BusinessObjectBase’s CRUD methods, would be done.
Somethings to keep in mind when answering this: For the sake of this question I’m highly interested in the design goals of decreased development time and low maintenance, and security is about a 3 on a scale of 1 to 10. In other words, I’m not interested in hearing answers telling me I should be using stored procedures to access data because they are more secure, or anything along those lines.
I know the users here are pretty liberal with the close votes when it comes to overly broad questions… so just to be safe let me reiterate the question, which is not too broad:
Are there any existing frameworks to do this or is there any compelling reason why I should not?
It has been done before; many times, many ways. The basic concept your thinking of is called ‘model first’ development. You can do model first in Entity Framework, NHibernate, Subsonic and many other ORM style-frameworks. Another incarnation of this line of thinking is LINQ-to-SQL where LINQ knows what queries to generate, whether or not you have CRUD stored procs.
Before ORM’s were mainstream we use code files (codified in T4 style templates these days) to write out what your describing. The whole idea was based on ‘convention’ meaning if a field is named ID then it must be the clustered-PK and so on.
Disclaimer
With that said, there is a down side to this line of thinking. In your typical web app (or insert your app style here) you have three layers to work with— client, middle, and DB. With model/code-first development you are effectively ignoring one of the three layers. Modern hardware has gotten quick enough that you can do this for most sites. When your site grows in volume & complexity, however, you will often find yourself working around this auto-crud setup for your most complex/frequently-used scenarios.
Anyway, hope that helps.