Unity: register instance at runtime
I can't seem to figure out this one. I have a component which depends on a service. The service has a URL which is only known at runtime. How can I get a Component instance using the container?
开发者_运维技巧public interface IService
{
Uri Url { get; set; }
}
public class Service : IService
{
public Uri Url { get; set; }
}
public class Component : IComponent
{
public Component(IService service) {... }
}
I can only think of using Unity as Service Locator to get the container on demand and register my runtime instance:
var service = new Service { Url = new Uri("http://...") };
Container.Resolve<IUnityContainer>().RegisterInstance<IService>(service);
var component = Container.Resolve<IComponent>();
But I really want to avoid SL. Is there another way?
精彩评论