将结构图转换为Autofac [英] Translating Structure Map into Autofac

查看:48
本文介绍了将结构图转换为Autofac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近购买了一本关于MVVM的非常好的书-《 Silverlight和WPF中用于企业体系结构的MVVM生存指南》

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

不幸的是,与IoC相关的部分中有很多针对StructureMap的代码示例,而Silverlight无法使用

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

任何人都可以指向我一个链接,该链接可以帮助我将结构图"代码转换为Autofac,这是我正在使用的注入工具

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

保罗

推荐答案

Autofac和StructureMap的工作方式不同,因此您不能将其一对一地翻译".
但是,这是应该达到的样子.
我做了一些假设,因为并不是所有的东西都可以测试您的代码.

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

这篇关于将结构图转换为Autofac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆