Sunday, May 1, 2011

.NET IOC container that allows removal of registrations

I'm only really familiar with AutoFac, and I asked if the feature detailed below was possible with it, and it seemed 'not exactly'. Do you know a container with which this would be possible?

[TestFixture]
public class SomeCunningTests
{
    class SomeType        {        }

    [Test]
    public void TestRegistrationScope()
    {
        var container = new IocContainerOfSomeKind();
        using (new RegistrationScope(container, new SomeType()))
        {
            Assert.IsTrue(container.IsRegistered(typeof(SomeType)));
        }
        Assert.IsFalse(container.IsRegistered(typeof(SomeType)));
    }

    public class RegistrationScope<T> : IDisposable
    {
        public RegistrationScope(IocContainerOfSomeKind container, T instance)
        {
            //code here for registering instance
        }
        public void Dispose()
        {
            //unregister instance somehow
        }
    }
}
From stackoverflow
  • MEF would work with this scenario. I believe all you'd need to do is use an AggregateCatalog for your catalog, which provides the ability to add to and remove from your catalog at runtime, since it exposes a ComposablePartCatalogCollection.

    The catalog is basically the registry of types that's used for composition.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.