Commit 2aa84847 authored by 阿星Plus's avatar 阿星Plus

dependency

parent abb12214
...@@ -48,7 +48,10 @@ namespace Plus.EntityFramework ...@@ -48,7 +48,10 @@ namespace Plus.EntityFramework
}); });
} }
return _iocResolver.Resolve<TDbContext>(concreteType); return _iocResolver.Resolve<TDbContext>(new
{
options = CreateOptions<TDbContext>(connectionString, existingConnection)
});
} }
catch (DependencyResolverException ex) catch (DependencyResolverException ex)
{ {
......
using Castle.DynamicProxy; using Castle.DynamicProxy;
using Castle.MicroKernel.Registration; using Castle.MicroKernel.Registration;
using Plus.Event;
using System.Reflection; using System.Reflection;
namespace Plus.Dependency namespace Plus.Dependency
...@@ -8,9 +7,9 @@ namespace Plus.Dependency ...@@ -8,9 +7,9 @@ namespace Plus.Dependency
/// <summary> /// <summary>
/// 用于注册基本依赖项实现 /// 用于注册基本依赖项实现
/// </summary> /// </summary>
public class BasicConventionalRegistrar : IDependencyRegistrar public class BasicConventionalRegistrar : IConventionalDependencyRegistrar
{ {
public void RegisterAssembly(IRegistrationContext context) public void RegisterAssembly(IConventionalRegistrationContext context)
{ {
//Transient //Transient
context.IocManager.IocContainer.Register( context.IocManager.IocContainer.Register(
...@@ -43,16 +42,6 @@ namespace Plus.Dependency ...@@ -43,16 +42,6 @@ namespace Plus.Dependency
.WithService.Self() .WithService.Self()
.LifestyleTransient() .LifestyleTransient()
); );
//IConsumer
context.IocManager.IocContainer.Register(
Classes.FromAssembly(context.Assembly)
.IncludeNonPublicTypes()
.BasedOn(typeof(IConsumer<>))
.If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
.WithService.Self()
.LifestyleTransient()
);
} }
} }
} }
\ No newline at end of file
using Castle.DynamicProxy;
using Plus.Configuration;
namespace Plus.Dependency
{
/// <summary>
/// ConventionalRegistrationConfig
/// </summary>
public class ConventionalRegistrationConfig : DictionaryBasedConfig
{
/// <summary>
/// Install all <see cref="IInterceptor"/> implementations automatically or not.
/// Default: true.
/// </summary>
public bool InstallInstallers { get; set; }
/// <summary>
/// Creates a new <see cref="ConventionalRegistrationConfig"/> object.
/// </summary>
public ConventionalRegistrationConfig()
{
InstallInstallers = true;
}
}
}
\ No newline at end of file
...@@ -2,24 +2,31 @@ ...@@ -2,24 +2,31 @@
namespace Plus.Dependency namespace Plus.Dependency
{ {
public class ConventionalRegistrationContext : IRegistrationContext /// <summary>
/// ConventionalRegistrationContext
/// </summary>
internal class ConventionalRegistrationContext : IConventionalRegistrationContext
{ {
public Assembly Assembly /// <summary>
{ /// Gets the registering Assembly.
get; /// </summary>
private set; public Assembly Assembly { get; private set; }
}
public IIocManager IocManager /// <summary>
{ /// Reference to the IOC Container to register types.
get; /// </summary>
private set; public IIocManager IocManager { get; private set; }
}
/// <summary>
/// Registration configuration.
/// </summary>
public ConventionalRegistrationConfig Config { get; private set; }
internal ConventionalRegistrationContext(Assembly assembly, IIocManager iocManager) internal ConventionalRegistrationContext(Assembly assembly, IIocManager iocManager, ConventionalRegistrationConfig config)
{ {
Assembly = assembly; Assembly = assembly;
IocManager = iocManager; IocManager = iocManager;
Config = config;
} }
} }
} }
\ No newline at end of file
namespace Plus.Dependency
{
/// <summary>
/// IConventionalDependencyRegistrar
/// </summary>
public interface IConventionalDependencyRegistrar
{
void RegisterAssembly(IConventionalRegistrationContext context);
}
}
\ No newline at end of file
using System.Reflection;
namespace Plus.Dependency
{
/// <summary>
/// IConventionalRegistrationContext
/// </summary>
public interface IConventionalRegistrationContext
{
/// <summary>
/// Gets the registering Assembly.
/// </summary>
Assembly Assembly { get; }
/// <summary>
/// Reference to the IOC Container to register types.
/// </summary>
IIocManager IocManager { get; }
/// <summary>
/// Registration configuration.
/// </summary>
ConventionalRegistrationConfig Config { get; }
}
}
\ No newline at end of file
...@@ -9,59 +9,67 @@ namespace Plus.Dependency ...@@ -9,59 +9,67 @@ namespace Plus.Dependency
public interface IIocRegistrar public interface IIocRegistrar
{ {
/// <summary> /// <summary>
/// Add Registrar /// Adds a dependency registrar for conventional registration.
/// </summary> /// </summary>
/// <param name="registrar"></param> /// <param name="registrar">dependency registrar</param>
void AddRegistrar(IDependencyRegistrar registrar); void AddConventionalRegistrar(IConventionalDependencyRegistrar registrar);
/// <summary> /// <summary>
/// Register Assembly /// Registers types of given assembly by all conventional registrars. See <see cref="IocManager.AddConventionalRegistrar"/> method.
/// </summary> /// </summary>
/// <param name="assembly"></param> /// <param name="assembly">Assembly to register</param>
void RegisterAssembly(Assembly assembly); void RegisterAssembly(Assembly assembly);
/// <summary> /// <summary>
/// Register /// Registers types of given assembly by all conventional registrars. See <see cref="IocManager.AddConventionalRegistrar"/> method.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <param name="assembly">Assembly to register</param>
/// <param name="lifeStyle"></param> /// <param name="config">Additional configuration</param>
void Register<T>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where T : class; void RegisterAssembly(Assembly assembly, ConventionalRegistrationConfig config);
/// <summary> /// <summary>
/// Register /// Registers a type as self registration.
/// </summary> /// </summary>
/// <param name="type"></param> /// <typeparam name="T">Type of the class</typeparam>
/// <param name="lifeStyle"></param> /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
void Register<T>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
where T : class;
/// <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>
void Register(Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton); void Register(Type type, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton);
/// <summary> /// <summary>
/// Register /// Registers a type with it's implementation.
/// </summary> /// </summary>
/// <typeparam name="TType"></typeparam> /// <typeparam name="TType"></typeparam>
/// <typeparam name="TImpl"></typeparam> /// <typeparam name="TImpl"></typeparam>
/// <param name="lifeStyle"></param> /// <param name="lifeStyle"></param>
void Register<TType, TImpl>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where TType : class where TImpl : class, TType; void Register<TType, TImpl>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton)
where TType : class
where TImpl : class, TType;
/// <summary> /// <summary>
/// Register /// Registers a type with it's implementation.
/// </summary> /// </summary>
/// <param name="type"></param> /// <param name="type">Type of the class</param>
/// <param name="impl"></param> /// <param name="impl">The type that implements <paramref name="type"/></param>
/// <param name="lifeStyle"></param> /// <param name="lifeStyle">Lifestyle of the objects of this type</param>
void Register(Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton); void Register(Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton);
/// <summary> /// <summary>
/// 是否注册了给定的类型 /// Checks whether given type is registered before.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <param name="type">Type to check</param>
/// <returns></returns> bool IsRegistered(Type type);
bool IsRegistered<T>();
/// <summary> /// <summary>
/// 是否注册了给定的类型 /// Checks whether given type is registered before.
/// </summary> /// </summary>
/// <param name="type"></param> /// <typeparam name="TType">Type to check</typeparam>
/// <returns></returns> bool IsRegistered<TType>();
bool IsRegistered(Type type);
} }
} }
\ No newline at end of file
...@@ -8,60 +8,98 @@ namespace Plus.Dependency ...@@ -8,60 +8,98 @@ namespace Plus.Dependency
public interface IIocResolver public interface IIocResolver
{ {
/// <summary> /// <summary>
/// 从IOC容器中获取对象 /// Gets an object from IOC container.
/// Returning object must be Released (see <see cref="Release"/>) after usage.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T">Type of the object to get</typeparam>
/// <returns></returns> /// <returns>The object instance</returns>
T Resolve<T>(); T Resolve<T>();
/// <summary> /// <summary>
/// 从IOC容器中获取对象 /// Gets an object from IOC container.
/// Returning object must be Released (see <see cref="Release"/>) after usage.
/// </summary> /// </summary>
/// <param name="type"></param> /// <typeparam name="T">Type of the object to cast</typeparam>
/// <returns></returns> /// <param name="type">Type of the object to resolve</param>
/// <returns>The object instance</returns>
T Resolve<T>(Type type);
/// <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 get</typeparam>
/// <param name="argumentsAsAnonymousType">Constructor arguments</param>
/// <returns>The object instance</returns>
T Resolve<T>(object argumentsAsAnonymousType);
/// <summary>
/// Gets an object from IOC container.
/// Returning object must be Released (see <see cref="Release"/>) after usage.
/// </summary>
/// <param name="type">Type of the object to get</param>
/// <returns>The object instance</returns>
object Resolve(Type type); object Resolve(Type type);
/// <summary> /// <summary>
/// 从IOC容器中获取对象 /// Gets an object from IOC container.
/// Returning object must be Released (see <see cref="Release"/>) after usage.
/// </summary> /// </summary>
/// <param name="type"></param> /// <param name="type">Type of the object to get</param>
/// <param name="argumentsAsAnonymousType"></param> /// <param name="argumentsAsAnonymousType">Constructor arguments</param>
/// <returns></returns> /// <returns>The object instance</returns>
object Resolve(Type type, object argumentsAsAnonymousType); object Resolve(Type type, object argumentsAsAnonymousType);
/// <summary> /// <summary>
/// 从IOC容器中获取对象 /// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T">Type of the objects to resolve</typeparam>
/// <param name="argumentsAsAnonymousType"></param> /// <returns>Object instances</returns>
/// <returns></returns> T[] ResolveAll<T>();
T Resolve<T>(object argumentsAsAnonymousType);
/// <summary> /// <summary>
/// 从IOC容器中获取对象 /// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T">Type of the objects to resolve</typeparam>
/// <returns></returns> /// <param name="argumentsAsAnonymousType">Constructor arguments</param>
T[] ResolveAll<T>(); /// <returns>Object instances</returns>
T[] ResolveAll<T>(object argumentsAsAnonymousType);
/// <summary> /// <summary>
/// 是否注册了给定的类型 /// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <param name="type">Type of the objects to resolve</param>
/// <returns></returns> /// <returns>Object instances</returns>
bool IsRegistered<T>(); object[] ResolveAll(Type type);
/// <summary> /// <summary>
/// 是否注册了给定的类型 /// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary> /// </summary>
/// <param name="type"></param> /// <param name="type">Type of the objects to resolve</param>
/// <returns></returns> /// <param name="argumentsAsAnonymousType">Constructor arguments</param>
bool IsRegistered(Type type); /// <returns>Object instances</returns>
object[] ResolveAll(Type type, object argumentsAsAnonymousType);
/// <summary> /// <summary>
/// 释放预解析对象 /// Releases a pre-resolved object. See Resolve methods.
/// </summary> /// </summary>
/// <param name="obj"></param> /// <param name="obj">Object to be released</param>
void Release(object obj); void Release(object obj);
/// <summary>
/// Checks whether given type is registered before.
/// </summary>
/// <param name="type">Type to check</param>
bool IsRegistered(Type type);
/// <summary>
/// Checks whether given type is registered before.
/// </summary>
/// <typeparam name="T">Type to check</typeparam>
bool IsRegistered<T>();
} }
} }
\ No newline at end of file
This diff is collapsed.
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Castle.Core" Version="4.4.0" /> <PackageReference Include="Castle.Core" Version="4.4.0" />
<PackageReference Include="Castle.LoggingFacility" Version="4.1.1" /> <PackageReference Include="Castle.LoggingFacility" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
......
...@@ -18,7 +18,7 @@ namespace Plus ...@@ -18,7 +18,7 @@ namespace Plus
{ {
public override void PreInitialize() public override void PreInitialize()
{ {
IocManager.AddRegistrar(new BasicConventionalRegistrar()); IocManager.AddConventionalRegistrar(new BasicConventionalRegistrar());
ConfigureCaches(); ConfigureCaches();
AddIgnoredTypes(); AddIgnoredTypes();
AddMethodParameterValidators(); AddMethodParameterValidators();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment