Code-First or Database-First, how to choose?

后端 未结 11 1205
盖世英雄少女心
盖世英雄少女心 2020-12-23 16:34

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 DA

相关标签:
11条回答
  • 2020-12-23 17:06

    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.

    0 讨论(0)
  • 2020-12-23 17:07

    There are 3 important aspects that need to be considered when developing a database application...

    1. User Experience
    2. Data Quality
    3. Cost to maintain the first two (User Experience, and Data Quality)

    I believe the priority of these three items are expressed in the order they are presented, meaning the highest priority is User Experience, the second highest priority is data quality, and the third is the cost to do so. Of course these can be debated, but the notion of code first or database first is relative to the third priority - the cost. Whatever the choice is - code first or database first, ensure the first two priorities are fulfilled...

    0 讨论(0)
  • 2020-12-23 17:08

    Domain modelling vs relational modelling

    There are several fundamental ways how we can express our ideas of business domain

    I think it is important to first establish that the domain is independent of concrete technologies and/or programming paradigms (e.g. OO, FP, relational). This answer will assume you've already separately defined your domain, e.g. using DDD practices, and now wish to use a relational database to store it.

    Relational model strengths

    The relational model was invented, among other reasons, to do away with the many problems that were caused by previous models, including the networked model (which includes OO models), the hierarchical model (which includes XML/JSON models), or simple key value stores. It has a lot of strengths over all alternatives, which has made it so popular for decades.

    In my opinion, these strengths indicate that you should design your database model inside of the database, which has been made for precisely that purpose. All other models, including your client model, are copies of that original, relational model from within the database. Thus, all other models should be derived from it, not source for it.

    See the relational model, expressed in DDL, as your source of truth

    XSD is the same

    XML/XSD is a similar technology. Your data is expressed in terms of XML, but when two systems communicate with each other through an XML based API, XSD (or e.g. WSDL if you want) is the most appropriate language to specify that communication.

    If you want to to bind to your XML documents using client technology, e.g. JAXB in Java, then you should generate those JAXB classes from the XSD, not vice versa. Why? Because XSD is the source of truth

    I've written about this topic here.

    0 讨论(0)
  • 2020-12-23 17:11

    In my opinion there is no correct answer to this. I guess it mostly boils down to your own personal or your teams preferences. All mentioned approaches (database first, code first, interface first) have their own advantages and disadvantages.

    I'd probably sit down with pen and paper and sketch up the general structure and the main functions of the application before i do anything specific, be it code or database tables. A simple drawing of the basic user interface also helps a lot.

    0 讨论(0)
  • 2020-12-23 17:16

    To answer your edited question (manual db/auto classes or manual classes/db), I'd choose "neither". Autogenerated code of both kinds are to be avoided for a number of reasons, first of all YAGNI. You end up with code you never wrote but are nonetheless responsible for, code you'll never use, and (in my experience) code you'll end up spending more time refactoring than if you'd designed and written it yourself in the first place. And they both keep your focus far away from the most important location - the User.

    0 讨论(0)
  • 2020-12-23 17:16

    Start by not directly thinking about either, rather "model" (preferably on paper) what parts your application will have from the users point of view.

    If you have a clear mental picture of that model, You can divide the parts up in common and specific little elements which you can translate to both object definitions and database tables.

    I find this method to cut database normalization time and effort significantly.

    0 讨论(0)
提交回复
热议问题