Is it possible to deduplicate registration in unit

2019-02-19 17:38发布

Consider interfaces:

public interface IOne{}
public interface ITwo{}
public interface IBoth : IOne, ITwo{}

And class

public class Both : IBoth{}

But when I need to resolve base interfaces I need to register both interfaces in container

<register type="IOne" MapTo="Both"/>
<register type="ITwo" MapTo="Both"/>

The question is - can I deduplicate the registration in a way like this:

<register type="IBoth" MapTo="Both"/>

But resolve it in different places from different interfaces:

var o = containet.Resolve<IOne>();
var t = containet.Resolve<ITwo>();

Can I do such a trick in any other way since this scenario is not working?...

1条回答
虎瘦雄心在
2楼-- · 2019-02-19 18:41

Short answer: You can't. Long answer: You can write a custom container extension that does this kind of trick for you.

[TestMethod]
public void TestMethod1()
{
  var container = new UnityContainer().AddNewExtension<DeduplicateRegistrations>();
  container.RegisterType<IBoth, Both>();
  IThree three = container.Resolve<IThree>();
  Assert.AreEqual("3", three.Three());
}

public class DeduplicateRegistrations : UnityContainerExtension
{
  protected override void Initialize()
  {
    this.Context.Registering += OnRegistering;
  }
  private void OnRegistering(object sender, RegisterEventArgs e)
  {
    if (e.TypeFrom.IsInterface)
    {
      Type[] interfaces = e.TypeFrom.GetInterfaces();
      foreach (var @interface in interfaces)
      {
        this.Context.RegisterNamedType(@interface, null);
        if (e.TypeFrom.IsGenericTypeDefinition && e.TypeTo.IsGenericTypeDefinition)
        {
          this.Context.Policies.Set<IBuildKeyMappingPolicy>(
            new GenericTypeBuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo)),
            new NamedTypeBuildKey(@interface, null));
        }
        else
        {
          this.Context.Policies.Set<IBuildKeyMappingPolicy>(
            new BuildKeyMappingPolicy(new NamedTypeBuildKey(e.TypeTo)),
            new NamedTypeBuildKey(@interface, null));
        }
      }
    }
  }
}
public class Both : IBoth
{
  public string One() { return "1"; }
  public string Two() { return "2"; }
  public string Three() { return "3"; }
}
public interface IOne : IThree
{
  string One();
}
public interface IThree
{
  string Three();
}
public interface ITwo
{
  string Two();
}
public interface IBoth : IOne, ITwo
{
}

You will need to fine-tune the extension in order to catch registration of interfaces like IDisposable or overwriting an already existing registration for a given interface.

查看更多
登录 后发表回答