In order to get started with Agnostic IoC follow these simple steps.
Plugin code
In your plugin assemblies, simply download the Agnostic.IoC.Core nuget package.
Consumer code
In your consuming assemblies, you will simply need to download the Agnostic.IoC.YourContainerOfChoice nuget package, if your container isn’t currently supported, please comment and we will endeavor to help you make it so.
Container Manager
In your consuming code, you can access the containers at any time by using the ContainerManager class, this accepts several constructor arguments. The ContainerManager can maintain references to multiple ContainerAdapter instances utilising a string key to switch between them.
The default constructor utilises String.Empty as the key and attempts to scan your loaded assemblies for a class inheriting from IContainerAdapter with the Name property of String.Empty (the default for container adapters).
IContainerManager containerManager = new ContainerManager();
You may also instantiate a ContainerManager with a known container type:
// register an autofac container adapter. var adapter = new AutofacContainerAdapter("MyContainer"); adapter.Register<IDependantClass, DependantClass2>(); IContainerManager containerManager = new ContainerManager(adapter); Assert.IsNotNull(containerManager.Resolve<IDependantClass>()); // now my container adapter is known, I can refer to it in future using the string key var containerManager2 = new ContainerManager("MyContainer"); Assert.IsNotNull(containerManager2.Resolve<IDependantClass>());
ContainerAdapter classes generally support a dependency injected container, so it allows pre-existing containers to be used.
// Register components with the original container IWindsorContainer container = new WindsorContainer(); container.Register(Component.For<IDependantClass>().ImplementedBy<DependantClass>()); // it isn't necessary to use a container key, but this shows you can. ContainerManager containerManager = new ContainerManager(new WindsorContainerAdapter("MyContainer", container)); IDependantClass dependency = containerManager.Resolve<IDependantClass>(); Assert.IsNotNull(dependency); Assert.AreEqual(typeof(DependantClass), dependency.GetType());
Individual Registrations
Individual registrations can be made against Agnostic IoC containers by calling:
IContainerManager containerManager = new ContainerManager(new AutofacContainerAdapter()); containerManager.Adapter.Register<IDependantClass, DependantClass>();
Group Registrations
Group Registrations can be contained within your plugin codebase by creating a class deriving from IContainerManagerGroupRegisration (see the example below).
public class TestGroupRegistration : IContainerManagerGroupRegistration { public void RegisterComponents(IContainerAdapter containerAdapter) { containerAdapter.Register<IDependantClass, DependantClass>(); containerAdapter.Register<IDependantClass, DependantClass2>("new name"); } }
You may register this with the container manager by calling:
containerManager.Adapter.RegisterGroup(groupRegistration);
Resolution
Resolving components can be performed from the ContainerManager or ContainerAdapter objects using the ‘Resolve’ methods.
Pingback: Agnostic IoC – Released | CardinalCore