AutoRegistDependency 1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package AutoRegistDependency --version 1.0.0
NuGet\Install-Package AutoRegistDependency -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoRegistDependency" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoRegistDependency --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AutoRegistDependency, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AutoRegistDependency as a Cake Addin #addin nuget:?package=AutoRegistDependency&version=1.0.0 // Install AutoRegistDependency as a Cake Tool #tool nuget:?package=AutoRegistDependency&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AutoRegister
介绍
AutoFac二次封装,实现根据特性自动注入功能
使用说明
- 注册服务 program.cs文件中 (.net6以上)
builder.Host.ConfigureContainer<ContainerBuilder>((hostBuilderContext, containerBuilder) =>
{
new Register()
//.SetDefaultLifeTime() //设置所有实例的默认生命周期,默认scope
.RegisterType(containerBuilder);
});
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
**如果控制器需要使用属性注入,需要使autofac接管controller,需添加以下代码**
builder.Services.AddControllers().AddControllersAsServices();//控制器生成作为服务
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
.net core 3.x startup.cs文件中加入方法
public void ConfigureContainer(ContainerBuilder builder)
{
new Register()
//.SetDefaultLifeTime() //设置所有实例的默认生命周期,默认scope
.RegisterType(builder);
}
program.cs文件中
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
**.UseServiceProviderFactory(new AutofacServiceProviderFactory());**
.net core 2.x startup文件中,改写ConfigureServices方法
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var builder = new ContainerBuilder();//实例化 AutoFac 容器
builder.Populate(services);//管道寄居
new Register()
//.SetDefaultLifeTime() //设置所有实例的默认生命周期,默认scope
.RegisterType(builder);
ApplicationContainer = builder.Build();//IUserService UserService 构造
return new AutofacServiceProvider(ApplicationContainer);//将autofac反馈到管道中
}
- 特性说明 ComponentAttribute 只能标记在类上,代表此类型需要被自动注入
/// constructor
/// </summary>
/// <param name="lifetime">实例生命周期</param>
/// <param name="order">排序(数小在前)</param>
/// <param name="autoActivate">注册后是否激活实例</param>
/// <param name="interceptorEnum">代理类型</param>
/// <param name="registSelf">是否注册自己(作为接口实现类此属性false只作为接口注入,本身不注入)</param>
/// <param name="name">注册的服务名</param>
/// <param name="key">注册服务的key</param>
/// <param name="registAs">注册服务目标类型</param>
/// <param name="propertyInjectType">属性注入类型 all为所有属性自动注入 autowired为带注解的属性才注入</param>
public ComponentAttribute(LifetimeEnum lifetime = LifetimeEnum.Default, int order = 0, bool autoActivate = false,
InterceptorEnum interceptorEnum = InterceptorEnum.All, bool registSelf = true, string name = "",object key=null,Type registAs = null,PropertyInjectEnum propertyInjectType = PropertyInjectEnum.Autowired)
{
Lifetime = lifetime;
Order = order;
AutoActivate = autoActivate;
InterceptorType = interceptorEnum;
RegistSelf = registSelf;
Name = name;
RegistAs = registAs;
PropertyInjectType = propertyInjectType;
Key = key;
}
AutowiredAttribute 可以标记在属性、字段上 代表此属性(字段)将注入到实例中
/// <summary>
/// constructor
/// </summary>
/// <param name="name">需要获取的服务名(根据参数的类型和name去取)</param>
public AutowiredAttribute(string name = "")
{
Name = name;
}
/// <summary>
/// constructor
/// </summary>
/// <param name="key">需要获取的服务的key名</param>
public AutowiredAttribute(object key)
{
Key= key;
}
ConfignatrueAttribute 仅能标记在类上,代表自动从IConfiguration读取配置文件数据
//path为读取配置文件的字符串,和使用IConfiguration原生读取规则相同。
public ConfignatrueAttribute(string path)
{
Path = path;
}
ValueAttribute 可标记在属性,字段和参数上。代表被标记的对象需要从配置文件中自动读取数据
//path为读取配置文件的字符串,和使用IConfiguration原生读取规则相同。
public ValueAttribute(string path)
{
Path = path;
}
- 动态代理注册方法 继承抽象类 AbstractInterceptor 实现抽象类的抽象方法
public abstract class AbstractInterceptor : IInterceptor, IAsyncInterceptor
{
protected readonly IEnumerable<Assembly> dlls;//为实例化Register时所扫描的程序集
public AbstractInterceptor(IEnumerable<Assembly> dlls)
{
this.dlls = dlls;
}
public abstract IEnumerable<Type> GetInterceptorTypes();//此方法返回需要被代理的所有类型
public abstract void Intercept(IInvocation invocation);//固定写法 this.ToInterceptor().Intercept(invocation);
public abstract void InterceptAsynchronous(IInvocation invocation);//Task返回类型的拦截方法
public abstract void InterceptAsynchronous<TResult>(IInvocation invocation);//Task<TResult>返回类型的拦截方法
public abstract void InterceptSynchronous(IInvocation invocation);//同步方法的拦截
}
4.简单代码示例 新建web项目 新建接口和实现
public interface ITest
{
string Say();
}
[Component(name:"Test")]
public class Test : ITest
{
public Test() { }
public string Say()
{
return nameof(Test);
}
}
[Component(key: "Test1")]
public class Test1 : ITest
{
public virtual string Say()
{
return nameof(Test1);
}
}
//新建配置文件自动绑定实体
[Confignatrue(path: "Logging:LogLevel")]
public class LogModel
{
public string Default { get; set; }
}
//拦截器
[Component]
public class TestInterceptor : AbstractInterceptor
{
public TestInterceptor(IEnumerable<Assembly> dlls) : base(dlls)
{
}
public override IEnumerable<Type> GetInterceptorTypes()
{
//从基类继承的dll变量是扫描到的所有程序集,可以通过程序集获取对应的被代理类型 这里只是个示例
return new Type[] { typeof(Test),typeof(Test1) };
}
public override void Intercept(IInvocation invocation)
{
this.ToInterceptor().Intercept(invocation);
}
public override void InterceptAsynchronous(IInvocation invocation)
{
Console.WriteLine($"before:{nameof(InterceptAsynchronous)}");
invocation.Proceed();//执行被拦截的返回值为Task的方法
Console.WriteLine($"after:{nameof(InterceptAsynchronous)}");
}
public override void InterceptAsynchronous<TResult>(IInvocation invocation)
{
Console.WriteLine($"before:{nameof(InterceptAsynchronous)}");
invocation.Proceed();
Console.WriteLine($"after:{nameof(InterceptAsynchronous)}");
}
public override void InterceptSynchronous(IInvocation invocation)
{
Console.WriteLine($"before:{nameof(InterceptSynchronous)}");
invocation.Proceed();
Console.WriteLine($"after:{nameof(InterceptSynchronous)}");
}
}
//注入调用
[ApiController]
[Component]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly LogModel logModel;
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
//属性注入指定key的服务
[Autowired(key: "Test1")]
private Test1 test1;
private ITest test;
//属性注入配置文件的值
[Value("Logging:LogLevel")]
private LogModel testValue;
//不指定服务的属性注入
[Autowired]
private IGenercTest<LogModel> genercTest;
public WeatherForecastController([ComponentFilter(name:"Test")]ITest test, //构造函数注入使用特性获取指定服务
LogModel logModel, //注入已注册的配置类实体
ILogger<WeatherForecastController> logger)
{
_logger = logger;
this.test= test;
this.logModel = logModel;
this.genercTest = genercTest;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
Console.WriteLine($"test:{test1.Say()}");
Console.WriteLine($"genercTest:{genercTest.Say()}");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Autofac (>= 6.5.0)
- Autofac.Extensions.DependencyInjection (>= 8.0.0)
- Autofac.Extras.DynamicProxy (>= 6.0.1)
- Castle.Core.AsyncInterceptor (>= 2.1.0)
- Microsoft.Extensions.Configuration (>= 7.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 7.0.2)
- Microsoft.Extensions.Configuration.FileExtensions (>= 7.0.0)
- Microsoft.Extensions.Configuration.Json (>= 7.0.0)
- Microsoft.Extensions.DependencyModel (>= 7.0.0)
- System.Runtime.Loader (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.55 | 109 | 9/6/2024 |
1.0.54 | 132 | 6/5/2024 |
1.0.53 | 109 | 6/5/2024 |
1.0.52 | 99 | 5/21/2024 |
1.0.50 | 154 | 4/24/2024 |
1.0.49 | 115 | 4/23/2024 |
1.0.48 | 125 | 2/28/2024 |
1.0.47 | 140 | 1/23/2024 |
1.0.46 | 194 | 12/5/2023 |
1.0.45 | 215 | 9/8/2023 |
1.0.44 | 187 | 8/23/2023 |
1.0.43 | 151 | 8/10/2023 |
1.0.42 | 155 | 6/26/2023 |
1.0.41 | 151 | 5/23/2023 |
1.0.40 | 196 | 4/18/2023 |
1.0.39 | 197 | 4/14/2023 |
1.0.38 | 186 | 4/11/2023 |
1.0.37 | 201 | 4/11/2023 |
1.0.36 | 217 | 4/3/2023 |
1.0.35 | 240 | 3/16/2023 |
1.0.34 | 270 | 3/6/2023 |
1.0.32 | 243 | 3/3/2023 |
1.0.31 | 256 | 3/2/2023 |
1.0.30 | 253 | 3/2/2023 |
1.0.29 | 252 | 3/1/2023 |
1.0.28 | 273 | 2/25/2023 |
1.0.27 | 266 | 2/22/2023 |
1.0.26 | 259 | 2/22/2023 |
1.0.25 | 253 | 2/21/2023 |
1.0.24 | 257 | 2/20/2023 |
1.0.23 | 248 | 2/17/2023 |
1.0.22 | 319 | 2/15/2023 |
1.0.21 | 283 | 2/13/2023 |
1.0.20 | 271 | 2/9/2023 |
1.0.19 | 274 | 2/9/2023 |
1.0.18 | 282 | 2/7/2023 |
1.0.17 | 264 | 2/7/2023 |
1.0.16 | 258 | 2/7/2023 |
1.0.15 | 277 | 2/4/2023 |
1.0.14 | 287 | 2/2/2023 |
1.0.13 | 309 | 1/31/2023 |
1.0.12 | 296 | 1/31/2023 |
1.0.11 | 283 | 1/31/2023 |
1.0.10 | 311 | 1/30/2023 |
1.0.9 | 306 | 1/30/2023 |
1.0.8 | 302 | 1/30/2023 |
1.0.5 | 296 | 1/28/2023 |
1.0.4 | 321 | 1/28/2023 |
1.0.3 | 316 | 1/23/2023 |
1.0.2 | 317 | 1/21/2023 |
1.0.1 | 311 | 1/17/2023 |
1.0.0 | 318 | 1/17/2023 |