代码之家  ›  专栏  ›  技术社区  ›  Inx51

在控制台应用程序中使用Unity DI

  •  4
  • Inx51  · 技术社区  · 11 年前

    然而,我试图让Unity使用我的控制台应用程序。。我尝试依赖注入的所有属性仍然设置为null。

    这是我的代码:

    程序.cs

    namespace .Presentation.Console
    {
        class Program
        {
            static void Main(string[] args)
            {
                var mainThread = new MainThread();
            }
        }
    }
    

    主线程.cs

    namespace xxxx.Presentation.Console
    {
        public class MainThread
        {
            public IUnitOfWork UnitOfWork { get; set; }
    
            public IMapper Mapper { get; set; }
    
            public MainThread()
            {
                Mapper.RegisterMappings();
            }
        }
    }
    

    应用程序.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
      </startup>
      <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <alias alias="IDataAccess" type="UnityFramework.IDataAccess, UnityFramework" />
        <namespace name="UnityFramework" />
        <assembly name="UnityFramework" />
        <container>
          <types>
    
            <type type="IMapper" mapTo="xxxx.Core.Parse.ParseMapper" />
    
          </types>
        </container>
      </unity>
    </configuration>
    

    App.config设置为“始终复制”

    在这种情况下,Mapper返回为null(我假设UnitOfWork也是)

    我还需要做什么吗?是否向app.config中添加内容?我错过了什么吗?

    提前感谢!

    Br, Inx公司

    1 回复  |  直到 11 年前
        1
  •  12
  •   Community Mohan Dere    9 年前

    团结一致 只有 为通过获得的组件提供依赖关系 Resolve 或者在解决子依赖关系时。必须手动从容器中获取“根”组件。

    使用 new Program 自动提供依赖项,因为它绕过Unity容器。

    static class ProgramEntry
    {
        static void Main(string[] args)
        {
            var unity = CreateUnityContainerAndRegisterComponents();
            // Explicitly Resolve the "root" component or components
            var program = unity.Resolve<Program>();
            program.Run();
        }
    }
    
    public class Program
    {
        readonly Ix _x;
    
        // These dependencies will be automatically resolved
        // (and in this case supplied to the constructor)
        public Program(IMapper mapper, Ix x)
        {
            // Use dependencies
            mapper.RegisterMappings();
    
            // Assign any relevant properties, etc.
            _x = x;
        }
    
        // Do actual work here
        public void Run() {
            _x.DoStuff();
        }
    }
    

    对于大多数任务,我更喜欢基于代码的注册。

    • 我建议 使用属性,尽管它们“会起作用” 如果 遵循 决定 上面的图案。 必须手动解析“根”对象。

      属性的问题是这些添加依赖项 在…上 团结——“反转”就这么多!

      构造函数注入(如图所示)是 自动/默认 看见 Setter / property injection in Unity without attributes 如果首选属性注入。

    • 我可能会解决工厂(或 Func<UoW> )创建一个UoW,并在适用时将其提供给调用堆栈上下文(即将其传递给方法)。应用程序在一次运行期间可能有许多不同的UoW。在这种情况下,您可能还对创建作用域感兴趣。

    • 我也可能会在解析IMapper对象时使用工厂来创建预配置的IMapper,而不是事后使用RegisterMappings。