2011-05-12 3 views
1

Quelle version Autofac dois-je utiliser pour ASP.NET MVC 1.0. En outre, quels exemples sont disponibles pour l'utiliser avec ASP.NET MVC en utilisant le modèle de référentiel?Autofac - Quelle version utiliser pour ASP.NET MVC 1 et des exemples?

public class MyController : BaseController 
{ 
    private readonly IUser _user; 
    private readonly ICompany _company; 

    public MyController(IUser user, ICompany company) 
    { 
     _user = user; 
     _company = company; 
    } 

    public ActionResult Index() 
    { 
     // Actions 
    } 
} 

Répondre

0

J'ai décidé d'utiliser StructureMap pour mon application. Voici ce que je l'ai fait pour le faire fonctionner:

Créer ma propre classe CustomFactory:

public class StructureMapControllerFactory : DefaultControllerFactory 
{ 
    protected override IController GetControllerInstance(Type controllerType) 
    { 
     if (controllerType == null) 
     { 
      return base.GetControllerInstance(controllerType); 
     } 

     try 
     { 
      return ObjectFactory.GetInstance(controllerType) as Controller; 
     } 
     catch (StructureMapException ex) 
     { 
      throw; 
     } 
    } 
} 

Créer une Bootstrapper, en utilisant la convention de StructureMap pour enregistrer toutes les interfaces et sa mise en œuvre pour mon montage. (Je l'ai posté un blog post à ce sujet):

public class StructureMapBootstrapper : IBootstrapper 
{ 
    private static bool hasStarted; 

    public void BootstrapStructureMap() 
    { 
     ObjectFactory.Initialize(x => 
     { 
      x.AddRegistry<FactoryRegistry>(); 
     }); 
    } 

    public static void Restart() 
    { 
     if (hasStarted) 
     { 
      ObjectFactory.ResetDefaults(); 
     } 
     else 
     { 
      Bootstrap(); 
      hasStarted = true; 
     } 
    } 

    public static void Bootstrap() 
    { 
     new StructureMapBootstrapper().BootstrapStructureMap(); 
    } 
} 

public class FactoryRegistry : Registry 
{ 
    public FactoryRegistry() 
    { 
     Scan(s => 
     { 
      s.Assembly("Calendar.Library"); 
      s.TheCallingAssembly(); 
      s.AddAllTypesOf<IController>().NameBy(type => type.Name.Replace("Controller", "").ToLower()); 
      s.WithDefaultConventions(); 
     }); 
    } 
} 

Inscrivez-StructureMap à Global.asax.cs:

protected void Application_Start() 
{ 
    StructureMapBootstrapper.Bootstrap(); 
    ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory()); 
} 
1

Je ne pense pas que vous pouvez l'utiliser du tout.

Selon http://code.google.com/p/autofac/wiki/MvcIntegration seul MVC2 était pris en charge.

+0

Je regardais ce droit page avant posté ma question ici et espérais que Quelqu'un me dirigerait vers la bonne version. Dommage que ce ne soit pas supporté. Qu'en est-il de StructureMap, prend-il en charge ASP.NET MVC 1? Merci. – Saxman

+0

Ne sait pas - jamais utilisé ... –

1

Vous pouvez utiliser Autofac 1.4.1 à 1.4.4 pour ASP.NET MVC1. Je cours 1.4.2. Voici un exemple de programme d'amorçage pour Autofac 1.4.2 (avec quelques extraits supprimés, vous ne savez pas ce qui a changé dans 1.4.4 qui pourrait affecter ce code).

namespace Web 
{ 
    public class PropertyInjectionForAllComponents : Module 
    { 
     protected override void AttachToComponentRegistration (IContainer container, IComponentRegistration registration) 
     { 
      registration.Activating += ActivatingHandler.InjectProperties; 
     } 
    } 

    public static class Bootstrapper 
    { 
     public static IContainerProvider ConfigureAutofac (string serverPath) 
     { 
      var configManager = new ConfigManager (new ConfigurationManagerWrapper()); 

      string environment = configManager.AppSettings ("Environment"); 

      RequireSslAttribute.Enable = configManager.AppSettings ("EnableSSL").IsTrue(); 

      // Autofac IoC configuration see http://www.codeproject.com/KB/architecture/di-with-autofac.aspx for details 
      // ExternallyOwned() - Use on objects that are disposable which you do not want disposed such as Console.Out as an implementation TextWriter. 
      // OnActivating() - Allows property injection 
      // SingletonScoped() - Only one instance of the class will ever be created for the process. This is the default behavior. 
      // FactoryScoped() - Each time a component is requested from the container, a new instance will be created. 
      // ContainerScoped() - This provides the flexibility needed to implement per-thread, per-request, or per-transaction component life-cycles. 
      // HttpRequestScoped() - Means that at most one instance of the component will be created for each incoming web request. This is handy for items that should be shared within a single request, e.g. repositories. 
      var builder = new ContainerBuilder(); 

      // SCIBS.Core components 
      builder.Register (c => new HttpContextWrapper (HttpContext.Current)).As<HttpContextBase>().HttpRequestScoped(); 
      builder.Register<ElmahLogger>().As<ILogger>().OnActivating (ActivatingHandler.InjectProperties).SingletonScoped(); 
      builder.Register<WebConfigurationManagerWrapper>().WithArguments (new NamedParameter ("applicationVirtualPath", HostingEnvironment.ApplicationVirtualPath)).As<IConfigurationManager>().SingletonScoped(); 
      builder.Register<ConfigManager>().As<IConfigManager>().SingletonScoped(); 
      builder.Register<AppSettings>().As<IAppSettings>().SingletonScoped(); 
      builder.Register<SparkTemplate>().As<ITemplate>().SingletonScoped(); 
      builder.Register<Security>().As<ISecurity>().HttpRequestScoped(); 
      builder.Register<MailerService>().WithArguments (new NamedParameter ("mailLogger", new Log4NetLogger ("EmailLogger"))).As<IMailerService>().FactoryScoped(); 

      builder.Register<QuoteService>().As<IQuoteService>().FactoryScoped(); 
      builder.Register<OrderService>().As<IOrderService>().FactoryScoped(); 

      builder.Register<AccountRepository>().As<IAccountRepository>().FactoryScoped(); 

      builder.Register<ContactService>().As<IContactService>().FactoryScoped(); 
      builder.Register<AccountService>().As<IAccountService>().FactoryScoped(); 
      builder.Register<AddressService>().As<IAddressService>().FactoryScoped(); 
      builder.Register<AspNetMembershipProvider>().As<IMembershipProvider>().FactoryScoped(); 

      var autofacControllerModule = new AutofacControllerModule (System.Reflection.Assembly.GetExecutingAssembly()); 
      autofacControllerModule.ActivatingHandler += ActivatingHandler.InjectProperties; 
      builder.RegisterModule (autofacControllerModule); 

      builder.RegisterModule (new PropertyInjectionForAllComponents()); 

      IContainerProvider containerProvider = new ContainerProvider (builder.Build()); 
      ControllerBuilder.Current.SetControllerFactory (new AutofacControllerFactory (containerProvider)); 

      return containerProvider; 
     } 
    } 
} 

Et comment il est appelé à partir de votre application:

public class MvcApplication : HttpApplication, IContainerProviderAccessor 
{ 
    private static IContainerProvider _containerProvider; 

    public IContainerProvider ContainerProvider 
    { 
     get 
     { 
      return _containerProvider; 
     } 
    } 

    protected void Application_Start() 
    { 
     Log.Info ("Application_Start"); 

     string serverPath = Server.MapPath ("~"); 
     _containerProvider = Bootstrapper.ConfigureAutofac (serverPath); 

     var configManager = ContainerProvider.RequestContainer.Resolve<IConfigManager>(); 
     EnableSSL = configManager.AppSettings ("EnableSSL").IsTrue(); 

     RegisterRoutes (RouteTable.Routes); 

     // NOTE: http://stackoverflow.com/questions/2854024/how-to-prevent-debug-assert-to-show-a-modal-dialog 
     var logger = ContainerProvider.RequestContainer.Resolve<ILogger>(); 
     Debug.Listeners.Clear(); 
     Debug.Listeners.Add (new LoggerTraceListener() {Log = logger}); 
    } 
} 
+0

Salut Todd, est-ce qu'il y a un moyen pour Autofac d'enregistrer automatiquement les Référentiels/Interfaces automatiquement, ou nous devons le faire manuellement pour chaque individu? StructureMap a une convention où il peut scanner l'ensemble et enregistrer tous les composants nécessaires. Merci. – Saxman

+0

L'appel à AutofacControllerModule le fait pour vous. Donnez-lui simplement une référence d'assemblage différente. –

Questions connexes