代码优先还是数据库优先,如何选择? [英] Code-First or Database-First, how to choose?

查看:397
本文介绍了代码优先还是数据库优先,如何选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我们要开始新的项目 - 应用程序包含一些业务逻辑,ASP.NET,WPF或两者的用户界面。我们想使用ORM或DAL代码生成器,并在.NET类中实现我们的业务逻辑。有几种基本方法可以表达我们的业务领域的想法:




  • 在.NET上实现业务类,并让ORM生成适当的数据库模式

  • 手动创建数据库模式并通过代码生成器生成.NET类

  • 使用某种可视化设计器,可以生成业务类和数据库结构,



您最喜欢写什么:Create Table Persons(...)或public class Person {... }?

这些方法的优点和缺点是什么?

也许有一些特殊情况下,一种方法比另一种方法更好?

如何在特定项目中选择最佳方式?



我非常熟悉Code-First(或Model-First)方式, ORM被设计为代码生成器或映射器,假设我将手动实现数据库结构和业务类。



基于过期的答案和ORM的示例特别受欢迎。



编辑:请注意,问题不是在启动新项目时应该首先做什么? /自动生成,域类或数据库结构?

解决方案

我认为系统分析和设计的适当方法是建模你的对象和他们之间的关系首先。如果你正在创建一个库系统,你应该把Book,Author,Publisher,ISBN看成是对象而不是数据库表或属性。我相信这是应该的方式。有人说,让我们承认代码生成器节省了大量的时间,那些需要一个关系数据库,以便生成模型并将其映射到DB对象。我认为这是开发商倾向于由D.B.开始的主要原因。可以证明我的观点更多的是,代码生成器开发人员正在努力扭转当前实现的操作(即,您提供一个业务模型 - 对象和类,并且生成器为此创建具有适当模式的数据库)。



编辑

以下是域首创建器(ADO.NET Entity Framework本身)
模型第一


Visual Studio 2010必须能够生成DDL并创建数据库来存储实体数据模型。开发人员完全控制整个过程,能够定制DDL,或选择他想要的数据库,或微调映射过程。



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 :

Visual Studio 2010 has to ability to generate a DDL and create a database to store the entity data model. The developer has complete control over the entire process being able to customize the DDL, or to select the database he desires, or fine tune the mapping process.

这篇关于代码优先还是数据库优先,如何选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆