WebAPI with ninject and code first — Part 1
Quick blog on WebAPI on .NET framework with correct approach.
There are many blogs and tutorials talk about how to do WebAPI and dependency injection, but the key problem for these tutorials and blogs do not give real world implementation example.
However, you might ask my code functionality works well, why do I need to follow these best practice or coding pattern. The answer to that is if you are building a small project, and yes this might work well. However, if you are working on project a few hundred thousand lines then you need proper structure to ensure your code is maintainable. And this is called Software Engineering.
Entity framework code first approach
This enables you to create your database structure using class files. In old days you need to create these classes manual or using tools. Entity framework helps you to automate the process. When you have database changes, just trigger migration command and you will get the delta generated in .cs file. And you can trigger update database to push the database changes to your database.
How to do it ?
- Use nuget to add ‘EntityFramework’ in your package.
- Type Add-migration “init” in package manager console, this will create migration class file. If you cannot see package manager console, go to [View] → [Other windows] → [Package Manager Console] to activate it
- Type Update-database to push the changes to database, database structure will be updated based on the migration class file generated
Next, we will talk about dependency injection.
