//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
namespace TouchSocket.Core
{
///
/// IContainerExtensions
///
public static class ContainerExtension
{
///
/// 注册单例
///
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, TTo instance)
where TFrom : class
where TTo : class, TFrom
{
RegisterSingleton(container, typeof(TFrom), instance);
return container;
}
///
/// 注册单例
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, object instance)
{
if (instance is null)
{
throw new ArgumentNullException(nameof(instance));
}
RegisterSingleton(container, instance.GetType(), instance);
return container;
}
///
/// 注册单例
///
///
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, string key, TTo instance)
where TFrom : class
where TTo : class, TFrom
{
RegisterSingleton(container, typeof(TFrom), instance, key);
return container;
}
///
/// 注册单例
///
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, Type fromType, object instance, string key = "")
{
container.Register(new DependencyDescriptor(fromType, instance), key);
return container;
}
///
/// 注册单例
///
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, object instance, string key = "")
{
container.Register(new DependencyDescriptor(typeof(TFrom), instance), key);
return container;
}
///
/// 注册单例
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, object instance, string key = "")
{
container.Register(new DependencyDescriptor(instance.GetType(), instance), key);
return container;
}
///
/// 注册单例
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, string key = "")
{
container.Register(new DependencyDescriptor(typeof(TFrom), typeof(TFrom), Lifetime.Singleton), key);
return container;
}
///
/// 注册单例
///
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, Type fromType, Type toType, string key = "")
{
container.Register(new DependencyDescriptor(fromType, toType, Lifetime.Singleton), key);
return container;
}
///
/// 注册单例
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container)
where TFrom : class
where TTO : class, TFrom
{
RegisterSingleton(container, typeof(TFrom), typeof(TTO));
return container;
}
///
/// 注册单例
///
///
///
///
///
///
public static IContainer RegisterSingleton(this IContainer container, string key)
where TFrom : class
where TTO : class, TFrom
{
RegisterSingleton(container, typeof(TFrom), typeof(TTO), key);
return container;
}
#region Transient
///
/// 注册临时映射
///
///
///
///
///
public static IContainer RegisterTransient(this IContainer container)
where TFrom : class
where TTO : class, TFrom
{
RegisterTransient(container, typeof(TFrom), typeof(TTO));
return container;
}
///
/// 注册临时映射
///
///
///
///
public static IContainer RegisterTransient(this IContainer container)
where TFrom : class
{
RegisterTransient(container, typeof(TFrom), typeof(TFrom));
return container;
}
///
/// 注册临时映射
///
///
///
///
///
///
public static IContainer RegisterTransient(this IContainer container, string key)
where TFrom : class
where TTO : class, TFrom
{
RegisterTransient(container, typeof(TFrom), typeof(TTO), key);
return container;
}
///
/// 注册临时映射
///
///
///
///
///
///
public static IContainer RegisterTransient(this IContainer container, Type fromType, Type toType, string key = "")
{
container.Register(new DependencyDescriptor(fromType, toType, Lifetime.Transient), key);
return container;
}
#endregion Transient
#region Scoped
///
/// 注册区域映射
///
///
///
///
///
public static IContainer RegisterScoped(this IContainer container)
where TFrom : class
where TTO : class, TFrom
{
RegisterScoped(container, typeof(TFrom), typeof(TTO));
return container;
}
///
/// 注册区域映射
///
///
///
///
public static IContainer RegisterScoped(this IContainer container)
where TFrom : class
{
RegisterScoped(container, typeof(TFrom), typeof(TFrom));
return container;
}
///
/// 注册区域映射
///
///
///
///
///
///
public static IContainer RegisterScoped(this IContainer container, string key)
where TFrom : class
where TTO : class, TFrom
{
RegisterScoped(container, typeof(TFrom), typeof(TTO), key);
return container;
}
///
/// 注册区域映射
///
///
///
///
///
///
public static IContainer RegisterScoped(this IContainer container, Type fromType, Type toType, string key = "")
{
container.Register(new DependencyDescriptor(fromType, toType, Lifetime.Scoped), key);
return container;
}
#endregion Scoped
///
/// 创建类型对应的实例
///
///
///
///
///
///
public static T Resolve(this IContainerProvider container, object[] ps, string key = "")
{
return (T)container.Resolve(typeof(T), ps, key);
}
///
/// 创建类型对应的实例
///
///
///
///
public static T Resolve(this IContainerProvider container)
{
return Resolve(container, null);
}
///
/// 创建类型对应的实例
///
///
///
///
///
public static T Resolve(this IContainerProvider container, string key)
{
return Resolve(container, null, key);
}
#region Unregister
///
/// 移除注册信息
///
///
///
///
///
public static IContainer Unregister(this IContainer container, Type fromType, string key = "")
{
container.Unregister(new DependencyDescriptor(fromType), key);
return container;
}
///
/// 移除注册信息
///
///
///
///
public static IContainer Unregister(this IContainer container, string key = "")
{
container.Unregister(new DependencyDescriptor(typeof(TFrom)), key);
return container;
}
#endregion Unregister
}
}