我正在尝试找出将ICommand注入(inject)ViewModel的正确方法。

鉴于我的ViewModel看起来像这样。

public class ViewModel : IViewModel
{
    ICommand LoadCommand { get; }
    ICommand SaveCommand { get; }
}

我目前在我的构造函数中执行此操作
public ViewModel(IRepository repository, IErrorLog errorLog, IValidator validator)
{
    LoadCommand = new LoadCommandImpl(repository, errorLog);
    SaveCommand = new SaveCommandImpl(repository, errorLog, validator);
}

请注意,除了构造命令之外,ViewModel根本不使用参数。

虽然我尝试在注入(inject)的接口(interface)中包含尽可能多的逻辑,但是命令中仍然包含逻辑。

这样做似乎更合适
public ViewModel(ICommand loadCommand, ICommand saveCommand)
{
    LoadCommand = loadCommand;
    SaveCommand = saveCommand;

    LoadCommand.SetViewModel(this);
    SaveCommand.SetViewModel(this);
}

但是,要做到这一点,我需要像这样进行我的Unity注册。这不是世界末日,但似乎很痛苦。
container.RegisterType<ICommand, LoadCommandImpl>("loadCommand");
container.RegisterType<ICommand, SaveCommandImpl>("saveCommand");

container.RegisterType<IViewModel, ViewModel>(
    new InjectionConstructor(
        new ResolvedParameter<ICommand>("loadCommand"),
        new ResolvedParameter<ICommand>("SaveCommand")));

另外,我可以创建ILoadCommandISaveCommand接口(interface),但是这些接口(interface)将为空或可能实现ICommand

我不是所有这些解决方案的忠实拥护者。这里推荐的方法是什么?

编辑以响应blindmeis

让我们暂时假装这不是命令。
public ViewModel(IFoo foo)
{
    Bar = new Bar(foo);
}

我认为,仅注入(inject)IBar会更合适
public ViewModel(IBar bar)
{
    Bar = bar;
}

但是现在我有了Bar1Bar2。所以我可以
public ViewModel(IFoo foo)
{
    Bar1 = new Bar1(foo);
    Bar2 = new Bar2(foo);
}

或者
public ViewModel(IBar bar1, IBar bar2)
{
    Bar1 = bar1;
    Bar2 = bar2;
}

最佳答案

这种行为不包含在Unity中,但不难改进。

var container = new UnityContainer();
container.AddNewExtension<MapParameterNamesToRegistrationNamesExtension>();
container.RegisterType<ICommand, LoadCommand>("loadCommand");
container.RegisterType<ICommand, SaveCommand>("saveCommand");
container.RegisterType<ViewModel>(new MapParameterNameToRegistrationName());
var vm = container.Resolve<ViewModel>();
Assert.IsType(typeof(LoadCommand), vm.LoadCommand);
Assert.IsType(typeof(SaveCommand), vm.SaveCommand);

public class MapParameterNamesToRegistrationNamesExtension : UnityContainerExtension
{
  protected override void Initialize()
  {
    var strategy = new MapParameterNamesToRegistrationNamesStrategy();
    this.Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
  }
}
public class MapParameterNamesToRegistrationNamesStrategy : BuilderStrategy
{
  public override void PreBuildUp(IBuilderContext context)
  {
    if (context.Policies.Get<IMapParameterNameToRegistrationNamePolicy>(context.BuildKey) == null)
    {
      return;
    }
    IPolicyList resolverPolicyDestination;
    IConstructorSelectorPolicy selector = context.Policies.Get<IConstructorSelectorPolicy>(context.BuildKey, out resolverPolicyDestination);
    var selectedConstructor = selector.SelectConstructor(context, resolverPolicyDestination);
    if (selectedConstructor == null)
    {
      return;
    }
    var parameters = selectedConstructor.Constructor.GetParameters();
    var parameterKeys = selectedConstructor.GetParameterKeys();
    for (int i = 0; i < parameters.Length; i++)
    {
      Type parameterType = parameters[i].ParameterType;
      if (parameterType.IsAbstract || parameterType.IsInterface)
      {
        IDependencyResolverPolicy resolverPolicy = new NamedTypeDependencyResolverPolicy(parameterType, parameters[i].Name);
        context.Policies.Set<IDependencyResolverPolicy>(resolverPolicy, parameterKeys[i]);
      }
    }
    resolverPolicyDestination.Set<IConstructorSelectorPolicy>(new SelectedConstructorCache(selectedConstructor), context.BuildKey);
  }
}
public class MapParameterNameToRegistrationName : InjectionMember
{
  public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies)
  {
    policies.Set<IMapParameterNameToRegistrationNamePolicy>(new MapParameterNameToRegistrationNamePolicy(), new NamedTypeBuildKey(implementationType, name));
  }
}
public interface IMapParameterNameToRegistrationNamePolicy : IBuilderPolicy
{
}
public class MapParameterNameToRegistrationNamePolicy : IMapParameterNameToRegistrationNamePolicy
{
}

该代码和测试可以在TecX project on CodePlex的源代码中找到。 TecX.Unity项目(文件夹注入(inject))。

关于silverlight - 将命令注入(inject)到ViewModels中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11567323/

10-14 22:08
查看更多