温莎城堡类型工厂设施等效项

发布于 2024-10-01 02:40:57 字数 377 浏览 1 评论 0原文

是否有任何其他 .NET IoC 容器提供与温莎城堡中的类型化工厂设施等效的功能?

例如,如果我在 WPF 应用程序中使用抽象工厂模式:

public class MyViewModel
{
   private IAnotherViewModelFactory factory;

   public void ShowAnotherViewModel()
   {
      viewController.ShowView(factory.GetAnotherViewModel());
   }
}

我不想为我希望显示的每种类型的 ViewModel 创建 IAnotherViewModelFactory 的手动实现,我希望容器为我处理这个问题。

do any other .NET IoC containers provide equivalent functionality to the typed factory facility in Castle Windsor?

e.g. if I am using an abstract factory pattern in a WPF application:

public class MyViewModel
{
   private IAnotherViewModelFactory factory;

   public void ShowAnotherViewModel()
   {
      viewController.ShowView(factory.GetAnotherViewModel());
   }
}

I don't want to have to create a manual implementation of IAnotherViewModelFactory for every type of ViewModel I wish to show, I want the container to take care of this for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

腹黑女流氓 2024-10-08 02:40:57

AutoFac 有一个名为委托工厂的功能,但据我所知,它仅适用于委托,不适用于接口。

我在 StructureMap 和 Unity 中都没有遇到过类似 Castle 的类型化工厂设施的东西,但这并不一定意味着它们不存在......


我能想象为接口实现这样的东西的唯一方法是通过动态代理。由于温莎城堡有一个动态代理,但很少有其他容器有类似的东西,这可能有助于解释为什么这个功能并不普遍。

Unity 还提供拦截功能,因此它必须具有某种动态代理实现,但我很确定它没有与类型工厂等效的任何东西。与其他容器相比,Unity 相当基础。

AutoFac has a feature called Delegate Factories, but as far as I can tell, it works only with delegates, and not interfaces.

I haven't encountered anything similar to Castle's Typed Factory Facility in neither StructureMap nor Unity, but that doesn't necessarily mean that they're not there...


The only way I can imagine that something like this could be implemented for interfaces is via a dynamic proxy. Since Castle Windsor has a Dynamic Proxy, but few other containers have anything similar, this might go a long way to explain why this feature isn't ubiquitous.

Unity also offers interception capabilities, so it must have some sort of dynamic proxy implementation, but I'm pretty sure it doesn't have anything equivalent to Typed Factories. Compared to other containers, Unity is rather basic.

通知家属抬走 2024-10-08 02:40:57

在 Autofac 中,您可以在 Mark 提到的委托方法之上实现类型化工厂。例如,

class AnotherViewModelFactory : IAnotherViewModelFactory {
    Func<AnotherViewModel> _factory;
    public AnotherViewModelFactory(Func<AnotherViewModel> factory) {
        _factory = factory;
    }
    public AnotherViewModel GetAnotherViewModel() {
        return _factory();
    }
}

如果此类在容器中注册,则 Autofac 将与 AnotherViewModel 一起隐式提供 Func 实现:

builder.RegisterType<AnotherViewModel>();
builder.RegisterType<AnotherViewModelFactory>()
    .As<IAnotherViewModelFactory>();

实际上,您可以使用类型化工厂设施实现的任何接口使用这种方法在 Autofac 中实现。主要区别在于 Windsor 实现通过组件注册 API 来配置工厂,而在 Autofac 中,工厂本身就是一个组件。

有关更复杂的示例,您可能需要查看:http://code.google.com/ p/autofac/wiki/RelationshipTypeshttp://nblumhardt.com/ 2010/01/the-relationship-zoo/

In Autofac you can implement typed factories on top of the delegate approach Mark mentions. E.g.

class AnotherViewModelFactory : IAnotherViewModelFactory {
    Func<AnotherViewModel> _factory;
    public AnotherViewModelFactory(Func<AnotherViewModel> factory) {
        _factory = factory;
    }
    public AnotherViewModel GetAnotherViewModel() {
        return _factory();
    }
}

If this class is registered with the container, along with AnotherViewModel Autofac will provide the Func<AnotherViewModel> implementation implicitly:

builder.RegisterType<AnotherViewModel>();
builder.RegisterType<AnotherViewModelFactory>()
    .As<IAnotherViewModelFactory>();

Practically any interface you can implement using Typed Factory Facility can be implemented in Autofac using this kind of approach. The primary difference is that the Windsor implementation configures the factory through the component registration API, while in Autofac the factory is a component in its own right.

For more sophisticated examples you might like to look at: http://code.google.com/p/autofac/wiki/RelationshipTypes and http://nblumhardt.com/2010/01/the-relationship-zoo/.

江湖彼岸 2024-10-08 02:40:57

我最近为 Unity 实现了相当于 Castle Windsor Typed Factories 的功能。您可以在 https://github.com/PombeirP/Unity.TypedFactories 找到该项目,并且NuGet 包位于 http://nuget.org/packages/Unity.TypedFactories

用法如下:

unityContainer
    .RegisterTypedFactory<IFooFactory>()
    .ForConcreteType<Foo>();

参数匹配是按名称完成的,这很适合我的需求,尽管可以轻松扩展该库以支持其他需求。

I have recently implemented an equivalent of Castle Windsor Typed Factories for Unity. You can find the project at https://github.com/PombeirP/Unity.TypedFactories, and the NuGet package at http://nuget.org/packages/Unity.TypedFactories.

The usage is the following:

unityContainer
    .RegisterTypedFactory<IFooFactory>()
    .ForConcreteType<Foo>();

The parameter matching is done by name, which is fine for my needs, although the library could easily be extended to support other needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文