Unity - 无需resolve()即可解析

发布于 2024-11-01 06:55:54 字数 525 浏览 2 评论 0原文

我只想用属性 [DoInjection] 标记一个属性并让 unity 进行注入。我不想使用 prop = Unity.Resolve(type)。那真是痛苦又混乱。 Unity 是否提供了执行此操作的属性,还是我必须构建自己的属性?

编辑:

ISessionFactory sf = new SessionFactory();
            container.RegisterType<IRepository, CustomerRepository>(new InjectionConstructor(sf.CurrentUoW));
            container.RegisterInstance<IUnitOfWork>(sf.CurrentUoW);

在 ClassX 其他类中的 IUnitOfWork 属性上使用 [Dependancy] 在 App.Main 中注册,但它始终为空。我是否需要使用 Unity 构建 ClassX 实例才能使其正常工作?看来我确实必须这样做。我不喜欢这样。

I would like to just markup a property with an attribute [DoInjection] and have unity do the injection. I don't want to have to use prop = Unity.Resolve(type). Thats a pain and messy. Does unity provide attributes to do this or do I have to build my own?

Edit: register in App.Main

ISessionFactory sf = new SessionFactory();
            container.RegisterType<IRepository, CustomerRepository>(new InjectionConstructor(sf.CurrentUoW));
            container.RegisterInstance<IUnitOfWork>(sf.CurrentUoW);

Using [Dependancy] on IUnitOfWork propery in ClassX other class but it's always null. Do I need to build ClassX instance using Unity to get this to work? It looks like I do have to. I don't like that.

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

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

发布评论

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

评论(1

时光与爱终年不遇 2024-11-08 06:55:54

Unity 有一个 DependencyAttribute 您可以使用它:

public class MyObject
{
  private SomeOtherObject _dependentObject;

  [Dependency]
  public SomeOtherObject DependentObject 
  {
    get { return _dependentObject; }
    set { _dependentObject = value; }
  }
}

http: //msdn.microsoft.com/en-us/library/ff650198.aspx

根据您的问题,听起来您可能尝试在错误的位置使用 Unity,而您的设计感告诉您事实并非如此。感觉不对。您应该只在引导应用程序的地方看到 Unity。这是控制台应用程序中的 Main 方法,或者是 Web 或 wcf 应用程序中的 Global.asax 方法。这个想法是在整个链上一直依赖依赖关系,直到到达使用 IoC 容器引导并解析一个顶级对象的位置。在控制台应用程序中,我这样做:

class Program
{
    static void Main(string[] args)
    {
        using (var container = new UnityContainer())
        {
            container
                .AddExtension(new ConfigureForConsole(args))
                .Resolve<MyApplication>()
                .Execute();
        }
    }
}

http://www.agileatwork.com/console -application-with-ioc/

在这种情况下,MyApplication 是我的顶级对象(这里不需要是接口)。 ConfigureForConsole 只是一个一次性的自定义容器扩展,其中包含所有 RegisterType 行。或者,您可以在此处从 App.Config 初始化容器。但想法是您的 Main 方法中几乎没有任何内容。这种方法的另一个好处是它使您的代码更加可移植。我发现控制台应用程序通常会变成 Windows 服务,并且保持干净整洁可以使这种转换变得非常轻松。

Unity has a DependencyAttribute you can use for this:

public class MyObject
{
  private SomeOtherObject _dependentObject;

  [Dependency]
  public SomeOtherObject DependentObject 
  {
    get { return _dependentObject; }
    set { _dependentObject = value; }
  }
}

http://msdn.microsoft.com/en-us/library/ff650198.aspx

Based on your question, it sounds like you might be trying to use Unity in the wrong spot and your design sense was telling you it didn't feel right. You should only see Unity where you bootstrap your application. That's your Main method in a console app or Global.asax in a web or wcf app. The idea is to keep relying on dependencies all the way up the chain until you get to where you bootstrap and resolve just that one top level object using your IoC container. In a console app, I do this:

class Program
{
    static void Main(string[] args)
    {
        using (var container = new UnityContainer())
        {
            container
                .AddExtension(new ConfigureForConsole(args))
                .Resolve<MyApplication>()
                .Execute();
        }
    }
}

http://www.agileatwork.com/console-application-with-ioc/

In this case, MyApplication is my top level object (it doesn't need to be an interface here). The ConfigureForConsole is just a one-off custom container extension that has all the RegisterType lines in there. Alternatively you could initialize the container from App.Config here. The idea though is that your Main method has almost nothing in it. Another benefit of this approach is that it makes your code more portable. I find that console apps usually turn into windows services and keeping things clean here makes that transition pretty painless.

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