在尝试设置此示例一段时间后,简短的回答是:TinyIoC 不能正确地做到这一点,至少如果我们在传统意义上谈论“存储库”并希望它们被视为单例。容器。
话虽这么说,这种作品:
public interface IRepository { }
public class MoreAdvancedRepository : IRepository
{
public MoreAdvancedRepository(IRepository innerRepository, ISomeOtherDependency otherDependency) { }
}
public class TrickyRepository : IRepository
{
public TrickyRepository(IRepository innerRepository, ISomeOtherDependency otherDependency) { }
}
public class SimpleRepository : IRepository { }
public interface ISomeOtherDependency { }
public class SomeOtherDependencyWeasel : ISomeOtherDependency { }
// Register the other dependency
container.Register<ISomeOtherDependency, SomeOtherDependencyWeasel>();
// Register the "innermost" repository with a name
container.Register<IRepository, SimpleRepository>("simple");
// Register the inner decorator implementation, also with a name, and telling the container what to do for the dependency called "innerRepository"
container.Register<IRepository>((c, p) => c.Resolve<MoreAdvancedRepository>(new NamedParameterOverloads() { { "innerRepository", c.Resolve<IRepository>("simple") } }), "advanced");
// Register the outer decorator the same way, only without a name for the registration, so this will be what's resolved whenever IRepository is requested without specifying a name
container.Register<IRepository>((c, p) => c.Resolve<TrickyRepository>(new NamedParameterOverloads() { { "innerRepository", c.Resolve<IRepository>("advanced") } }));
// Resolve stuff to check how the registration worked out
var simple1 = container.Resolve<IRepository>("simple");
var simple2 = container.Resolve<IRepository>("simple");
var advanced1 = container.Resolve<IRepository>("advanced");
var advanced2 = container.Resolve<IRepository>("advanced");
var tricky1 = container.Resolve<IRepository>();
var tricky2 = container.Resolve<IRepository>();
Assert.IsType<SimpleRepository>(simple1); // this passes, unsurprisingly
Assert.Same(simple1, simple2); // this passes, too, as simple Register<TResolve, TImpl>() calls are implicitly .AsSingleton()
Assert.IsType<MoreAdvancedRepository>(advanced1); // passes
Assert.IsType<TrickyRepository>(tricky1); // passes
Assert.Same(advanced1, advanced2); // this fails, as Register<TResolve>(Func<TResolve, TinyIoCContainer, NamedParameterOverloads>) calls are implicitly .AsMultiInstance() and can not be changed to .AsSingleton()
Assert.Same(tricky1, tricky2); // fails for the same reason
现在有人可能会试图通过这样做来欺骗容器(我曾经):
container.Register<MoreAdvancedRepository>((c, p) => c.Resolve<MoreAdvancedRepository>(new NamedParameterOverloads() { { "innerRepository", c.Resolve<IRepository>("simple") } })); // always .AsMultiInstance()
container.Register<IRepository, MoreAdvancedRepository>("advanced"); // implicitly .AsSingleton(), so only one instance should be created and then returned for subsequent calls
然后可以将其包装在扩展方法中,以便在容器上再次调用单个方法。不幸的是,这不起作用——在尝试解析IRepository
名为“advanced”的注册时,容器显然不再找到以前的显式注册MoreAdvancedRepository
并抛出TinyIoCResolutionException
.
因此,如果
- 每个不同的存储库类型仅解析一次,或者
- 每次解析特定的存储库类型时创建一个新实例都没有问题,则上述方法将起作用。
否则,您可能需要为这些课程退回到穷人的 DI 或使用不同的 IoC 容器。