简体   繁体   English

简单喷油器中的工厂设施

[英]Factory facility in Simple Injector

I am migrating from Windsor to Simple Injector , I tried to follow the following this link . 我从Windsor迁移到Simple Injector ,我尝试遵循以下此链接 But I could not find any replacement for: 但是我找不到任何替代品:

container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ICacheDependencyFactory>().AsFactory());

What will be the replacement the above code in Simple Injector ? Simple Injector中将替换上面的代码吗?

Edited: 编辑:

ICacheDependencyFactory.cs ICacheDependencyFactory.cs

public interface ICacheDependencyFactory
{
    T Create<T>()
       where T : ICacheDependency;

    void Release<T>(T cacheDependency)
        where T : ICacheDependency;
}

Castle's factory facility is able to generate a proxy class based on the interface you supply. Castle的工厂设施能够根据您提供的接口生成代理类。 This proxy class will call back into the container to request the creation of a new instance of such type. 该代理类将回调到容器中,以请求创建此类新实例。

Simple Injector lacks such feature. 简单喷油器缺乏这种功能。 Simple Injector doesn't implement this, because: Simple Injector不能实现此目的,因为:

  • The number of factories your application code needs, should be fairly limited (to just a couple at most). 您的应用程序代码所需的工厂数量应该受到相当的限制(最多只有几个)。 Well designed applications hardly need factories. 设计良好的应用程序几乎不需要工厂。
  • It's really easy to implement this with a hand-written factory. 用手写工厂来实现它真的很容易。
  • Hand-written factories should be part of your composition root (where there already is a dependency on the container). 手写工厂应该是您的合成根目录的一部分(那里已经对容器有依赖性)。 This prevents application code from having to take a dependency on the container. 这避免了应用程序代码必须依赖于容器。

Here's an example: 这是一个例子:

private sealed class CacheDependencyFactory : ICacheDependencyFactory {
    public Container Container { get; set; }
    public T Create<T>() where T : ICacheDependency, class {
        return this.Container.GetInstance<T>();
    }
}

This factory can be registered as follows: 该工厂可以注册如下:

container.RegisterSingle<ICacheDependencyFactory>(
    new CacheDependencyFactory { Container = container });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM