From 28b414e282ef536fcbf4f5f1f1ae77ca3efb6265 Mon Sep 17 00:00:00 2001 From: Caskia Date: Wed, 28 Aug 2019 12:23:15 +0800 Subject: [PATCH] autofac container settable --- .gitignore | 1 + src/ECommon.Autofac/AutofacObjectContainer.cs | 145 ++++++++++-------- .../ObjectContainerExtensions.cs | 16 ++ 3 files changed, 100 insertions(+), 62 deletions(-) create mode 100644 src/ECommon.Autofac/ObjectContainerExtensions.cs diff --git a/.gitignore b/.gitignore index cee7dd2..c3fc710 100644 --- a/.gitignore +++ b/.gitignore @@ -156,3 +156,4 @@ $RECYCLE.BIN/ # Mac desktop service store files .DS_Store /src/.vs/ECommon/v15 +/src/.vs/ECommon/v16/Server/sqlite3 diff --git a/src/ECommon.Autofac/AutofacObjectContainer.cs b/src/ECommon.Autofac/AutofacObjectContainer.cs index 1cf9e02..dd4f640 100644 --- a/src/ECommon.Autofac/AutofacObjectContainer.cs +++ b/src/ECommon.Autofac/AutofacObjectContainer.cs @@ -1,6 +1,6 @@ -using System; -using Autofac; +using Autofac; using ECommon.Components; +using System; namespace ECommon.Autofac { @@ -9,13 +9,14 @@ namespace ECommon.Autofac public class AutofacObjectContainer : IObjectContainer { private readonly ContainerBuilder _containerBuilder; - private IContainer _container; + private ILifetimeScope _container; /// Default constructor. /// public AutofacObjectContainer() : this(new ContainerBuilder()) { } + /// Parameterized constructor. /// public AutofacObjectContainer(ContainerBuilder containerBuilder) @@ -23,22 +24,23 @@ public AutofacObjectContainer(ContainerBuilder containerBuilder) _containerBuilder = containerBuilder; } - /// Represents the iner autofac container builder. + /// Represents the inner autofac container. /// - public ContainerBuilder ContainerBuilder + public ILifetimeScope Container { get { - return _containerBuilder; + return _container; } } - /// Represents the inner autofac container. + + /// Represents the iner autofac container builder. /// - public IContainer Container + public ContainerBuilder ContainerBuilder { get { - return _container; + return _containerBuilder; } } @@ -48,6 +50,45 @@ public void Build() { _container = _containerBuilder.Build(); } + + /// Register a implementer type as a service implementation. + /// + /// The service type. + /// The implementer type. + /// The service name. + /// The life cycle of the implementer type. + public void Register(string serviceName = null, LifeStyle life = LifeStyle.Singleton) + where TService : class + where TImplementer : class, TService + { + var registrationBuilder = _containerBuilder.RegisterType().As(); + if (serviceName != null) + { + registrationBuilder.Named(serviceName); + } + if (life == LifeStyle.Singleton) + { + registrationBuilder.SingleInstance(); + } + } + + /// Register a implementer type instance as a service implementation. + /// + /// The service type. + /// The implementer type. + /// The implementer type instance. + /// The service name. + public void RegisterInstance(TImplementer instance, string serviceName = null) + where TService : class + where TImplementer : class, TService + { + var registrationBuilder = _containerBuilder.RegisterInstance(instance).As().SingleInstance(); + if (serviceName != null) + { + registrationBuilder.Named(serviceName); + } + } + /// Register a implementation type. /// /// The implementation type. @@ -80,6 +121,7 @@ public void RegisterType(Type implementationType, string serviceName = null, Lif } } } + /// Register a implementer type as a service implementation. /// /// The service type. @@ -113,58 +155,54 @@ public void RegisterType(Type serviceType, Type implementationType, string servi } } } - /// Register a implementer type as a service implementation. + + /// Resolve a service. /// /// The service type. - /// The implementer type. - /// The service name. - /// The life cycle of the implementer type. - public void Register(string serviceName = null, LifeStyle life = LifeStyle.Singleton) - where TService : class - where TImplementer : class, TService + /// The component instance that provides the service. + public TService Resolve() where TService : class { - var registrationBuilder = _containerBuilder.RegisterType().As(); - if (serviceName != null) - { - registrationBuilder.Named(serviceName); - } - if (life == LifeStyle.Singleton) - { - registrationBuilder.SingleInstance(); - } + return _container.Resolve(); } - /// Register a implementer type instance as a service implementation. + + /// Resolve a service. /// - /// The service type. - /// The implementer type. - /// The implementer type instance. - /// The service name. - public void RegisterInstance(TImplementer instance, string serviceName = null) - where TService : class - where TImplementer : class, TService + /// The service type. + /// The component instance that provides the service. + public object Resolve(Type serviceType) { - var registrationBuilder = _containerBuilder.RegisterInstance(instance).As().SingleInstance(); - if (serviceName != null) - { - registrationBuilder.Named(serviceName); - } + return _container.Resolve(serviceType); } + /// Resolve a service. /// /// The service type. + /// The service name. /// The component instance that provides the service. - public TService Resolve() where TService : class + public TService ResolveNamed(string serviceName) where TService : class { - return _container.Resolve(); + return _container.ResolveNamed(serviceName); } + /// Resolve a service. /// + /// The service name. /// The service type. /// The component instance that provides the service. - public object Resolve(Type serviceType) + public object ResolveNamed(string serviceName, Type serviceType) { - return _container.Resolve(serviceType); + return _container.ResolveNamed(serviceName, serviceType); + } + + /// + /// Set container + /// + /// container + public void SetContainer(ILifetimeScope container) + { + _container = container; } + /// Try to retrieve a service from the container. /// /// The service type to resolve. @@ -174,6 +212,7 @@ public bool TryResolve(out TService instance) where TService : class { return _container.TryResolve(out instance); } + /// Try to retrieve a service from the container. /// /// The service type to resolve. @@ -183,24 +222,7 @@ public bool TryResolve(Type serviceType, out object instance) { return _container.TryResolve(serviceType, out instance); } - /// Resolve a service. - /// - /// The service type. - /// The service name. - /// The component instance that provides the service. - public TService ResolveNamed(string serviceName) where TService : class - { - return _container.ResolveNamed(serviceName); - } - /// Resolve a service. - /// - /// The service name. - /// The service type. - /// The component instance that provides the service. - public object ResolveNamed(string serviceName, Type serviceType) - { - return _container.ResolveNamed(serviceName, serviceType); - } + /// Try to retrieve a service from the container. /// /// The name of the service to resolve. @@ -212,5 +234,4 @@ public bool TryResolveNamed(string serviceName, Type serviceType, out object ins return _container.TryResolveNamed(serviceName, serviceType, out instance); } } -} - +} \ No newline at end of file diff --git a/src/ECommon.Autofac/ObjectContainerExtensions.cs b/src/ECommon.Autofac/ObjectContainerExtensions.cs new file mode 100644 index 0000000..0bfb936 --- /dev/null +++ b/src/ECommon.Autofac/ObjectContainerExtensions.cs @@ -0,0 +1,16 @@ +using Autofac; +using ECommon.Components; +using System; + +namespace ECommon.Autofac +{ + public static class ObjectContainerExtensions + { + public static void SetContainer(this IObjectContainer objectContainer, ILifetimeScope container) + { + if (!(objectContainer is AutofacObjectContainer autofacObjectContainer)) + throw new InvalidOperationException($"instance of {nameof(ObjectContainer)} is not of type {nameof(AutofacObjectContainer)}"); + autofacObjectContainer.SetContainer(container); + } + } +} \ No newline at end of file