GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — The IoC container The IoC container
mambrowv edited this page
This page has moved to docs.servicestack.net/iocServiceStack uses a slightly modified version of Funq - which was adopted because of its excellent performance and memory characteristics. ServiceStack's version of Funq has been enhanced with Expression-based Auto-wiring and lifetime Request Scope. If you so wish, you can still elect to use your favourite IOC by creating an
For each of these features, dependencies are resolved for all parameters in the constructor that has the most arguments as well as all public properties. You can register dependencies so that all the dependencies are automatically auto-wired. Just like the hooks above, once resolved Funq will create a new instance resolving dependencies for the constructor that has the most arguments as well as all public properties. public override void Configure(Container container) { container.RegisterAutoWired<MyType>(); container.RegisterAutoWiredAs<MyType,IMyType>(); } There's also support for registration of run-time types with these APIs below: container.RegisterAutoWiredType(typeof(MyType)); container.RegisterAutoWiredType(typeof(MyType),typeof(IMyType)); container.RegisterAutoWiredTypes(typeof(MyType),typeof(MyType2),typeof(MyType3)); Funq also supports custom creation of instances. When used no auto-wiring is performed and it's left up to you to explicitly resolve all your dependencies. In addition to Auto-wiring Funq allows you to customize the creation of your instance with custom delegates. This is useful when your dependencies require custom configuration. E.g: container.Register(c => new MyType(c.Resolve<IDependency>(), connectionString)); container.Register<IMyType>(c => new MyType(c.Resolve<IDependency>(), connectionString)); container.Register(c => CreateAndInitializeMyType(c.Resolve<IDependency1>(), c.Resolve<IDependency2>)); Other than factories, you can also register custom instances where instead of returning a lambda above you can return an instance: container.Register(new MyType(c.Resolve<IDependency>(), connectionString));
container.Register<T>(c => new Foo(c.Resolve<Some>(), c.Resolve<Other>(), c.Resolve<Dependencies>()); By default all dependencies registered in Funq have singleton scope, where the same instance is injected into all dependencies. This behaviour can be changed by defining the scope explicitly, which is supported with the following APIs: container.Register(c => new MyType()).ReusedWithin(ReuseScope.None); container.RegisterAutoWired<MyType>().ReusedWithin(ReuseScope.None); container.RegisterAutoWiredAs<MyType,IMyType>().ReusedWithin(ReuseScope.None); container.RegisterAutoWiredType(typeof(MyType), ReuseScope.None); container.RegisterAutoWiredType(typeof(MyType), typeof(IMyType), ReuseScope.None);
Whilst ServiceStack's IOC doesn't have a native support for registering types based on a generic type definition, it's easy to use the Runtime Type APIs and register them yourself. E.g the example below will register and autowire all types that implement GetType().Assembly.GetTypes() .Where(x => x.IsOrHasGenericInterfaceTypeOf(typeof(ICommand<>))) .Each(x => container.RegisterAutoWiredType(x)); public interface IResolver { T TryResolve<T>(); } public interface IContainerAdapter : IResolver { T Resolve<T>(); } IKernel kernel = new StandardKernel(); container.Adapter = new NinjectIocAdapter(kernel); Adding a custom ContainerAdapter allows your services to resolve its dependencies from an alternative IOC, in this way they act like a dependency repository where the services are still registered in Funq but all their dependencies are resolved by the ContainerAdapter specified. Dependencies in constructors are resolved by calling Here are some example how to use some popular IoC containers side-by-side with Funq. Of course you're not only limited to the these IoC containers here: public class NinjectIocAdapter : IContainerAdapter { private readonly IKernel kernel; public NinjectIocAdapter(IKernel kernel) { this.kernel = kernel; } public T Resolve<T>() { return this.kernel.Get<T>(); } public T TryResolve<T>() { return this.kernel.CanResolve<T>() ? this.kernel.Get<T>() : default(T); } } Then in the //Create Ninject IoC container IKernel kernel = new StandardKernel(); //Now register all depedencies to your custom IoC container //... //Register Ninject IoC container, so ServiceStack can use it container.Adapter = new NinjectIocAdapter(kernel); public class StructureMapContainerAdapter : IContainerAdapter { public T TryResolve<T>() { return ObjectFactory.TryGetInstance<T>(); } public T Resolve<T>() { return ObjectFactory.TryGetInstance<T>(); } } In //Configure User Defined REST Paths container.Adapter = new StructureMapContainerAdapter(); //Register your dependencies ObjectFactory.Inject(typeof(IFoo), new Foo());
// The "ApplicationAssemblyFilter" is a custom class that just helps to // automate registration to assemblies which match a particular pattern // in the app path public class ApplicationAssemblyFilter : AssemblyFilter { public ApplicationAssemblyFilter() : base(AppDomain.CurrentDomain.BaseDirectory, Assembly.GetExecutingAssembly().GetName().Name + ".*.dll"){} } public class WindsorContainerAdapter : IContainerAdapter, IDisposable { private readonly IWindsorContainer container; public WindsorContainerAdapter() { container = new WindsorContainer().Install(FromAssembly.InThisApplication(), FromAssembly.InDirectory(new ApplicationAssemblyFilter())); } public T TryResolve<T>() { if (container.Kernel.HasComponent(typeof(T))) { return (T)container.Resolve(typeof(T)); } return default(T); } public T Resolve<T>() { return container.Resolve<T>(); } public void Dispose() { container.Dispose(); } } public class AutofacIocAdapter : IContainerAdapter { private readonly IContainer container; public AutofacIocAdapter(IContainer container) { this.container = container; } public T Resolve<T>() { return container.Resolve<T>(); } public T TryResolve<T>() { T result; if (container.TryResolve<T>(out result)) { return result; } return default(T); } } Then in the //Create Autofac builder var builder = new ContainerBuilder(); //Now register all depedencies to your custom IoC container //... //Register Autofac IoC container adapter, so ServiceStack can use it IContainerAdapter adapter = new AutofacIocAdapter(builder.Build()); container.Adapter = adapter; public class SimpleInjectorIocAdapter : IContainerAdapter { private readonly Container container; public SimpleInjectorIocAdapter(Container container) { this.container = container; } public T Resolve<T>() { return (T)this.container.GetInstance(typeof(T)); } public T TryResolve<T>() { var registration = this.container.GetRegistration(typeof(T)); return registration == null ? default(T) : (T)registration.GetInstance(); } } Then in the //Create SimpleInjector IoC container Container simpleContainer = new Container(); //Now register all depedencies to your simpleContainer //... //Register SimpleInjector IoC container, so ServiceStack can use it container.Adapter = new SimpleInjectorIocAdapter (simpleContainer); public class UnityIocAdapter : IContainerAdapter { private readonly IUnityContainer container; public UnityIocAdapter(IUnityContainer container) { this.container = container; } public T Resolve<T>() { return container.Resolve<T>(); } public T TryResolve<T>() { if(container.IsRegistered<T>()) { return container.Resolve<T>(); } return default(T); } } Then in the //Create Unity IoC container var unityContainer = new UnityContainer(); //Now register all depedencies to your custom IoC container //... //Register your Unity IoC container, so ServiceStack can use it container.Adapter = new UnityIocAdapter(unityContainer); The Which will first try to call your ContainerAdapter if it implements So to have resolved services released back into your IOC, implement the public class WindsorContainerAdapter : IContainerAdapter, IRelease { private readonly IWindsorContainer _container; public void Release(object instance) { _container.Release(instance); } } Otherwise you can override default implementation in your
Clone this wiki locally |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |