使用 UNITY 覆盖对象参数

发布于 2024-12-06 03:55:48 字数 1309 浏览 1 评论 0原文

我已经开始使用 jg MS Unity 作为 IOC 容器的项目,并且有两个关于覆盖参数的问题。

public interface ITab
{
   bool AllowVisible {get;set;}
}

class Tab : ITab
{
  IViewModel vm;

  public Tab(IViewModel vm)
  {
    this.vm = vm; 
  }

  public bool allowVisible = false;

  public bool AllowVisible
  {
    get{ return allowVisible};
    set{ allowVisible = vlaue};
  }
} 

public interface IViewModule
{
  string Name;
}

public class ViewModel
{
  public string Name;
}

1)如何统一设置 Tab 类型,以便我可以将 true 或 false 作为参数传递给 AllowVisible 属性?我不想添加 附加行 tab.AllowVisible = true; 如下例所示

void Main()
{
   ITab tab = unityContainer.RegisterType<ITab, Tab>(); 
   tab.AllowVisible = true;
}

2) 如果我已经有 ViewModel 的实例,例如下面例中的 vm ,如何做我让容器解析Tab对象,同时将vm对象传递到其构造函数中?目前,当我解析 tab 对象时,容器 创建 ViewModel 的另一个实例。 我希望将虚拟机实例用作选项卡对象视图模型?

void Main()
{
    unityContainer.RegisterType<IViewModel, ViewModel>();
    unityContainer.RegisterType<ITab, Tab>();

   ViewModel vm = unityContainer.Resolve<IViewModel>();   
   ITab tab = unityContainer.RegisterType<ITab, Tab>(); 
}

I've started a project usinjg MS Unity as my IOC container and have two questions regarding overriding parameters.

public interface ITab
{
   bool AllowVisible {get;set;}
}

class Tab : ITab
{
  IViewModel vm;

  public Tab(IViewModel vm)
  {
    this.vm = vm; 
  }

  public bool allowVisible = false;

  public bool AllowVisible
  {
    get{ return allowVisible};
    set{ allowVisible = vlaue};
  }
} 

public interface IViewModule
{
  string Name;
}

public class ViewModel
{
  public string Name;
}

1) How do i set up the Tab type in unity so that i can pass in true or false to the AllowVisible property as a paramater? I dont want to have to add the
additional line of tab.AllowVisible = true; as in the case below

void Main()
{
   ITab tab = unityContainer.RegisterType<ITab, Tab>(); 
   tab.AllowVisible = true;
}

2) If i already have an instance of the ViewModel, such as vm in the case below, how do i make the container resolve the Tab object while passing in the vm object into its constructor? Currently when i resolve for the tab object, the container creates another instance of the ViewModel. I want the vm instance to get used as the tab objects viewmodel?

void Main()
{
    unityContainer.RegisterType<IViewModel, ViewModel>();
    unityContainer.RegisterType<ITab, Tab>();

   ViewModel vm = unityContainer.Resolve<IViewModel>();   
   ITab tab = unityContainer.RegisterType<ITab, Tab>(); 
}

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

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

发布评论

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

评论(1

烟雨凡馨 2024-12-13 03:55:48

如果您想自动为 ITab 实现的AllowVisible 属性分配一个值,则可以使用Unity 提供的InjectionProperty 类型

您可以以流畅的方式执行此操作,例如:

IUnityContainer myContainer = new UnityContainer();
myContainer.Configure<InjectedMembers>()
           .ConfigureInjectionFor<MyObject>(
               new InjectionProperty("MyProperty"),
               new InjectionProperty("MyStringProperty", "SomeText"))
);

更具体一点:

container.RegisterType<ITab, Tab>();
container.RegisterType<ITab, Tab>(
    new InjectionProperty("AllowVisible", true) 
);

有关如何注入构造函数参数、属性值等的更多信息,请查看:

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

至于你的第二部分问题,当您解析 ITab 的实现时,必须将构造函数参数 (IViewModel) 传递给 Unity 的 Resolve(...) 方法。

这个问题之前已经被问过,请查看:

我可以吗将构造函数参数传递给 Unity 的 Resolve() 方法?

为了完整起见:

var viewModel = container.Resolve<IViewModel>();
container.Resolve<ITab>(new ParameterOverrides<Tab> { { "vm", viewModel} });"

If you automatically want to assign a value to the AllowVisible property of your ITab implementation then you can use the InjectionProperty type provided by Unity.

You can do this in a fluent way, for example:

IUnityContainer myContainer = new UnityContainer();
myContainer.Configure<InjectedMembers>()
           .ConfigureInjectionFor<MyObject>(
               new InjectionProperty("MyProperty"),
               new InjectionProperty("MyStringProperty", "SomeText"))
);

A bit more concrete:

container.RegisterType<ITab, Tab>();
container.RegisterType<ITab, Tab>(
    new InjectionProperty("AllowVisible", true) 
);

For more information on how to inject constructor parameters, property values...etc, check out:

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

As for the second part of you question, you must pass constructor parameters (IViewModel) to Unity's Resolve(...) method when you resolve an implementation for an ITab.

This question has been asked before on SO, check out:

Can I pass constructor parameters to Unity's Resolve() method?

For completeness' sake:

var viewModel = container.Resolve<IViewModel>();
container.Resolve<ITab>(new ParameterOverrides<Tab> { { "vm", viewModel} });"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文