Wednesday, November 17, 2010

Things I wish I knew about how to create an ASP.NET MVC app....

I have a website I am doing as a hobby, and it is an ASP.NET MVC web application. I have started to write the code in March and I have launched the site in July so it is not very small not very large either, it is a normal size web app. :) I am full of plans though what new features to implement and even since July I have done already three new versions so the app is getting bigger and bigger. It has now reached a point that I have started to feel that the application is not well written enough to be maintainable in the long run so I have decided to refactor the whole backend. Here is a list of things I wish I knew when I started the app:
  • In many sample databases and apps (at least in the ones I have seen before starting to do my app) the naming convention for the db table id is TableNameID (ProductID, OrderID...). This naming convention is a piece of crap. It is much better to call all ids just simply Id, and then you can make an interface IHasId and have nice methods that handle objects that implement IHasId. And I tell you to rename table ids is really painful though it is not impossible....
  • LINQ to SQL was basically replaced by Entity Framework, and EF is developed by Microsoft while LINQ to SQL is just in maintenance mode... So if you can switch. I have switched....
  • Dependency Injection is nice and it is nice to use an IoC container. If you want to understand what is this whole stuff I suggest this great presentation from Brand Wilson. I am using now Ninject.
  • A Service layer is great to have. When I have started to do the app I have thought that only large apps need service layer and anyway I have a simple web app, no business logic. But it turns out there is always some business logic and service layer is good also in not so large apps...
  • There is a great starter ASP.NET MVC app made by Rob Conery on codeplex. It is nice to download it and study it a bit and lean form it. The generic CRUD repository that is usable for all enities is especially great. Before I had All, Single, Add and stuff like that for all my entities separately, and this starter app taught me how to have just one generic. :) I love if I can delete code from my app and make my app cleaner with less lines of code. :)
  • Never trust yourself or EF or LINQ to SQL and always check what gets actually executed on the database. I think that these abstractions and writing queries in C# and using navigation properties is great but you can find yourself very very easily doing very ineffective things on the database level (like select N+1, really ineffective queries with tons of joins....). Always monitor what is going on in the database and entity framework profiler is a great tool for that.

No comments:

Post a Comment