Let us suppose we are going to start new project – application that contains some business logic, user interface on ASP.NET, WPF or both of them. We’d like to use ORM or DAL code generator and implement our business logic in .NET classes. There are several fundamental ways how we can express our ideas of business domain:
- Implement business classes on .NET and let ORM generate appropriate database schema
- Create database schema manually and generate .NET classes by code generator
- Use some kind of visual designer, that can generate business classes and database structure or script
What do you prefer to write: “Create Table Persons ( … )” or “public class Person { … }”?
What are Pros and Cons of those ways?
Maybe there are some special situations where one way is better than another?
How to choose optimal way in a particular project?
I am quite familiar with “Code-First” (or “Model-First”) way, but it seems most of ORMs are designed as code generators or mappers, that suppose that I will manually implement both database structure and business classes.
Answers based on expirience and examples of ORM’s are especially welcome.
Edit: Note, the question is not “What should I do first when starting new project?”, but “What should be manually declared / automatically generated, domain classes or database structure?”
I think the appropriate approach to system analysis and design is to start by modeling your objects and the relations between them first. If you’re creating a library system you should think of the phrases Book, Author, Publisher, ISBN as objects not as database tables or attributes. I believe this is the way it should be. That been said, let’s admit that code generators save way a lot of time, and those require a relational database in order to generate the model and map it to the DB objects. I think this is the major reason why developers tend to start by the D.B. What could prove my point more is that code generators developers is trying hard to reverse the currently implemented operation (i.e. You provide a business model-objects and classes- and the generator creates the DB with the appropriate schema for this).
Edit:
Here’s an example of domain-first generators (ADO.NET Entity Framework itself)
Model First :