I have an MVC app that has inline SQL all over the place. I am currently using MySQL and was curious if it would be a good idea to move the inline sql into functions or routines. This way, if it needed to be changed, I would just have to change the routine or function at the database level instead of at the code level which would require a new build. Is there an alternative approach?
1 Answer
It is better to keep your sql statements out of c# code. There are couple of advantages,
- As you mentioned, you don't need a build everytime you change any sql code
- You will get slight performance improvisation
- More maintainability and readability
Also you could try EntityFramework as an alternate, where you don't need to bother about sql statements
2 Comments
Xaisoft
Ok, so my options are using an entity framework or routines? Thanks for the entity framework suggestion. If I change the underlying database and the orm is built upon that, wouldn't that essentialy require a rebuild?
vinodpthmn
If you change the schema of database, for sure you have to refresh EF POCO, and depends upon your change even you might go for refactoring as well. In EF your entities are CLR native objects and you write your business logic more in CLR language C#/VB. Naturally you require a rebuild when you change a c# code. This is a different context and i don't think there is a point in comparing EF related changes with inline sql statements changes.