Mapping a structural map to Autofac

I recently published a very good book about MVVM - MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF

Unfortunately, in one of the sections related to IoC, there are many sample code for StructureMap, which is not available for Silverlight

Can someone point me to a link that will help me translate the Structure Map code into Autofac, which is the injection tool I use

The code uses the factory method for creating classes and the loader

using Northwind.ViewModel; using StructureMap; namespace Northwind.UI.WPF { public class BootStrapper { public MainWindowViewModel MainWindowViewModel { get { return ObjectFactory .GetInstance<MainWindowViewModel>(); } } public BootStrapper() { ObjectFactory.Initialize( o => o.Scan( a => { a.WithDefaultConventions(); a.AssembliesFromApplicationBaseDirectory( d => d.FullName .StartsWith("Northwind")); a.LookForRegistries(); })); } } using StructureMap; namespace Northwind.ViewModel { public class CustomerDetailsViewModelFactory : ICustomerDetailsViewModelFactory { private readonly IContainer _container; public CustomerDetailsViewModelFactory( IContainer container) { _container = container; } public CustomerDetailsViewModel CreateInstance( string customerID) { return _container .With("customerID") .EqualTo(customerID) .GetInstance<CustomerDetailsViewModel>(); } } } 

Floor

+4
source share
1 answer

Autofac and StructureMap work differently, so you cannot "translate" it one on one.
However, this is what should look like to accomplish the same thing.
I made some assumptions, since not everything is to check your code.

 public class BootStrapper { private readonly ILifetimeScope _container; public BootStrapper() { var builder = new ContainerBuilder(); Assembly[] assemblies = GetAssembliesFromApplicationBaseDirectory( x => x.FullName.StartsWith("Northwind")); builder.RegisterAssemblyTypes(assemblies) .AsImplementedInterfaces(); // Module in Autofac = Registry in StructureMap builder.RegisterAssemblyModules(assemblies); Assembly viewModelAssembly = typeof(MainWindowViewModel).Assembly; builder.RegisterAssemblyTypes(viewModelAssembly); _container = builder.Build(); } private static Assembly[] GetAssembliesFromApplicationBaseDirectory(Func<AssemblyName, bool> condition) { string baseDirectoryPath = AppDomain.CurrentDomain.BaseDirectory; Func<string, bool> isAssembly = file => string.Equals( Path.GetExtension(file), ".dll", StringComparison.OrdinalIgnoreCase); return Directory.GetFiles(baseDirectoryPath) .Where(isAssembly) .Where(f => condition(new AssemblyName(f))) .Select(Assembly.LoadFrom) .ToArray(); } public MainWindowViewModel MainWindowViewModel { get { return _container.Resolve<MainWindowViewModel>(); } } } public class CustomerDetailsViewModelFactory : ICustomerDetailsViewModelFactory { private readonly ILifetimeScope _container; public CustomerDetailsViewModelFactory(ILifetimeScope container) { _container = container; } public CustomerDetailsViewModel CreateInstance(string customerID) { return _container.Resolve<CustomerDetailsViewModel>( new NamedParameter("customerID", customerID)); } } 
0
source

All Articles