使用 UNITY 覆盖对象参数
我已经开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想自动为 ITab 实现的AllowVisible 属性分配一个值,则可以使用Unity 提供的InjectionProperty 类型。
您可以以流畅的方式执行此操作,例如:
更具体一点:
有关如何注入构造函数参数、属性值等的更多信息,请查看:
http://msdn.microsoft.com/en-us/library/ff650036.aspx
至于你的第二部分问题,当您解析 ITab 的实现时,必须将构造函数参数 (IViewModel) 传递给 Unity 的 Resolve(...) 方法。
这个问题之前已经被问过,请查看:
我可以吗将构造函数参数传递给 Unity 的 Resolve() 方法?
为了完整起见:
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:
A bit more concrete:
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: