What do you do when you need to deregister/delete a registration item from the IOC container?
Someone once asked stackoverflow how to deregister a registration item from unity. Someone answered this question as "interesting. Why are you doing this ?", Some people have proposed some alternative solutions, suchLifetimeManager
And so on. These are not fundamental solutions. Because Service Registration/cancellation is a container-level process, it will certainly involve the creation/cleaning of some intermediate objects (dispose) and coordination between different objects, it should have been supported by containers.
Unfortunately, this function is rarely provided in most common IOC containers. As far as I know, only one castle Windsor has provided the unregister function. Later, it was removed in version 3.0 because these methods (unregister) the methods were implementation of "remove component from the container" feature which was flawed and problematic, hecen was scraped ).
This is the case with regard to the service logout/deletion function. As for the service update function (register a new service after the service is canceled to replace and update the original service), let alone.
Will the service not expire (obsolete? Is it really not necessary to log out/update the service once it is registered? I don't think so. I can think of at least two scenarios: The plug-in environment (in the plug-in environment, with the update of plug-ins, services may need to be updated) and the service-oriented environment (in the service-oriented environment, the service may be enabled, disabled, or updated at any time ).
Therefore, I decided to provide the deregister/update function for registration items in my. IOC framework. The usage is simple. See the following sample code:
using System;using System.Diagnostics;using My.Ioc;namespace ServiceUpdate{ #region Test Types public interface IService { string Name { get; } } public abstract class Service : IService { public string Name { get { return GetType().Name; } } } public class Service1 : Service { } public class Service2 : Service { } public interface IServiceConsumer { IService Service { get; } } public class ServiceConsumer : IServiceConsumer { readonly IService _service; public ServiceConsumer(IService service) { _service = service; } public IService Service { get { return _service; } } } #endregion class Program { static void Main(string[] args) { IObjectRegistration serviceRegistration; IObjectContainer container = new ObjectContainer(false); container.Register<IService, Service1>().Return(out serviceRegistration); container.Register<IServiceConsumer, ServiceConsumer>(); container.CommitRegistrations(); var consumer = container.Resolve<IServiceConsumer>(); Debug.Assert(consumer != null); Debug.Assert(consumer.Service != null); Debug.Assert(consumer.Service.Name == "Service1"); container.Unregister(serviceRegistration); container.Register<IService, Service2>().Return(out serviceRegistration); container.CommitRegistrations(); consumer = container.Resolve<IServiceConsumer>(); Debug.Assert(consumer != null); Debug.Assert(consumer.Service != null); Debug.Assert(consumer.Service.Name == "Service2"); Console.ReadLine(); } }}
View code
The source code can be downloaded here. The compressed package contains the source code of my. IOC framework and the source code of this example and other examples.
My. IOC sample code -- Registration and update of registration items