David Hayden [MVP C#]

Sponsors

The Lounge

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
MonoRail - Love that Windsor Integration

The Castle Team really needs to show off the Windsor Integration in their MonoRail Getting Started Sample Download because I think it is a really sweet feature. It is in the documentation, but a far more beautiful thing to see in action :)

The fact that MonoRail will automatically inject a service in my controller when I simply add the service as a parameter to the constructor and register the service in my configuration file is a huge deal!

 

public class HomeController : SmartDispatcherController
{
    private IMyService _myService;

    public HomeController(IMyService myService)
    {
        _myService = myService;
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <components>
        <component
            id="MyService"
            service="HelloWorld.Model.IMyService, HelloWorld"
            type="HelloWorld.Model.MyService, HelloWorld" />
    </components>
</configuration>

 

The Web Client Software Factory offers dependency injection, too, but you have to add the service programmatically in the business module or foundation module initializer class:

 

moduleServices.AddNew<MyService,IMyService>();

 

I actually wrote a tutorial on adding services and dependency injection in WCSF:

Registering and Using Business Module Services in the Web Client Software Factory

 

I personally like the configuration file route in MonoRail and hope WCSF has this option in the future, but that is just my preference. Of course, you can add components to Windsor programatically as well.

 

My jaw hit the floor when I realized that the Windsor Integration in MonoRail did setter injection. I tend to prefer constructor injection and perhaps didn't realize Windsor would also check properties. Anyway, MonoRail can automatically inject the service via the property setter as well:

 

public class HomeController : SmartDispatcherController
{
    private IMyService _myService;
    public IMyService MyService
    {
        set { _myService = value; }
    }
}

 

You gotta love that Windsor Integration!

by David Hayden


Posted 03-15-2007 7:09 PM by David Hayden

[Advertisement]

Comments

Interesting finding - 03/15/2007 « Another .NET Blog wrote Interesting finding - 03/15/2007 &laquo; Another .NET Blog
on 03-15-2007 8:17 PM
Christopher Steen wrote Link Listing - March 16, 2007
on 03-16-2007 9:34 PM

ClickOnce Community Resource Kit has found a new home [Via: brian@softinsight.com ] Health Monitoring...

Technical Related Notes » Blog Archive » links for 2007-03-18 wrote Technical Related Notes &raquo; Blog Archive &raquo; links for 2007-03-18
on 08-18-2007 1:05 PM

Pingback from  Technical Related Notes  &raquo; Blog Archive   &raquo; links for 2007-03-18