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

dependency

parent abb12214
......@@ -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)
{
......
using Castle.DynamicProxy;
using Castle.MicroKernel.Registration;
using Plus.Event;
using System.Reflection;
namespace Plus.Dependency
......@@ -8,9 +7,9 @@ namespace Plus.Dependency
/// <summary>
/// 用于注册基本依赖项实现
/// </summary>
public class BasicConventionalRegistrar : IDependencyRegistrar
public class BasicConventionalRegistrar : IConventionalDependencyRegistrar
{
public void RegisterAssembly(IRegistrationContext context)
public void RegisterAssembly(IConventionalRegistrationContext context)
{
//Transient
context.IocManager.IocContainer.Register(
......@@ -43,16 +42,6 @@ namespace Plus.Dependency
.WithService.Self()
.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 @@
namespace Plus.Dependency
{
public class ConventionalRegistrationContext : IRegistrationContext
/// <summary>
/// ConventionalRegistrationContext
/// </summary>
internal class ConventionalRegistrationContext : IConventionalRegistrationContext
{
public Assembly Assembly
{
get;
private set;
}
/// <summary>
/// Gets the registering Assembly.
/// </summary>
public Assembly Assembly { get; private set; }
public IIocManager IocManager
{
get;
private set;
}
/// <summary>
/// Reference to the IOC Container to register types.
/// </summary>
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;
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
public interface IIocRegistrar
{
/// <summary>
/// Add Registrar
/// Adds a dependency registrar for conventional registration.
/// </summary>
/// <param name="registrar"></param>
void AddRegistrar(IDependencyRegistrar registrar);
/// <param name="registrar">dependency registrar</param>
void AddConventionalRegistrar(IConventionalDependencyRegistrar registrar);
/// <summary>
/// Register Assembly
/// Registers types of given assembly by all conventional registrars. See <see cref="IocManager.AddConventionalRegistrar"/> method.
/// </summary>
/// <param name="assembly"></param>
/// <param name="assembly">Assembly to register</param>
void RegisterAssembly(Assembly assembly);
/// <summary>
/// Register
/// Registers types of given assembly by all conventional registrars. See <see cref="IocManager.AddConventionalRegistrar"/> method.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="lifeStyle"></param>
void Register<T>(DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton) where T : class;
/// <param name="assembly">Assembly to register</param>
/// <param name="config">Additional configuration</param>
void RegisterAssembly(Assembly assembly, ConventionalRegistrationConfig config);
/// <summary>
/// Register
/// Registers a type as self registration.
/// </summary>
/// <param name="type"></param>
/// <param name="lifeStyle"></param>
/// <typeparam name="T">Type of the class</typeparam>
/// <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);
/// <summary>
/// Register
/// Registers a type with it's implementation.
/// </summary>
/// <typeparam name="TType"></typeparam>
/// <typeparam name="TImpl"></typeparam>
/// <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>
/// Register
/// Registers a type with it's implementation.
/// </summary>
/// <param name="type"></param>
/// <param name="impl"></param>
/// <param name="lifeStyle"></param>
/// <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>
void Register(Type type, Type impl, DependencyLifeStyle lifeStyle = DependencyLifeStyle.Singleton);
/// <summary>
/// 是否注册了给定的类型
/// Checks whether given type is registered before.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
bool IsRegistered<T>();
/// <param name="type">Type to check</param>
bool IsRegistered(Type type);
/// <summary>
/// 是否注册了给定的类型
/// Checks whether given type is registered before.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
bool IsRegistered(Type type);
/// <typeparam name="TType">Type to check</typeparam>
bool IsRegistered<TType>();
}
}
\ No newline at end of file
......@@ -8,60 +8,98 @@ namespace Plus.Dependency
public interface IIocResolver
{
/// <summary>
/// 从IOC容器中获取对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// 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>
/// <returns>The object instance</returns>
T Resolve<T>();
/// <summary>
/// 从IOC容器中获取对象
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
/// 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>
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);
/// <summary>
/// 从IOC容器中获取对象
/// </summary>
/// <param name="type"></param>
/// <param name="argumentsAsAnonymousType"></param>
/// <returns></returns>
/// 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>
/// <param name="argumentsAsAnonymousType">Constructor arguments</param>
/// <returns>The object instance</returns>
object Resolve(Type type, object argumentsAsAnonymousType);
/// <summary>
/// 从IOC容器中获取对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="argumentsAsAnonymousType"></param>
/// <returns></returns>
T Resolve<T>(object argumentsAsAnonymousType);
/// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary>
/// <typeparam name="T">Type of the objects to resolve</typeparam>
/// <returns>Object instances</returns>
T[] ResolveAll<T>();
/// <summary>
/// 从IOC容器中获取对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T[] ResolveAll<T>();
/// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary>
/// <typeparam name="T">Type of the objects to resolve</typeparam>
/// <param name="argumentsAsAnonymousType">Constructor arguments</param>
/// <returns>Object instances</returns>
T[] ResolveAll<T>(object argumentsAsAnonymousType);
/// <summary>
/// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary>
/// <param name="type">Type of the objects to resolve</param>
/// <returns>Object instances</returns>
object[] ResolveAll(Type type);
/// <summary>
/// Gets all implementations for given type.
/// Returning objects must be Released (see <see cref="Release"/>) after usage.
/// </summary>
/// <param name="type">Type of the objects to resolve</param>
/// <param name="argumentsAsAnonymousType">Constructor arguments</param>
/// <returns>Object instances</returns>
object[] ResolveAll(Type type, object argumentsAsAnonymousType);
/// <summary>
/// 是否注册了给定的类型
/// Releases a pre-resolved object. See Resolve methods.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
bool IsRegistered<T>();
/// <param name="obj">Object to be released</param>
void Release(object obj);
/// <summary>
/// 是否注册了给定的类型
/// Checks whether given type is registered before.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
/// <param name="type">Type to check</param>
bool IsRegistered(Type type);
/// <summary>
/// 释放预解析对象
/// Checks whether given type is registered before.
/// </summary>
/// <param name="obj"></param>
void Release(object obj);
/// <typeparam name="T">Type to check</typeparam>
bool IsRegistered<T>();
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -32,7 +32,7 @@
<ItemGroup>
<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.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.2.0" />
......
......@@ -18,7 +18,7 @@ namespace Plus
{
public override void PreInitialize()
{
IocManager.AddRegistrar(new BasicConventionalRegistrar());
IocManager.AddConventionalRegistrar(new BasicConventionalRegistrar());
ConfigureCaches();
AddIgnoredTypes();
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