These examples will mainly be from an MVC based solution in C#. The principles however will be very similar for any other application.
Audience
You should have basic knowledge of what Dependency Injection is and understand why we use interfaces. Domain Driven Design Knowledge would also be benefit when creating projects using DI.
Example
Nuget Package Required is ASP.NET MVC 5 Integration for Autofac 4.0.2
Full Example: https://github.com/netferret/AutoFacExample
Bootstrapping AutoFac in an MVC based project.
Full Example: https://github.com/netferret/AutoFacExample
Bootstrapping AutoFac in an MVC based project.
using Autofac;
using Autofac.Integration.Mvc;
using AutoFacExample.Domain.Service.Abstract;
using AutoFacExample.Domain.Service.Concrete;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace AutoFacExample
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var builder = new ContainerBuilder();
builder.RegisterType<ExampleService>().As<IExampleService>();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
builder.RegisterModelBinderProvider();
builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterSource(new ViewRegistrationSource());
builder.RegisterFilterProvider();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
Firstly we will look at the lines in order to hook up our service, we have created a very basic service which will take a string and then return the string with the current date and time. This is specified as ExampleService which implements IExampleService interface.
Its important that ExampleService inherits IExampleService in order for dependency injection to work correctly.
ContainerBuilder will need register all of the Services, Controllers, Views and Models.
Ok so lets break this down
Register our example service
Register all controllers
Register all our models
Register Views for System.Web.Mvc.WebViewPage, System.Web.Mvc.ViewPage, System.Web.Mvc.ViewMasterPage and System.Web.Mvc.ViewUserControl derived types
Ok, so by now you should understand how the bootstrapping works for AutoFac, so next let look at how we hook this up to a standard controller.
We have a standard controller setup called HomeController, this will resolve as usual, however we want to add some logic from our Example Service
First we need to inject our Example Service into the controller which will automatically resolve using AutoFac. As you look at the pattern you will see why this approach is so effective as you can mix & match services and switch out functionality based on your needs.
ok so now we have setup our service, so how do we know its hooked and how can we use it?
Well this is pretty straight forward as well, so usually we would just return ActionResult and return a View Model, but for the sake of this example lets keep it really simple.
The following will output a string with the current date and time. Everytime its refeshed the date and time should update.
The result should then be something very similar to this.
Its important that ExampleService inherits IExampleService in order for dependency injection to work correctly.
ContainerBuilder will need register all of the Services, Controllers, Views and Models.
Ok so lets break this down
Register our example service
builder.RegisterType<ExampleService>().As<IExampleService>();
Register all controllers
builder.RegisterControllers(typeof(MvcApplication).Assembly);
Register all our models
builder.RegisterModelBinders(typeof(MvcApplication).Assembly); builder.RegisterModelBinderProvider();
Register Views for System.Web.Mvc.WebViewPage, System.Web.Mvc.ViewPage, System.Web.Mvc.ViewMasterPage and System.Web.Mvc.ViewUserControl derived types
builder.RegisterSource(new ViewRegistrationSource());
Ok, so by now you should understand how the bootstrapping works for AutoFac, so next let look at how we hook this up to a standard controller.
We have a standard controller setup called HomeController, this will resolve as usual, however we want to add some logic from our Example Service
First we need to inject our Example Service into the controller which will automatically resolve using AutoFac. As you look at the pattern you will see why this approach is so effective as you can mix & match services and switch out functionality based on your needs.
private IExampleService _exampleService;
public HomeController(IExampleService exampleService)
{
_exampleService = exampleService;
}
ok so now we have setup our service, so how do we know its hooked and how can we use it?
Well this is pretty straight forward as well, so usually we would just return ActionResult and return a View Model, but for the sake of this example lets keep it really simple.
The following will output a string with the current date and time. Everytime its refeshed the date and time should update.
public string Index()
{
var result = _exampleService.GetMessage("Welcome to our first example using AutoFac.");
return result;
}
The result should then be something very similar to this.
No comments:
Post a Comment