Skip to content

Instantly share code, notes, and snippets.

@brunomlopes
Created March 5, 2010 01:09
Show Gist options
  • Save brunomlopes/322350 to your computer and use it in GitHub Desktop.
Save brunomlopes/322350 to your computer and use it in GitHub Desktop.
A presenter factory for WebformsMvp using Autofac
using System;
using Autofac;
using Autofac.Integration.Web;
using WebFormsMvp;
using WebFormsMvp.Autofac;
using WebFormsMvp.Binder;
using weDiscuss.Logic.Presenters;
namespace weDiscuss.Web
{
public class Global : System.Web.HttpApplication, IContainerProviderAccessor
{
static IContainerProvider _containerProvider;
protected void Application_Start(object sender, EventArgs e)
{
var builder = new ContainerBuilder();
builder.RegisterPresenters(typeof(ThreadsPresenter).Assembly);
_containerProvider = new ContainerProvider(builder.Build());
PresenterBinder.Factory = new AutofacPresenterFactory(_containerProvider);
}
public IContainerProvider ContainerProvider
{
get { return _containerProvider; }
}
}
}
<configuration>
<system.web>
<httpModules>
<add name="ContainerDisposal" type="Autofac.Integration.Web.ContainerDisposalModule, Autofac.Integration.Web"/>
using System;
using System.Globalization;
using System.Linq;
using Autofac;
using Autofac.Core;
using Autofac.Integration.Web;
using WebFormsMvp.Binder;
namespace WebFormsMvp.Autofac
{
public class AutofacPresenterFactory : IPresenterFactory
{
private readonly IContainerProvider _containerProvider;
public AutofacPresenterFactory(IContainerProvider containerProvider)
{
_containerProvider = containerProvider;
}
public IPresenter Create(Type presenterType, Type viewType, IView viewInstance)
{
if (presenterType == null) throw new ArgumentNullException("presenterType");
if (viewType == null) throw new ArgumentNullException("viewType");
if (viewInstance == null) throw new ArgumentNullException("viewInstance");
var parameter = new TypedParameter(FindViewType(presenterType, viewInstance), viewInstance);
return (IPresenter) _containerProvider.RequestLifetime.Resolve(presenterType, new Parameter[]{parameter});
}
public void Release(IPresenter presenter)
{
// with ContainerDisposalModule set up as an http module and presenters configured as HttpRequestScoped
// Autofac should release them correctly at the end of the request.
return;
}
static Type FindViewType(Type presenterType, IView viewInstance)
{
var genericPresenterInterface = presenterType
.GetInterfaces()
.Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IPresenter<>))
.SingleOrDefault();
if (genericPresenterInterface == null)
{
throw new InvalidOperationException(string.Format(
CultureInfo.InvariantCulture,
"Please, make sure the presenter implements IPresenter<TView>, otherwise AutofacPresenterFactory" +
" can't really figure out the constructor parameters. Oh, and for now, please just implement it once. "+
"The presenter we were trying to create was {0} and the view instance was " +
"of type {1}.",
presenterType.FullName,
viewInstance.GetType().FullName
));
}
return genericPresenterInterface.GetGenericArguments()[0];
}
}
}
using System.Reflection;
using Autofac;
using Autofac.Integration.Web;
namespace WebFormsMvp.Autofac
{
public static class RegistrationExtensions
{
public static void RegisterPresenters(this ContainerBuilder builder, params Assembly[] presenterAssemblies)
{
builder.RegisterAssemblyTypes(presenterAssemblies)
.Where(t => typeof(IPresenter).IsAssignableFrom(t))
.HttpRequestScoped();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment