简体   繁体   English

首先如何从迁移代码中将依赖注入与Unity结合使用?

[英]How use Dependency Injection with Unity from Migration Code First?

I need to use dependency injection in the Migrations\\Configuration.cs for seeding values from my service layer. 我需要在Migrations \\ Configuration.cs中使用依赖项注入来从服务层播种值。 I use Unity to do that. 我使用Unity来做到这一点。 My Unity container work fine in the entire web site because I instantiate all Interfaces via Constructor class. 我的Unity容器在整个网站上都能正常工作,因为我通过Constructor类实例化了所有接口。 But I cannot do that in the Configuration.cs file because Code First use an empty Constructor. 但是我无法在Configuration.cs文件中执行此操作,因为“代码优先”使用空的构造函数。

Here is my code. 这是我的代码。 Tell me what I'm doing wrong? 告诉我我做错了什么?

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
    {
        private IGenderService _genderService;

        public Configuration()
        {
            AutomaticMigrationsEnabled = false;

            using (var container = UnityConfig.GetConfiguredContainer())
            {
                var isRegister = container.IsRegistered<IGenderService>(); // Return true!
                _genderService = container.Resolve<IGenderService>();

                if (_genderService == null)
                    throw new Exception("Doh! Empty");
                else
                    throw new Exception("Yeah! Can continue!!");
            };
        }

        public Configuration(IGenderService genderService) // Cannot use this constructor because of Code First!!!
        {
            _genderService = genderService;
        }
}

The _genderService is always null and I got this error in the same way : _genderService始终为null,并且我以相同的方式收到此错误:

Type 'Microsoft.Practices.Unity.ResolutionFailedException' in assembly 'Microsoft.Practices.Unity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable. 程序集“ Microsoft.Practices.Unity,版本= 3.5.0.0,文化=中性,PublicKeyToken = 31bf3856ad364e35”中的类型“ Microsoft.Practices.Unity.ResolutionFailedException”未标记为可序列化。

Thank, 谢谢,

David 大卫

I don't know Unity, but your question is a common DI patterns. 我不了解Unity,但是您的问题是常见的DI模式。

IMHO the problem is you pass your ioc container around , in Configuration ctor: 恕我直言,问题是您在配置ctor中将ioc容器传递给了

public Configuration()  {
  AutomaticMigrationsEnabled = false;
  using (var container = UnityConfig.GetConfiguredContainer())  {
        ....
  }

you should use a CompositionRoot : 您应该使用CompositionRoot

public class Bootstrap {
  public void Register() {
     var container = new UnityContainer();
     container.RegisterType<IGenderService, GenderService>();
     container.RegisterType<Configuration>();

  }
}

internal sealed class Configuration:  {
  private IGenderService _genderService;
  public Configuration(IGenderService genderService) {
     _genderService = genderService;
  }
}

...
// resolving
var config = container.Resolve<Configuration>();

Behind the scenes, the Unity container first constructs a GenderService object and then passes it to the constructor of the Configuration class. 在后台,Unity容器首先构造一个GenderService对象,然后将其传递给Configuration类的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM