| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using Castle.DynamicProxy;
- using Castle.MicroKernel;
- using Castle.MicroKernel.Registration;
- using Castle.Windsor;
- using Castle.Windsor.Installer;
- using Castle.Windsor.Proxy;
- namespace Abp.Dependency
- {
- /// <summary>
- /// This class is used to directly perform dependency injection tasks.
- /// </summary>
- public class IocManager : IIocManager
- {
- /// <summary>
- /// The Singleton instance.
- /// </summary>
- public static IocManager Instance { get; private set; }
- /// <summary>
- /// Singletone instance for Castle ProxyGenerator.
- /// From Castle.Core documentation it is highly recomended to use single instance of ProxyGenerator to avoid memoryleaks and performance issues
- /// Follow next links for more details:
- /// <a href="https://github.com/castleproject/Core/blob/master/docs/dynamicproxy.md">Castle.Core documentation</a>,
- /// <a href="http://kozmic.net/2009/07/05/castle-dynamic-proxy-tutorial-part-xii-caching/">Article</a>
- /// </summary>
- private static readonly ProxyGenerator ProxyGeneratorInstance = new ProxyGenerator();
- /// <summary>
- /// Reference to the Castle Windsor Container.
- /// </summary>
- public IWindsorContainer IocContainer { get; private set; }
- /// <summary>
- /// List of all registered conventional registrars.
- /// </summary>
- private readonly List<IConventionalDependencyRegistrar> _conventionalRegistrars;
- static IocManager()
- {
- Instance = new IocManager();
- }
- /// <summary>
- /// Creates a new <see cref="IocManager"/> object.
- /// Normally, you don't directly instantiate an <see cref="IocManager"/>.
- /// This may be useful for test purposes.
- /// </summary>
- public IocManager()
- {
- IocContainer = CreateContainer();
- _conventionalRegistrars = new List<IConventionalDependencyRegistrar>();
- //Register self!
- IocContainer.Register(
- Component
- .For<IocManager, IIocManager, IIocRegistrar, IIocResolver>()
- .Instance(this)
- );
- }
- protected virtual IWindsorContainer CreateContainer()
- {
- return new WindsorContainer(new DefaultProxyFactory(ProxyGeneratorInstance));
- }
- /// <summary>
- /// Adds a dependency registrar for conventional registration.
- /// </summary>
- /// <param name="registrar">dependency registrar</param>
- public void AddConventionalRegistrar(IConventionalDependencyRegistrar registrar)
- {
- _conventionalRegistrars.Add(registrar);
- }
- /// <summary>
- /// Registers types of given assembly by all conventional registrars. See <see cref="AddConventionalRegistrar"/> method.
- /// </summary>
- /// <param name="assembly">Assembly to register</param>
- public void RegisterAssemblyByConvention(Assembly assembly)
- {
- RegisterAssemblyByConvention(assembly, new ConventionalRegistrationConfig());
- }
- /// <summary>
- /// Registers types of given assembly by all conventional registrars. See <see cref="AddConventionalRegistrar"/> method.
- /// </summary>
- /// <param name="assembly">Assembly to register</param>
- /// <param name="config">Additional configuration</param>
- public void RegisterAssemblyByConvention(Assembly assembly, ConventionalRegistrationConfig config)
- {
- var context = new ConventionalRegistrationContext(assembly, this, config);
- foreach (var registerer in _conventionalRegistrars)
- {
- registerer.RegisterAssembly(context);
- }
- if (config.InstallInstallers)
- {
- IocContainer.Install(FromAssembly.Instance(assembly));
- }
- }
- /// <summary>
- /// Registers a type as self registration.
- /// </summary>
- /// <typeparam name="TType">Type of the class</typeparam>
- /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
- public void Register<TType>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where TType : class
- {
- IocContainer.Register(ApplyLifestyle(Component.For<TType>(), lifeStyle));
- }
- /// <summary>
- /// Registers a type as self registration.
- /// </summary>
- /// <param name="type">Type of the class</param>
- /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
- public void Register(Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
- {
- IocContainer.Register(ApplyLifestyle(Component.For(type), lifeStyle));
- }
- /// <summary>
- /// Registers a type with it's implementation.
- /// </summary>
- /// <typeparam name="TType">Registering type</typeparam>
- /// <typeparam name="TImpl">The type that implements <see cref="TType"/></typeparam>
- /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
- public void Register<TType, TImpl>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
- where TType : class
- where TImpl : class, TType
- {
- IocContainer.Register(ApplyLifestyle(Component.For<TType, TImpl>().ImplementedBy<TImpl>(), lifeStyle));
- }
- /// <summary>
- /// Registers a type with it's implementation.
- /// </summary>
- /// <param name="type">Type of the class</param>
- /// <param name="impl">The type that implements <paramref name="type"/></param>
- /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
- public void Register(Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
- {
- IocContainer.Register(ApplyLifestyle(Component.For(type, impl).ImplementedBy(impl), lifeStyle));
- }
- /// <summary>
- /// Checks whether given type is registered before.
- /// </summary>
- /// <param name="type">Type to check</param>
- public bool IsRegistered(Type type)
- {
- return IocContainer.Kernel.HasComponent(type);
- }
- /// <summary>
- /// Checks whether given type is registered before.
- /// </summary>
- /// <typeparam name="TType">Type to check</typeparam>
- public bool IsRegistered<TType>()
- {
- return IocContainer.Kernel.HasComponent(typeof(TType));
- }
- /// <summary>
- /// Gets an object from IOC container.
- /// Returning object must be Released (see <see cref="IIocResolver.Release"/>) after usage.
- /// </summary>
- /// <typeparam name="T">Type of the object to get</typeparam>
- /// <returns>The instance object</returns>
- public T Resolve<T>()
- {
- return IocContainer.Resolve<T>();
- }
- /// <summary>
- /// Gets an object from IOC container.
- /// Returning object must be Released (see <see cref="Release"/>) after usage.
- /// </summary>
- /// <typeparam name="T">Type of the object to cast</typeparam>
- /// <param name="type">Type of the object to resolve</param>
- /// <returns>The object instance</returns>
- public T Resolve<T>(Type type)
- {
- return (T)IocContainer.Resolve(type);
- }
- /// <summary>
- /// Gets an object from IOC container.
- /// Returning object must be Released (see <see cref="IIocResolver.Release"/>) after usage.
- /// </summary>
- /// <typeparam name="T">Type of the object to get</typeparam>
- /// <param name="argumentsAsAnonymousType">Constructor arguments</param>
- /// <returns>The instance object</returns>
- public T Resolve<T>(object argumentsAsAnonymousType)
- {
- return IocContainer.Resolve<T>(argumentsAsAnonymousType);
- }
- /// <summary>
- /// Gets an object from IOC container.
- /// Returning object must be Released (see <see cref="IIocResolver.Release"/>) after usage.
- /// </summary>
- /// <param name="type">Type of the object to get</param>
- /// <returns>The instance object</returns>
- public object Resolve(Type type)
- {
- return IocContainer.Resolve(type);
- }
- /// <summary>
- /// Gets an object from IOC container.
- /// Returning object must be Released (see <see cref="IIocResolver.Release"/>) after usage.
- /// </summary>
- /// <param name="type">Type of the object to get</param>
- /// <param name="argumentsAsAnonymousType">Constructor arguments</param>
- /// <returns>The instance object</returns>
- public object Resolve(Type type, object argumentsAsAnonymousType)
- {
- return IocContainer.Resolve(type, argumentsAsAnonymousType);
- }
- ///<inheritdoc/>
- public T[] ResolveAll<T>()
- {
- return IocContainer.ResolveAll<T>();
- }
- ///<inheritdoc/>
- public T[] ResolveAll<T>(object argumentsAsAnonymousType)
- {
- return IocContainer.ResolveAll<T>(argumentsAsAnonymousType);
- }
- ///<inheritdoc/>
- public object[] ResolveAll(Type type)
- {
- return IocContainer.ResolveAll(type).Cast<object>().ToArray();
- }
- ///<inheritdoc/>
- public object[] ResolveAll(Type type, object argumentsAsAnonymousType)
- {
- return IocContainer.ResolveAll(type, argumentsAsAnonymousType).Cast<object>().ToArray();
- }
- /// <summary>
- /// Releases a pre-resolved object. See Resolve methods.
- /// </summary>
- /// <param name="obj">Object to be released</param>
- public void Release(object obj)
- {
- IocContainer.Release(obj);
- }
- /// <inheritdoc/>
- public void Dispose()
- {
- IocContainer.Dispose();
- }
- private static ComponentRegistration<T> ApplyLifestyle<T>(ComponentRegistration<T> registration, DependencyLifeStyle lifeStyle)
- where T : class
- {
- switch (lifeStyle)
- {
- case DependencyLifeStyle.Transient:
- return registration.LifestyleTransient();
- case DependencyLifeStyle.Singleton:
- return registration.LifestyleSingleton();
- default:
- return registration;
- }
- }
- }
- }
|