Translating Structure Map into Autofac

2019-06-24 04:11发布

I have recently puchased a very good book on MVVM - MVVM Survival Guide For Enterprise Architectures in Silverlight and WPF

Unfortunatly, one of the sections relating to IoC has a lot of code samples for StructureMap which is not available for Silverlight

Can anyone point me to a link that would help me translate Structure Map code to Autofac which is the injection tool I am looking at using

The code uses the factory mehod of creating classes and a bootstrapper

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>();
        }
    }
}

Paul

1条回答
贼婆χ
2楼-- · 2019-06-24 04:30

Autofac and StructureMap work differently, so you can't "translate" it one to one.
However, this is what it should look like to accomplish the same.
I've made some assumptions as not everything is there to test out 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));
    }
}
查看更多
登录 后发表回答