Code First, do you like it?
-
We are thinking about moving from database first, to code first, with our new projects.
I like Break Before and After Code
-
We are thinking about moving from database first, to code first, with our new projects.
No, I love it. Bearing in mind that my application client base is small-to-mid organizations and so I'm not as concerned with specific database optimizations. And as a dev on a small team I'm just fine with that, because POCOs support my notion that a database is just where I keep my stuff. The primary motivator, on my side, is that they make Abstracting the DAL trivial. If we choose to move to Mongo or Couch at some point, I can just re-implement my IDataContext rather than re-write every single model. I only have 2 real gripes: first is that my preferred TPT hierarchy can result in some very hard to debug queries, and switching from int to GUID indexing can be a giant pain in the rear.
-
No, I love it. Bearing in mind that my application client base is small-to-mid organizations and so I'm not as concerned with specific database optimizations. And as a dev on a small team I'm just fine with that, because POCOs support my notion that a database is just where I keep my stuff. The primary motivator, on my side, is that they make Abstracting the DAL trivial. If we choose to move to Mongo or Couch at some point, I can just re-implement my IDataContext rather than re-write every single model. I only have 2 real gripes: first is that my preferred TPT hierarchy can result in some very hard to debug queries, and switching from int to GUID indexing can be a giant pain in the rear.
Nathan Minier wrote:
I only have 2 real gripes: first is that my preferred TPT hierarchy can result in some very hard to debug queries, and switching from int to GUID indexing can be a giant pain in the rear.
but obviously this is not so great a hindrance, that you don't use code first.
-
Nathan Minier wrote:
I only have 2 real gripes: first is that my preferred TPT hierarchy can result in some very hard to debug queries, and switching from int to GUID indexing can be a giant pain in the rear.
but obviously this is not so great a hindrance, that you don't use code first.
They're gripes, not deal-breakers. I enjoy the ability to abstract the DAL in this way, it just makes sense to me and the flexibility is nice. I forgot to mention earlier, a lot of the specific tweaking can also be separated from the Model using the Fluent API. You can apply all sorts of configurations and settings in the OnModelCreating() function of a DbContext, although I prefer to use a derivative of the EntityTypeConfiguration to tag classes for export, configure the SQL properties, and let MEF figure it out :)
-
Neither. I prefer to hand create both. Call me old school, but I don't trust auto created databases or entity layers. Too many times someone makes a "minor" tweak to one or the other and then forgets to regenerate, then you're hosed.
-
We are thinking about moving from database first, to code first, with our new projects.
It's a step in the right direction. You should not be picking your database early on in the project, that's an architectural decision that could have a lasting impact, and cause lots of headaches. In my experience starting with the DB leads to poorly designed classes. Design/model the problem, then figure out how you want to store it. You should watch this video by Uncle Bob: Robert C. Martin - Clean Architecture on Vimeo[^] Basically, you want to focus on the business model/problem and defer a lot of these big architectual decisions until you get a good understanding of the problem. Eventually you want to get to just a design/modeling phase, where you get the "business owners" and "technical owners" talking in one room, and hashing it out together. No code involved here, just whiteboard and lots of talking.
-
I still prefer to design the database the traditional way, but the migrations that code first uses are pretty cool.
I built a large project with code first using MS MVC (a couple of years ago) and felt that I addressed one of the major problems in programming. One of my basic rules of project development has been YOU BETTER GET YOUR DATABASE DESIGNED FIRST OR YOU WILL REALLY DESTABILIZE THE APP WHEN YOU MAKE CHANGES TO THE DB MIDSTREAM. Sometimes, however, you just don't have that luxury and Code First really helps in that scenario. With code first you focus on your classes and so you can work and think at a truly theoretical level. Changes to classes and new classes migrate to the DB at the push of a button and so there is never any lag between design and implementation when making design changes. That is the high level view. You are free to work and think at a class level. The companion to code first is LINQ. With code first and LINQ you can literally design and built a complex system and never even look at the database. It is a very cool experience. There are some considerations. The key one is test data. When you 'recycle' your DB after class / design changes you often need to migrate test data. When I was working on my code first project, MS had some data migration hooks but I just ended up building a routine to re-populate the test data. I am sure it is better now. I felt Code First was a useful technique when building a system from the ground up.
-
Not really, no. It feels better to start with but when you have a decent size model you basically have no way to visualise it except turning it into a database and then looking at that.
I have seen the many-tentacled monsters manifested from the murky depths of Code First. Epi's with their SAS armor fear to step within the shadow of these Lovecraftian beasts. The Oracles on high, throw ever more power against it, and the leviathan will not be sated. When it takes 109 left joins, 20 sub-selects and 17 decodes to pull a single death record, there is nothing but eye-clawing madness and sound of your own screaming. So... no. I do not like Code First.
-
It's a step in the right direction. You should not be picking your database early on in the project, that's an architectural decision that could have a lasting impact, and cause lots of headaches. In my experience starting with the DB leads to poorly designed classes. Design/model the problem, then figure out how you want to store it. You should watch this video by Uncle Bob: Robert C. Martin - Clean Architecture on Vimeo[^] Basically, you want to focus on the business model/problem and defer a lot of these big architectual decisions until you get a good understanding of the problem. Eventually you want to get to just a design/modeling phase, where you get the "business owners" and "technical owners" talking in one room, and hashing it out together. No code involved here, just whiteboard and lots of talking.
Josh Go wrote:
you want to focus on the business model/problem and defer a lot of these big architectual decisions until you get a good understanding of the problem.
:thumbsup:
-
We are thinking about moving from database first, to code first, with our new projects.
I do not like Code First. Database First encourages one to carefully consider the modelling with considerations for normalization and database performance (which is often a bottleneck in systems). There are also many tools out there for modelling databases which makes visualizing and collaborating on the model easier. Finally, I just don't trust Code First to make the right decision for generating schemas for large or slightly complex models.
-
We are thinking about moving from database first, to code first, with our new projects.
As with most such question, I think the best answer is 'it depends'. I've found code first very useful when building out a smallish self-contained system (an android app with a .net / sql backend), which has about 15 tables and is unlikely to grow. The beauty has been that I can keep coding, adding things to the models as I prototype and get feedback, think of things I've forgotten, and don't have to muck around with sqlscripts. Simply add-migration from the package console and job done. However, once that system goes into production I will revert to a traditional approach. On other bigger, more complex projects, where you have a logical schema with ~200 tables, there is no way I would do that code first.