Created
June 27, 2011 20:14
-
-
Save marcofranssen/1049724 to your computer and use it in GitHub Desktop.
Windsor Dependency Injection MVC 3 Example http://marcofranssen.nl/2011/06/27/injecting-dependencies-in-mvc3-with-windsor/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Castle.Windsor; | |
using Castle.Windsor.Installer; | |
using Website.Infrastructure; | |
namespace Website | |
{ | |
// Note: For instructions on enabling IIS6 or IIS7 classic mode, | |
// visit http://go.microsoft.com/?LinkId=9394801 | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
routes.MapRoute( | |
"Default", // Route name | |
"{controller}/{action}/{id}", // URL with parameters | |
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults | |
); | |
} | |
private void BootstrapContainer() | |
{ | |
Container = new WindsorContainer() | |
.Install(FromAssembly.This()); | |
var controllerFactory = new WindsorControllerFactory(Container.Kernel); | |
ControllerBuilder.Current.SetControllerFactory(controllerFactory); | |
} | |
public IWindsorContainer Container { get; private set; } | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
RegisterGlobalFilters(GlobalFilters.Filters); | |
RegisterRoutes(RouteTable.Routes); | |
BootstrapContainer(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Web.Mvc; | |
using Website.Repositories; | |
namespace Website.Controllers | |
{ | |
public class ProductController : Controller | |
{ | |
private readonly IProductRepository _repository; | |
public ProductController(IProductRepository repository) | |
{ | |
_repository = repository; | |
} | |
public ActionResult Index() | |
{ | |
var products = _repository.All(); | |
return View(products); | |
} | |
public ActionResult Details(int id) | |
{ | |
var product = _repository.GetById(id); | |
return View(product); | |
} | |
//Other members left for convenience...... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Website.Infrastructure; | |
using Website.Models; | |
namespace Website.Repositories | |
{ | |
public class ProductRepository : IProductRepository, IDisposable | |
{ | |
private WebsiteReadModelContext _context = new WebsiteReadModelContext(); | |
public IEnumerable<Product> All() | |
{ | |
_context.Products.Select(); | |
} | |
public Product GetById(int Id) | |
{ | |
_context.Products.SingleOrDefault(p => p.Id == id); | |
} | |
//Other members left for convenience...... | |
public void Dispose() | |
{ | |
Dispose(true); | |
GC.SuppressFinalize(this); | |
} | |
protected virtual void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
if (_context != null) | |
{ | |
_context.Dispose(); | |
_context = null; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Castle.MicroKernel; | |
namespace Website.Infrastructure | |
{ | |
public class WindsorControllerFactory : DefaultControllerFactory | |
{ | |
private readonly IKernel kernel; | |
public WindsorControllerFactory(IKernel kernel) | |
{ | |
this.kernel = kernel; | |
} | |
public override void ReleaseController(IController controller) | |
{ | |
kernel.ReleaseComponent(controller); | |
} | |
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) | |
{ | |
if (controllerType == null) | |
{ | |
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path)); | |
} | |
return (IController)kernel.Resolve(controllerType); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Web.Mvc; | |
using Castle.MicroKernel.Registration; | |
using Castle.MicroKernel.SubSystems.Configuration; | |
using Castle.Windsor; | |
using Website.Services; | |
using Website.Controllers; | |
using Website.Repositories; | |
namespace Website.Infrastructure | |
{ | |
public class WindsorInstaller : IWindsorInstaller | |
{ | |
public void Install(IWindsorContainer container, IConfigurationStore store) | |
{ | |
container.Register(FindControllers().Configure(c => c.LifeStyle.Transient)); | |
container.Register(Component.For<IProductRepository>().ImplementedBy<ProductRepository>().LifeStyle.Transient); | |
container.Register(Component.For<ICustomerRepository>().ImplementedBy<CustomerRepository>().LifeStyle.Transient); | |
container.Register(Component.For<ISomeWebService>().ImplementedBy<SomeWebServiceClient>().LifeStyle.Transient); | |
} | |
private BasedOnDescriptor FindControllers() | |
{ | |
return AllTypes.FromThisAssembly() | |
.BasedOn<IController>() | |
.If(Component.IsInSameNamespaceAs<HomeController>()) | |
.If(t => t.Name.EndsWith("Controller")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment