3

I have a relational database with more than 50 tables. They have more than one type of relationship, one to one, more than one.

Will code first or database first be more effective in the new MVC project I will create based on this database?

According to the best practice logic, which one is preferred in such a large (2 billion+ data in total) place?

Regards..

  • 1
    "or database first" - but you already have a database? Regardless, we can't possibly know which way will be more effective for you in this project. We don't even know what you mean by "effective". – Sergio Tulentsev Jun 09 '22 at 11:46
  • 1
    The database is ready on the server and the system is running now. What I want to ask is, actually, code first is a new technology compared to database first, and it has advantages, but it puts the center into practice. database first takes it to the old but central database. How does the decision-making process work here? –  Jun 09 '22 at 11:55
  • 1
    The best practice: An application based on the MVC pattern should not be developed around a database as its central point. Instead, the database should be handled "as a detail". Watch [_Architecture the Lost Years_ by Robert Martin](https://www.youtube.com/watch?v=WpkDN78P884) and [_Crafted Design_ by Sandro Mancuso](https://vimeo.com/107963074). – PajuranCodes Jun 09 '22 at 17:23
  • This is an existing 2 billion row database. I don't know if "code first" is going to cut it here. How does code first migrations handle adding and populating a column in a two billion row table? – Nick.Mc Jun 12 '22 at 00:46

1 Answers1

2

In my opinion, code first should be the better case in general since you are mostly building the software for application, not data. "Code first" means to focus on your problem domain and domain model. Accessing the database is an implementation detail for persisting your application states and should not be mentioned in the early phase of software design.

Similar concepts can be found in Domain-Driven Design and Clean Architecture.


In your case, although the data already exists in the database, the main question could still be: what is your new project for?

Case1: If it's simply for reading the data, such as presentation, then you could consider the data in the database as your data model; which might fit in the concept of "database-first". However, since the data model is the model for your problem, I would still consider it as code first.

Case2: On the other hand, if there are some specific requirements, you should definitely build your domain model first to focus on the requirements and problems of your application rather than the database. Otherwise, you might find your application is stuck by the schema of your database, resulting in complex application logic, depending on how much difference between your problem domain and your data, since your application is not focusing on the problem but on the data. For accessing the database, what you might be looking for is the Data Mapper pattern. Additional to it you could implement the Repository Pattern, which will be designed for the entities in your domain model.


For the efficiency problem you mentioned:
Suppose you are the case 2 mentioned previously. In that case, you can take a look into command query responsibility segregation(CQRS), which isolated the query (the operations for reading data) from the command (the operations that change the system state). Separating the responsibility of reading/writing to your system could improve the efficiency of the presentation in your application.


In conclusion, no database-driven design if you are not implementing a read-only application.

Nightlord
  • 56
  • 5
  • _mostly building the software for application, not data_ The two go hand in hand. I imagine someone must feel that those 2 billion records have some level of importance. – Nick.Mc Jun 12 '22 at 00:47
  • Of course the data is important, no matter if it's 2 billion or 2 thousand. But I'll consider they are important to the application, maybe for the client, not to the design. The importance of data to the model relates to the context, which is not specified in the questions. The 2 billion records may come from an old version system or other system which will be working simultaneously in the future. In this case, you don't want to make your design of the domain model be messed up with the old application without good reason. – Nightlord Jun 12 '22 at 11:37
  • Again, it's depending on the context. If the application is for presentation only, I agree it is good to consider database first, as long as the data matches the read model. Otherwise, I won't concern about how data are stored in the database, what the schema is, or even which database is using, while designing the domain model. – Nightlord Jun 12 '22 at 11:44