使用具有 Dependency 关键字和 UnityContainer 的属性进行初始化

发布于 2024-12-28 12:20:39 字数 926 浏览 0 评论 0原文

我有一个代码示例,看起来像这样。

public class AdventureWorksRepository
{
    [Dependency]
    private AdventureWorksEntities Context
    {
        get; set;
    }

    public AdventureWorksRepository()
    {
         SelectDemo();
    }

    public void SelectDemo()
    {
        var productNames = Context.Products.Select(item => item.Name);

        foreach (var productName in productNames)
        {
           Console.WriteLine("Name : "productName);
        }
    }
}

这是我所理解的主程序,

private static void Main(string[] args)
{
    UnityProvider.Container = new UnityContainer();
    UnityProvider.Container.RegisterInstance<AdventureWorksEntities>(new AdventureWorksEntities());
    var repository = UnityProvider.Container.Resolve<AdventureWorksRepository>();
}

Dependency 关键字应该告诉 Unity 初始化 AdventureworksEntities 属性,但我不断收到 null 引用异常任何提示我正在做什么或假设错误

I have a code sample which look something like this.

public class AdventureWorksRepository
{
    [Dependency]
    private AdventureWorksEntities Context
    {
        get; set;
    }

    public AdventureWorksRepository()
    {
         SelectDemo();
    }

    public void SelectDemo()
    {
        var productNames = Context.Products.Select(item => item.Name);

        foreach (var productName in productNames)
        {
           Console.WriteLine("Name : "productName);
        }
    }
}

and heres the main programe

private static void Main(string[] args)
{
    UnityProvider.Container = new UnityContainer();
    UnityProvider.Container.RegisterInstance<AdventureWorksEntities>(new AdventureWorksEntities());
    var repository = UnityProvider.Container.Resolve<AdventureWorksRepository>();
}

from what i've understood the Dependency keyword should tell Unity to initialize the the AdventureworksEntities property but I keep getting and null refrence exception any tips what I'm doing or assuming wrong

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

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

发布评论

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

评论(1

小…红帽 2025-01-04 12:20:39

我建议您不要使用 [Dependency] 属性。有了它们,您就可以在代码库中的任何地方引用容器。有关详细说明,请参阅本文

您可以告诉 Unity 您想要使用 InjectionProperty 注入依赖项,如下所示

container.RegisterType(typeof(IMyInterface), typeof(MyImplementation), new InjectionProperty("MyProperty"));

。如果您想将特定值注入该属性而不是让 Unity 解析该值,您还可以在 InjectionProperty 的构造函数中指定您自己的值。

顺便说一句:您的财产必须是公共的。它不适用于私人财产。如果您不想公开该属性,则应该进行构造函数注入

public class AdventureWorksRepository
{
  private readonly AdventureWorksContext context;
  public AdventureWorksRepository(AdventureWorksContext context)
  {
    if(context == null) throw new ArgumentNullException("context");
    this.context = context;
  }
}

I would recommend that you don't use the [Dependency] attribute. With them you have references to your container everywhere in your codebase. See this article for detailed explanation.

You can tell Unity that you want a dependency injected using InjectionProperty like this

container.RegisterType(typeof(IMyInterface), typeof(MyImplementation), new InjectionProperty("MyProperty"));

instead. If you want to inject a specific value into that property instead of letting Unity resolve that value you can also specify your own value in the constructor of InjectionProperty.

Btw.: Your property must be public. It won't work on private properties. If you don't want to make that property public you should instead go for constructor injection

public class AdventureWorksRepository
{
  private readonly AdventureWorksContext context;
  public AdventureWorksRepository(AdventureWorksContext context)
  {
    if(context == null) throw new ArgumentNullException("context");
    this.context = context;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文