![]() |
VOOZH | about |
dotnet add package TJC.Cyclops.DI --version 2026.6.11.2
NuGet\Install-Package TJC.Cyclops.DI -Version 2026.6.11.2
<PackageReference Include="TJC.Cyclops.DI" Version="2026.6.11.2" />
<PackageVersion Include="TJC.Cyclops.DI" Version="2026.6.11.2" />Directory.Packages.props
<PackageReference Include="TJC.Cyclops.DI" />Project file
paket add TJC.Cyclops.DI --version 2026.6.11.2
#r "nuget: TJC.Cyclops.DI, 2026.6.11.2"
#:package TJC.Cyclops.DI@2026.6.11.2
#addin nuget:?package=TJC.Cyclops.DI&version=2026.6.11.2Install as a Cake Addin
#tool nuget:?package=TJC.Cyclops.DI&version=2026.6.11.2Install as a Cake Tool
🧩 增强型依赖注入框架 🧩
Cyclops.DI 是企服版框架中的依赖注入核心库,提供了增强型的依赖注入功能,扩展了.NET标准依赖注入容器的能力。该库不仅支持标准的依赖注入模式,还提供了基于接口的自动注册、AOP代理拦截、生命周期管理以及丰富的配置选项,使开发者能够构建更加灵活、可维护的企业级应用。
dotnet add package TJC.Cyclops.DI
在应用程序的启动类中进行配置:
using Cyclops.DI;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// 自动注入所有实现了IDependency的类型(包含ITransient、IScoped、ISingleton)
services.AutoInjectAllCustomerServices();
// 其他服务注册...
}
}
using Cyclops.DI;
// 定义服务接口
public interface IUserService
{
User GetUserById(int id);
void SaveUser(User user);
}
// 实现服务并指定生命周期
public class UserService : IUserService, IScoped
{
private readonly IDbContext _dbContext;
public UserService(IDbContext dbContext)
{
_dbContext = dbContext;
}
public User GetUserById(int id)
{
return _dbContext.Users.FirstOrDefault(u => u.Id == id);
}
public void SaveUser(User user)
{
if (user.Id == 0)
{
_dbContext.Users.Add(user);
}
else
{
_dbContext.Users.Update(user);
}
_dbContext.SaveChanges();
}
}
using Cyclops.DI;
using Cyclops.DI.Attrs;
// 使用InjectionAttribute配置注入行为
[Injection(Order = 10, Pattern = EnumInjectionPatterns.All)]
public class ProductService : IProductService, ISingleton
{
// 服务实现...
}
// 抑制代理创建
[SuppressProxy]
public class LoggingService : ILoggingService, ITransient
{
// 服务实现...
}
using Cyclops.DI;
using Cyclops.DI.Core;
using System.Reflection;
// 创建自定义代理类
public class LoggingProxy : AspectDispatchProxy
{
public IServiceProvider Services { get; set; }
public object Target { get; set; }
private ILogger _logger;
private ILogger Logger => _logger ??= Services.GetRequiredService<ILogger>();
protected override object Invoke(MethodInfo targetMethod, object[] args)
{
// 方法调用前
var methodName = targetMethod.Name;
var className = Target.GetType().Name;
Logger.Info($"[{className}.{methodName}] 开始执行");
try
{
// 执行实际方法
var result = targetMethod.Invoke(Target, args);
// 方法调用后
Logger.Info($"[{className}.{methodName}] 执行完成");
return result;
}
catch (Exception ex)
{
// 异常处理
Logger.Error($"[{className}.{methodName}] 执行出错: {ex.InnerException?.Message}", ex);
throw ex.InnerException ?? ex;
}
}
}
// 在服务上指定自定义代理
[Injection(Proxy = typeof(LoggingProxy))]
public class OrderService : IOrderService, IScoped
{
// 服务实现...
}
using Cyclops.DI;
using Cyclops.DI.Attrs;
public void ConfigureServices(IServiceCollection services)
{
// 自动注入所有实现了IDependency的服务
services.AutoInjectAllCustomerServices();
// 手动注册服务
var injectionAttribute = new InjectionAttribute
{
Pattern = EnumInjectionPatterns.SelfWithFirstInterface,
Action = EnumInjectionActions.Add
};
ServiceDescriptorUtil.Register(
services,
typeof(IScoped),
typeof(CustomerService),
injectionAttribute,
typeof(ICustomerService)
);
}
using Cyclops.DI;
// 服务层 - 作用域生命周期
public class UserService : IUserService, IScoped
{
private readonly IRepository _repository; // 假设也是IScoped
private readonly ICacheService _cacheService; // 假设是ISingleton
public UserService(IRepository repository, ICacheService cacheService)
{
_repository = repository;
_cacheService = cacheService;
}
public User GetUser(int id)
{
// 优先从缓存获取
var cacheKey = $"user:{id}";
var user = _cacheService.Get<User>(cacheKey);
if (user == null)
{
// 缓存未命中,从数据库获取
user = _repository.GetById<User>(id);
// 更新缓存
if (user != null)
{
_cacheService.Set(cacheKey, user, TimeSpan.FromMinutes(30));
}
}
return user;
}
}
// 作用域创建和使用
public class SomeService
{
private readonly IServiceProvider _serviceProvider;
public SomeService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void ProcessBatch(List<int> ids)
{
foreach (var id in ids)
{
// 为每个操作创建单独的作用域
using (var scope = _serviceProvider.CreateScope())
{
var scopedService = scope.ServiceProvider.GetRequiredService<IScopedService>();
scopedService.Process(id);
}
}
}
}
接口继承顺序:实现类在继承ITransient、IScoped或ISingleton时,应确保它们是最后继承的接口之一,以避免类型解析问题
生命周期使用建议:
代理使用注意:
依赖注入循环引用:避免服务之间形成循环依赖,这可能导致服务初始化失败
线程安全:在使用ISingleton服务时,务必确保其是线程安全的,特别是在并发环境下
作用域管理:在长生命周期的服务中使用作用域服务时,应手动创建和管理作用域
异常处理:在代理中捕获和处理异常时,注意保留原始异常信息,以便于调试
服务注册顺序:对于有依赖关系的服务,应确保先注册被依赖的服务
日志记录:在代理中添加日志时,注意控制日志级别,避免过多的日志影响性能
内存管理:长时间运行的应用程序中,注意监控单例服务的内存使用情况,避免内存泄漏
我们欢迎社区贡献!如果您有任何想法或建议,欢迎提交 Issue 或 Pull Request。
MIT License
Cyclops.DI - 让依赖注入更加灵活强大,为企业级应用提供可靠的服务管理!✨
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 5 NuGet packages that depend on TJC.Cyclops.DI:
| Package | Downloads |
|---|---|
|
TJC.Cyclops.Orm
企服版框架中ORM核心,基于YitIdHelper、Mapster、SqlSugar封装 |
|
|
TJC.Cyclops.Common.Service
企服版框架中服务处理相关,包括常规Service的集成处理和后台任务JobService的处理 |
|
|
TJC.Cyclops.ApprovalFlow
企服版框架中审批流开发套件 |
|
|
TJC.Cyclops.Service
企服版框架中服务处理相关,包括常规Service的集成处理和后台任务JobService的处理 |
|
|
TJC.Cyclops.AIAgent
Cyclops.AIAgent 是一个 AI 代理框架,提供了与各种 AI 模型交互的能力,支持技能系统、工具调用和会话管理。 |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2026.6.11.2 | 371 | 6/11/2026 |
| 2026.6.11.1 | 411 | 6/11/2026 |
| 2026.6.9.4 | 398 | 6/9/2026 |
| 2026.6.9.3 | 391 | 6/9/2026 |
| 2026.6.9.2 | 391 | 6/9/2026 |
| 2026.6.9.1 | 379 | 6/9/2026 |
| 2026.6.8.3 | 402 | 6/8/2026 |
| 2026.6.8.2 | 357 | 6/8/2026 |
| 2026.6.8.1 | 355 | 6/8/2026 |
| 2026.6.5.1 | 315 | 6/5/2026 |
| 2026.5.18.1 | 294 | 5/18/2026 |
| 2026.5.11.1 | 299 | 5/11/2026 |
| 2026.5.7.2 | 329 | 5/7/2026 |
| 2026.5.7.1 | 322 | 5/7/2026 |
| 2026.4.29.2 | 258 | 4/29/2026 |
| 2026.4.29.1 | 282 | 4/29/2026 |
| 2026.4.27.1 | 251 | 4/27/2026 |
| 2026.4.24.2 | 244 | 4/24/2026 |
| 2026.4.24.1 | 234 | 4/24/2026 |
| 2026.4.14.2 | 264 | 4/14/2026 |
cyclops.framework中依赖注入核心库