Is Code First right way how to write modern enterprise applications?

Is Code First right way how to write modern enterprise applications?
1

Upvotes

Upvote

 May 31 2021
4 Comments (4 New)
Closed

I would like to write this idea because of I saw examples on projects how to use or not to use Code First. 

What is Code First? In basic code first is focus on high level programming language (i.e. C#). It means we cannot create database tables, relations, queries in some kind of SQL language. Programmer in C# cannnot be focused to SQL and cannot have deep knowledge of it. When we want to run application there is some tool which can translate our C# entities into SQL structures and can create them in database. 

Advance of this was is focus on model in one place - in C# code. Model changes are more dynamic without errors or bugs. It leads to programming is more flexible and dynamic. 

I discused with my colleague on some kind of access how to write code first. 

 

The first example is that we write entities one to one as we want to code database. It is more typical for programmers that start with code first. Their mind is more set to database focus. So they see entities as database objects (tables, relations, ...). This access can tend to very complex model which can be hard to hand. We call this approach as Database Code First because you create entities similarly as database schema in some data layer often. It could be nice and effective when you write small application with not too much complex business logic and with Transaction Script or Table Module business pattern or when you want to code super easy application and it doesn't matter on clean architecture principle (dependency inversion). 

 

The second example is more effective when you have more complex business logic. At first you need to forget there is database or database objects and relations. What you need is see entities and relations in business for which application is built. It needs to know some business knowledge. These entities will be same as database tables but it not must be true and entities can be different or more complex. Mapping to database is last thing you need to solve because you can simulate business processes by mock/fake environment without database and another infrastructure. We call it as Business Code First. This approach tends to domain model and model driven design methodology and leads to clean architecture principle with dependency inversion. 

 

I saw combination there exists business entities in business layer and data entities in data layer and some kind of mapping between them. I think it is not necessary because Entity Framework has very complex mapping mechanism. So you don't need data entity and you can decrease complexity of mapping.

 

I want to share this idea because I hear "We use Entity Framework and Code First approach and we create super moder application". Yeah it depends on business logic complexity. In case of small application you can choose Database Code First but in case of super heavy business complexity I tend to use Business Code First to do data layer (or infrastructure) very light and create focus to business first. So I think it is not Code First as Code First :)

Comments
Gold Contributor

Hello!

I like your commitment and I am glad that you wrote, but you must ask the Moderator to place in the right Group according to the content you wrote!

in this group there are ideas about the MTC space and rules!

Yes, that's not a right place for the discussion about Code/Database/Model First

Copper Contributor

I don't see some topic so I didnt know I wrote in bad topic. Please, if there is someone who can move it to right topic, he can do it. 

Community Manager
Status changed to: Closed

I am sorry we do not have a community that would be a natural fit for this idea. I also feel like this is more of a conversational piece than an idea. As a result you may get further traction by discussing it one of our developer communities.