我有许多类型,构造函数采用ICommand
参数,如下所示:
public AboutCommandMenuItem(ICommand command)
: base(command)
{
}
public OptionsCommandMenuItem(ICommand command)
: base(command)
{
}
我正在使用Ninject,我的ICommand
接口配置如下:
_kernel.Bind<ICommand>().To<AboutCommand>().WhenInjectedExactlyInto<AboutCommandMenuItem>();
_kernel.Bind<ICommand>().To<OptionsCommand>().WhenInjectedExactlyInto<OptionsCommandMenuItem>();
有没有办法设置约定,以便ICommand
接口XxxxCommand
注入XxxxCommandMenuItem
,并避免必须手动配置接口可以注入的每种可能的类型?
我尝试了BindToSelection
但是选择器表达式并没有捕获我注入的类型,而BindToRegex
似乎只是一个字符串类型的选择器。
这是我能得到的最接近的:
_kernel.Bind(t => t.FromThisAssembly()
.SelectAllClasses()
.InNamespaceOf<ICommand>()
.EndingWith("Command")
.Where(type => type.GetInterfaces().Contains(typeof(ICommand)))
.BindAllInterfaces()
.Configure(binding => binding
.When(request => request.Service == typeof(ICommand)
&& request.Target.Member.DeclaringType.Name.StartsWith(?)));
?
将是选择用于绑定的类的名称。
我是否坚持使用显式绑定?
答案 0 :(得分:5)
前体:
根据其他约束,最好使设计适应不共享ICommand
接口。为什么?将OptionsCommand
注入AboutCommandMenuItem
永远不会有意义。但是AboutCommandMenuItem
的ctor使它看起来好像没问题。
然而,我会假设你仍然希望继续这样做。以下是您的问题的几种可能的解决方案(不影响您的设计选择):
When
条件的方法。再次,将其与IBindingGenerator结合使用
When
表达式执行When
表达式仅执行比较。最后一个选项/替代方案的范例实施:
(有趣的部分首先)
测试(演示ItWorxxTm)
using FluentAssertions;
using Ninject;
using Ninject.Extensions.Conventions;
using Xunit;
public class MenuItemCommandConventionTest
{
[Fact]
public void Test()
{
var kernel = new StandardKernel();
kernel.Bind(x => x
.FromThisAssembly()
.IncludingNonePublicTypes()
.SelectAllClasses()
.InheritedFrom<ICommand>()
.BindWith<CommandBindingGenerator>());
kernel.Get<AboutMenuItem>()
.Command.Should().BeOfType<AboutCommand>();
kernel.Get<OptionsMenuItem>()
.Command.Should().BeOfType<OptionsCommand>();
}
}
绑定生成器:
using Ninject.Extensions.Conventions.BindingGenerators;
using Ninject.Syntax;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
public class CommandBindingGenerator : IBindingGenerator
{
private const string CommandSuffix = "Command";
private const string MenuItemTypeNamePattern = "{0}MenuItem";
public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(
Type type, IBindingRoot bindingRoot)
{
string commandName = GetCommandName(type);
Type menuItem = FindMatchingMenuItem(type.Assembly, commandName);
var binding = bindingRoot.Bind(typeof(ICommand)).To(type);
// this is a slight hack due to the return type limitation
// but it works as longs as you dont do Configure(x => .When..)
binding.WhenInjectedInto(menuItem);
yield return binding;
}
private static Type FindMatchingMenuItem(
Assembly assembly, string commandName)
{
string expectedMenuItemTypeName = string.Format(
CultureInfo.InvariantCulture,
MenuItemTypeNamePattern,
commandName);
Type menuItemType = assembly.GetTypes()
.SingleOrDefault(x => x.Name == expectedMenuItemTypeName);
if (menuItemType == null)
{
string message = string.Format(
CultureInfo.InvariantCulture,
"There's no type named '{0}' in assembly {1}",
expectedMenuItemTypeName,
assembly.FullName);
throw new InvalidOperationException(message);
}
return menuItemType;
}
private static string GetCommandName(Type type)
{
if (!type.Name.EndsWith(CommandSuffix))
{
string message = string.Format(
CultureInfo.InvariantCulture,
"the command '{0}' does not end with '{1}'",
type.FullName,
CommandSuffix);
throw new ArgumentException(message);
}
return type.Name.Substring(
0,
type.Name.Length - CommandSuffix.Length);
}
}
命令和菜单项:
public interface ICommand
{
}
class AboutCommand : ICommand
{
}
internal class OptionsCommand : ICommand
{
}
public abstract class MenuItem
{
private readonly ICommand command;
protected MenuItem(ICommand command)
{
this.command = command;
}
public ICommand Command
{
get { return this.command; }
}
}
public class OptionsMenuItem : MenuItem
{
public OptionsMenuItem(ICommand command)
: base(command) { }
}
public class AboutMenuItem : MenuItem
{
public AboutMenuItem(ICommand command)
: base(command) { }
}