0
public class ServiceThatProvidesDep
{
    public Dep GetDep()
    {
        // return dep object
    }
}

public class ServiceThatConsumesDep
{
    public ServiceThatConsumesDep(Dep dep)
    {
        // ...
    }
}

インストーラーは次のようになります。

container.Register(Component.For<ServiceThatProvidesDep>());
container.Register(Component.For<Dep>().UsingService<ServiceThatProvidesDep>(s => s.GetDep()));
4

2 に答える 2

0

ServiceThatProvidesDep は代わりに Dep ファクトリにする必要があります

public interface IDepFactory
{
    public Dep CreateDep();
}

それを消費するサービスに注入する必要があります

public class ServiceThatUsesDep
{
    public ServiceThatUesDep( IDepFactory factory )
    ...

このようにして、物事を再発明しようとはしません。

于 2013-09-08T18:28:53.343 に答える