I have been learning SimpleInjector for few days and it’s just awesome. I have already written two post about it(see below link) and this post will be third post about it. In this post we are going to learn how we can do Dependency Injection with ASP.NET MVC.
For those who are reading my posts about Simple Injector Followings are two previous post I have written about Simple Injector. I highly recommend to read first two post so that you can be aware about how we can configure SimpleInjector container.
Dependency Injection with Simple Injector
Singleton Instance in Simple Injector
So let’s start with creating a ASP.NET MVC Project from Visual Studio.
There is already nuget package created for ASP.NET MVC Integration available so you can install that via following nuget command.
1.
Install-Package SimpleInjector.Integration.Web.Mvc
01.
namespace
MVCDISimpleInjector.Models
02.
{
03.
public
class
Customer
04.
{
05.
public
int
Id {
get
;
set
; }
06.
public
string FirstName {
get
;
set
; }
07.
public
string LastName {
get
;
set
; }
08.
}
09.
}
Now let’s write a repository interface and repository class for the Customer Model Class.
01.
using System.Collections.Generic;
02.
03.
namespace
MVCDISimpleInjector.Models
04.
{
05.
public
interface
ICustomerRepository
06.
{
07.
List<Customer> GetCustomers();
08.
}
09.
}
Here is a implementation of ICustomerRepository interface.
01.
using System.Collections.Generic;
02.
03.
namespace
MVCDISimpleInjector.Models
04.
{
05.
class
CustomerRepository : ICustomerRepository
06.
{
07.
public
List<Customer> GetCustomers()
08.
{
09.
var
customers =
new
List<Customer>
10.
{
11.
new
Customer{Id=
1
,FirstName =
"Jalpesh"
, LastName =
"Vadgama"
},
12.
new
Customer{Id=
2
,FirstName =
"Vishal"
,LastName =
"Vadgama"
}
13.
};
14.
return
customers;
15.
}
16.
}
17.
}
Here I have only created a static list of Customers as purpose of this post to show dependency injection with ASP.NET MVC. Let’s create a controller for customer via right click on controller folder and add new –> Controller.
01.
using System.Web.Mvc;
02.
using MVCDISimpleInjector.Models;
03.
04.
namespace
MVCDISimpleInjector.Controllers
05.
{
06.
public
class
CustomerController : Controller
07.
{
08.
private
ICustomerRepository _customerRepository;
09.
10.
public
CustomerController(ICustomerRepository customerRepository)
11.
{
12.
_customerRepository = customerRepository;
13.
}
14.
15.
// GET: Customer
16.
public
ActionResult Index()
17.
{
18.
var
customer = _customerRepository.GetCustomers();
19.
return
View(customer);
20.
}
21.
}
22.
}
Here I have used only Index Action result as purpose of this post to show dependency injection with ASP.NET MVC and SimpleInejctor.
Let’s add a new view for customer via right click on View in Index ActionResult in Customer Controller.
Now let’s run this. It will give error
It’s because ASP.NET MVC by default only parameter less constructor and we have already added a parameter in Customer Controller. So it’s time to write a code for that.
I have written a following code in Application_Start event for doing injection for controller.
Let’s add a new view for customer via right click on View in Index ActionResult in Customer Controller.
Now let’s run this. It will give error
It’s because ASP.NET MVC by default only parameter less constructor and we have already added a parameter in Customer Controller. So it’s time to write a code for that.
I have written a following code in Application_Start event for doing injection for controller.
01.
using System.Reflection;
02.
using System.Web.Mvc;
03.
using System.Web.Optimization;
04.
using System.Web.Routing;
05.
using MVCDISimpleInjector.Models;
06.
using SimpleInjector;
07.
using SimpleInjector.Integration.Web.Mvc;
08.
09.
namespace
MVCDISimpleInjector
10.
{
11.
public
class
MvcApplication : System.Web.HttpApplication
12.
{
13.
protected
void
Application_Start()
14.
{
15.
AreaRegistration.RegisterAllAreas();
16.
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
17.
RouteConfig.RegisterRoutes(RouteTable.Routes);
18.
BundleConfig.RegisterBundles(BundleTable.Bundles);
19.
20.
//Code for registering our repository class and DI
21.
var
container =
new
Container();
22.
container.Register<ICustomerRepository, CustomerRepository>();
23.
24.
// This two extension method from integration package
25.
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
26.
DependencyResolver.SetResolver(
27.
new
SimpleInjectorDependencyResolver(container));
28.
}
29.
}
30.
}
Here if you see the code carefully First I have created a object of Simple Injector container and then I have registered Customer Repository classes and interface. So whenever you request ICustomerRespotiroy object it will crate CustomerRepository class object. Then I have used “ResgisterMVCControllers” method which register all controllers in current executing assembly. At last I have set MVC dependency resolver to Simple Injector Dependency Resolver with container as parameter so that it will resolve all dependencies for controller.
Now let's run again and it’s working.
It’s very easy with Simple Injector integration Nuget Pacakge. Hope you like it. Stay tuned for more!
You can find complete source code of this application on github at https://github.com/dotnetjalps/MVCDISimpleInjector
Now let's run again and it’s working.
It’s very easy with Simple Injector integration Nuget Pacakge. Hope you like it. Stay tuned for more!
You can find complete source code of this application on github at https://github.com/dotnetjalps/MVCDISimpleInjector
No comments:
Post a Comment