Unity [Dependency] 属性无法解析

发布于 2024-08-18 10:46:32 字数 1709 浏览 6 评论 0原文

我有以下代码,当我尝试运行它时,我可以看到 BrokerProvider 没有被解析。这是我的代码:

static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);

            new TestBroker().RunTestBroker();              
        }


class TestBroker
    {
        private IBrokerProvider brokerProvider;

        public void RunTestBroker()
        {
            List<IPortfolio> portfolios = BrokerProvider.GetPortfolios();
        }

        [Dependency]
        public IBrokerProvider BrokerProvider
        {
            get { return brokerProvider; }
            set { brokerProvider = value; }
        }
    }

相关的配置

 <unity>
    <typeAliases>
      <typeAlias alias="string" type="System.String, mscorlib" />
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="IBrokerProvider" type="PA.Common.Interfaces.IBrokerProvider, PA.Common" />

      <typeAlias alias="PManager" type="PA.BrokerProviders.PManager, PA.BrokerProviders" />
    </typeAliases>
    <containers>
      <container>
        <types>
          <type type="IBrokerProvider" mapTo="PManager">
            <lifetime type="singleton" />
          </type>
        </types>
      </container>
    </containers>
  </unity>

另一个问题:我是否需要在我想使用 Unity 的每个其他类中重复 main 下的相同 3 行代码,或者设置一次就足够了?

I have the following code, and when I try to run it, I can see that the BrokerProvider is not being resolved. Here is my code:

static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            UnityConfigurationSection section = (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
            section.Containers.Default.Configure(container);

            new TestBroker().RunTestBroker();              
        }


class TestBroker
    {
        private IBrokerProvider brokerProvider;

        public void RunTestBroker()
        {
            List<IPortfolio> portfolios = BrokerProvider.GetPortfolios();
        }

        [Dependency]
        public IBrokerProvider BrokerProvider
        {
            get { return brokerProvider; }
            set { brokerProvider = value; }
        }
    }

The related config

 <unity>
    <typeAliases>
      <typeAlias alias="string" type="System.String, mscorlib" />
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="IBrokerProvider" type="PA.Common.Interfaces.IBrokerProvider, PA.Common" />

      <typeAlias alias="PManager" type="PA.BrokerProviders.PManager, PA.BrokerProviders" />
    </typeAliases>
    <containers>
      <container>
        <types>
          <type type="IBrokerProvider" mapTo="PManager">
            <lifetime type="singleton" />
          </type>
        </types>
      </container>
    </containers>
  </unity>

Another question: Do I need to repeat the same 3 lines of code that I have under main in every other class that I would like to use unity or setting it up once is enough?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀念你的温柔 2024-08-25 10:46:32

这是因为通过调用操作符 new 直接创建 TestBroker:

new TestBroker().RunTestBroker();

为了统一解决您的依赖关系,您需要像这样调用框架:

var broker = container.Resolve<TestBroker>();

IUnityContainer 是接口将为您完成所有工作 - 即将类型解析为实例。您只需创建一次,然后将其传递到应用程序中任何需要的地方。

That's because are creating TestBroker directly by calling operator new on it:

new TestBroker().RunTestBroker();

In order for unity to resolve your dependencies you need to call the framework like so:

var broker = container.Resolve<TestBroker>();

IUnityContainer is the interface that is going to be doing all the work for you - i.e. resolving types to instances. You only need to create it once and then just pass it around the application where ever you need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文