您的位置:首页 > 编程语言 > ASP

转:Setting Up IoC in ASP.NET MVC using Castle Windsor

2012-03-09 17:29 716 查看
from: http://mvcsharp.wordpress.com/2010/01/09/setting-up-ioc-in-asp-net-mvc-using-castle-windsor/

Abstract: This post gathers information needed to setting up Inversion of Control (IoC) in an ASP.NET MVC application using Castle Windsor.

Introduction: In previous post I summarized how ASP.NET MVC components work together to process request from user and return result . In that post I mentioned we can customize each step to satisfy our needs so I’m going to show you how you can create a custom Controller Factory to implement IoC in your application. For more information about IoC and why we use it you can take a look at this like :http://www.mikesdotnetting.com/Article/117/Dependency-Injection-and-Inversion-of-Control-with-ASP.NET-MVC .

I learned how to use Windsor to set up IoC by reading the great book of Steven Sanderson “Pro ASP.NET MVC framework”. You can read the section I used for this post here :Pro ASP.NET MVC framework by Steven Sanderson

You can get the latest version of Castle project here : www.castleproject.org/castle/download.html

After download and install Castle Project follow these steps to set up IoC in a very simple MVC project. I use default project template & routing configuration that comes with MVC 2.

1.Right click your References folder and add reference to “Castle.Core.dll”, “Castle.MicroKernel.dll”, “Castle.Windsor.dll”files which are in ”C:\Program Files\CastleProject\Bin\net-2.0 after installing Castle.



2.Next we are going to create a custom Controller Factory class. Add WindsorControllerFactory.cs to the root of your application and write these lines of codes to create a controller factory which uses castle to load the required objects at run time base on configuration section which we set up later. Don’t afraid of XML & Reflection parts in the code. These snippets just say to Windsor Container that where in the Web.config file look for registering components and Reflection part just registers all controllers’types as components. Usually when we use IoC we want to inject an object to an constructor parameter at run time, but in constructor code we just say which interface that object implements but IoC Container loads the needed object base on its configuration. The Interfaces, objects and classes which their constructor injected are components of an IoC container. In this example we register components in two places: We register controllers in the controller factory but register other types using castle section in Web.config file. So let just right the needed Controller Factory which uses the snippets of Steven’s book that I just updated it to be compatible with MVC 2:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using Castle.Core.Resource;
using System.Reflection;
using Castle.Core;
using System.Web.Routing;

namespace Mvc2Application6
{
public class WindsorControllerFactory : DefaultControllerFactory
{
WindsorContainer container;
// The constructor:
// 1. Sets up a new IoC container
// 2. Registers all components specified in web.config
// 3. Registers all controller types as components
public WindsorControllerFactory()
{
// Instantiate a container, taking configuration from web.config
container = new WindsorContainer(
new XmlInterpreter(new ConfigResource("castle"))
);
// Also register all the controller types as transient
var controllerTypes =
from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes)
container.AddComponentWithLifestyle
(t.FullName, t,LifestyleType.Transient);
}

// Constructs the controller instance needed to service each request this part is Updated to be compatible with MVC 2
protected override IController
GetControllerInstance
(RequestContext requestContext, Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
/*
* For MVC 1 use this
protected override IController GetControllerInstance(Type controllerType)
{
return (IController)container.Resolve(controllerType);
}*/
}
}


  

Note : This post works with Castle 1 RC 3. I recently got an email from one of readers that this not works with the latest version which is 2
so for those folks who want to work which the latest version I notify that instead of AddComponentWithLifestyle method they should use the new AddComponentLifeStyle method.

3.To ASP.NET MVC use your new controller factory add this to the application start method of your global.asax.cs file :

1
ControllerBuilder.Current.SetControllerFactory(
new
WindsorControllerFactory());
4. Next we need manipulate our Web.config file in the root of the application: Add a<castle>node directly in the configuration node: <castle><components></components></castle>we edit this shortly. In the configSections node add this:

1
<
section
name
=
"castle"
type
=
"Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor"
/>
5. To work with our new IoC structure you need to construct your application to use it so follow these steps to set up a simple application which use our IoC implementation:
Create an IEbookRepository interface in Model folder:

01
namespace
Mvc2Application6.Models
02
03
{
04
05
public
interface
IEbookRepository
06
07
{
08
09
IEnumerable<Book>ListEbook();
10
11
}
12
13
}
As you see it’s a simple interface with a property that returns a list of Books. Book is a simple class with 3 properties:

01
namespace
Mvc2Application6.Models
02
03
{
04
05
public
class
Book
06
07
{
08
09
public
int
ID {
get
;
set
;}
10
11
public
string
Name {
get
;
set
;}
12
13
public
string
Author {
get
;
set
;}
14
15
}
16
17
}
Create a concrete class which implements IEbookRepository interface (for simplicity we don’t touch DB and an in memory list of books satisfies our example):

01
namespace
Mvc2Application6.Models
02
03
{
04
05
public
class
EBookRepository : IEbookRepository
06
07
{
08
09
public
IEnumerable<Book>ListEbook()
10
11
{
12
13
List<Book>books = 
new
List<Book>();
14
15
books.Add(
new
Book { ID = 1, Author =
"Steven Sanderson"
, Name =
"Pro ASP.NET MVC framewok"
});
16
17
books.Add(
new
Book { ID = 2, Author =
"Stphan Waler"
, Name =
"ASP.NET MVC framework Unleashed"
});
18
19
books.Add(
new
Book { ID = 3, Author =
"Scott Guthrie"
, Name =
"ASP.NET MVC 1.0"
});
20
21
return
books;
22
23
}
24
25
}
26
27
}
28
29
Add a
new
Constructor to your Home Controller:
30
31
private
IEbookRepository _repository ;
32
33
public
HomeController(IEbookRepository repository)
34
35
{
36
37
this
._repository = repository;
38
39
}
Now we need to tell WindsorContainer to inject IEbookRepository with an object of type EBookRepository so back to your Web.config file and edit castle node we created in step 4:

01
<
castle
>
02
03
<
components
>
04
05
<
component
id
=
"EbookRepository"
06
07
service
=
"Mvc2Application6.Models.IEbookRepository,Mvc2Application6"
08
09
type
=
"Mvc2Application6.Models.EBookRepository,Mvc2Application6"
10
11
lifestyle
=
"PerWebRequest"
>
12
13
</
component
>
14
15
</
components
>
16
17
</
castle
>
This says whenever a class has a dependency to a IEbookRepository (in our example through its constructor) IoC container should load an EBookRepository object with whenever a web request comes.

Then we need to register Windsor’s PerRequestLifestyle module in httpModules section :

1
<
add
name
=
"PerRequestLifestyle"
type
=
"Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule,Castle.MicroKernel"
/>
To register Windsor’s PerRequestLifestyle for IIS 7 refer to the original book “APRESS Pro ASP.NET MVC framework”or use the Google book link above.

So that’s it we set up an IoC container for our ASP.NET MVC application.

you can download sample project here.

Summary: We used Castle Windsor to Set up IoC in a very simple MVC application and for this we created a custom controller factory and added some configuration in web.config file. We also edited Application Start Method to tell MVC use our new controller factory instead of DefaultControllerFactory.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: