在Windsor注册一个开放泛型类型组件,用于开放泛型类型的通用服务 [英] Register in Windsor an open generic type component for generic service of open generic type

查看:143
本文介绍了在Windsor注册一个开放泛型类型组件,用于开放泛型类型的通用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望标题至少有一定意义。



我有如下代码的情况。测试通过,但我想注册

  GenericCommandHandler<> 

作为实现的开放泛型

  IHandler< GenericCommand<>> 

我可以忍受下面的事情,因为赋给GenericCommandHandler的类型数量是有限的,我可以使用Castle.MicroKernel.Registration;注册它们都是单独的,但想要更自动化的东西。

  
使用Castle.Windsor;
使用NUnit.Framework;

[TestFixture]
public class Class1
{
[Test]
public void t()
{
using( var container = new WindsorContainer())
{
//如何将其注册为开放通用类型?
container.Register(
Component.For< IHandler< GenericCommand< object>>>()
.ImplementedBy< GenericCommandHandler< object>>());

var handler = container.Resolve< IHandler< GenericCommand< object>>>();
}
}
}

公共接口IHandler< TCommand>
{
void Handle(TCommand o);
}

public class GenericCommand< T>
{
}

公共类GenericCommandHandler< T> :IHandler< GenericCommand< T>>
{
public void Handle(GenericCommand< T> $)
{
}
}


解决方案

Artur,

您要求的是支持半正如Mauricio所说,.NET框架中没有API来支持这些泛型。你可能会破解它,以便它在大多数情况下都能正常工作(Windsor的问题跟踪器中有一张支持这个标签的门票),但是API中没有建立血统会意味着使其工作将是一项重大的努力。 / p>

你可以看看在我的这篇博客中,它解决了特定组件的问题。也许这对你的需求就足够了。


I hope the title makes at least some sense.

I have the situation as in the code below. The test passes, but I would like to register

GenericCommandHandler<> 

as open generic type as implementation for

IHandler<GenericCommand<>>

I can live with the thing below, because the number of types given to GenericCommandHandler is limited and I can register them all individually, but would like something more "automated".

using Castle.MicroKernel.Registration;
using Castle.Windsor;
using NUnit.Framework;

[TestFixture]
public class Class1
{
   [Test]
   public void t()
   {
      using( var container = new WindsorContainer() )
      {
         // HOW TO REGISTER IT AS OPEN GENERIC TYPE?
         container.Register(
            Component.For<IHandler<GenericCommand<object>>>()
               .ImplementedBy<GenericCommandHandler<object>>() );

         var handler = container.Resolve<IHandler<GenericCommand<object>>>( );
      }
   }
}

public interface IHandler<TCommand>
{
   void Handle(TCommand o);
}

public class GenericCommand<T>
{
}

public class GenericCommandHandler<T> : IHandler<GenericCommand<T>>
{
   public void Handle( GenericCommand<T> o )
   {
   }
}

解决方案

Artur,

What you're asking for is support for semi-closed generics and as Mauricio mentioned there's no API in .NET framework to support these. You kind of could hack this in, so that it works in most of the cases (there's a ticket open in Windsor's issue tracker to support this) but lack of descent built in API would mean that making it work would be a significant effort.

You can have a look at this blogpost of mine, which tackles the issue for specific component. Perhaps that would be enough for your needs.

这篇关于在Windsor注册一个开放泛型类型组件,用于开放泛型类型的通用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆