Dependency Injection – ASP.NET
By taking a closer look on a Dependency Injection (DI) , we can understand it is a software design pattern which allows us to develop a loosely coupled code and it also decreases the code dependency of components of the tightly coupled software. It is also known as Inversion-of-Control, which helps us to do the unit testing more convenient.
Advantages of using Dependency Injection are:
1. Increases code re usability
.2. Reduces class coupling
3. Improves code maintainability
4. Improves application testing
Basic CRUD Operations & Scaffold Templates
- File -> New Project-> Visual C# -> ASP.NET Web Application-> OK
- Select MVC, Change Authentication = No Authentication->OK
- Create a two table in the new database in Sql Server as Department and Employee
- Right Click on Project ->Add New Item-> Under Visual C#, Data Select ADO.NET Entity Data Model, Name = DeccansoftEntity -> Add
- Select EFDesigner from database -> Next
- New Connection -> Select Server Name = ADMIN-PC\SQLEXPRESS2017, database name = Deccansoft -> OK -> Next -> Select Entity Framework 6.x -> Next
- Expand tables and Select all the tables (Department and Employee)which we have created on the step 3 -> Finish
- Solution Explorer -> Right click on Controller -> New -> Controller -> MVC 5 Controller with Views using Entity Framework ASP.NET MVC 5.X CRUD Operations
- Model Class=Employee, DataContextClass=EFDemoDbEntities,Contro
llerName=EmployeesController ->Add 10. Build and run the application
Writing Generic Class/Repository
1. To project add the following Generic class in the Repository folder
2. Create a new folder, Name=BO.
3. Under BO folder, add the Employee BO class to the project.
4. Under BO Folder, add the Department BO class to the project.
4.1 Under BO Folder, add Context Helper class to the project
Dependency Injection and Custom Controller Factory
4.2 Add-> Global.asax in below code
4.3 To separate the creation of the dependencies from the main class we use Dependency injection technique, so we typically inject an object which is needed by a class through a constructor.
5. Edit the code of Employees Controller as below-
5.1 Create Index Action Result in a Controller as below
5.2 Create Index Action Result method view(Index-> Add View-> View name=Index, Template=List, Model class = Employee, Data Context class=DeccansoftEntities -> Add)
5.3 Display Index View in Browser
6. Edit the code of Employees Controller, write a Create Action method as below.
6.1 Create Action Result method View (Create->Add View->View name=Create, Template=Create,Model class=Employee,Data Context= DeccansoftEntities -> Add)
6.2 Create Index Action Result in a Controller as below
6.3 Display Create View in Browser
7. Edit the code of Employees Controller, Write a Edit Action method as below.
7.1 Edit Action Result method View (Edit->Add View->View name=Edit, Template=Create,Model class=Employee,Data Context=DeccansoftEntities->Add)
7.2 Create Edit Action Result method in a Controller as below
7.3 Display Edit View in Browser
8. Edit the code of Employees Controller, write a Details Action method as below.
8.1 Details Action Result method View (Create->Add View->View name=Details, Template=Details,Model class=Employee,Data Context=DeccansoftEntities->Add)
8.3 Display Details View in Browser
9. Edit the code of Employees Controller, Write a Delete Action method as below.
9.1 Delete Action Result method View (Create->Add View->View name=Delete, Template=Delete,Model class=Employee, Data Context=DeccansoftEntities->Add)
9.2 Create Delete Action Result in a Controller as below
9.3 Display Details View in Browser
Superb wonderful information given really helpful blog given keep sharing more thanks for it