我试图允许我的库的用户通过tinyioc内部连接,传递他们自己在我的库中定义的接口的实现,然后在需要实例时将其解析。
例如,用户可以这样做:
public class Logger : Things.ILogger { // Implement members public void Log(string s) { ... } } var thing = new Things.ThingBuilder().WithLogger(new Logger()).Build();在我的库中,我想连接传入的记录器。类似于:
namespace Things { public class ThingBuilder() { private ILogger logger; public ThingBuilder WithLogger(ILogger logger) { this.logger = logger; return this; } public Thing Build() { TinyIocContainer container = new TinyIocContainer(); container.Register(ILogger, logger); // I want this to be used // wherever an ILogger is needed ... } } }但是,我找不到TinyIoc让我这样做的方法。 这支持吗? 我知道TinyIoc支持注册它知道的类型的实例,但是如果类型未知则不支持。
I'm trying to allow users of my library, which is internally wired up via tinyioc, to pass in their own implementation of an interface defined within my library which will then be resolved to whenever an instance is needed.
For example, the user can do this:
public class Logger : Things.ILogger { // Implement members public void Log(string s) { ... } } var thing = new Things.ThingBuilder().WithLogger(new Logger()).Build();Within my library I want to wire up the logger which is passed in. Something like:
namespace Things { public class ThingBuilder() { private ILogger logger; public ThingBuilder WithLogger(ILogger logger) { this.logger = logger; return this; } public Thing Build() { TinyIocContainer container = new TinyIocContainer(); container.Register(ILogger, logger); // I want this to be used // wherever an ILogger is needed ... } } }However, I can't find a way TinyIoc will let me do this. Is this supported? I know that TinyIoc supports registering instances of types it knows about, but not if the type is unknown.
最满意答案
最后,我通过手动将用户提供的ILogger实例传递到库代码库中所需的任何地方来解决这个问题。 这是令人满意的,因为我认为反思我最初的期望是不切实际的。
I worked around this in the end by manually passing the user-supplied instance of ILogger to wherever it was required in the library codebase. This is satisfactory as I think on reflection my initial expectations were unrealistic.
更多推荐
发布评论