当前位置: 首页 > wzjs >正文

已经做好的网站怎么维护业务推广平台

已经做好的网站怎么维护,业务推广平台,尊园地产做的网站,做普通网站需要服务器吗文章目录 一、依赖注入核心原理1. 控制反转(IoC)与DI关系2. .NET DI核心组件 二、服务生命周期1. 三种生命周期类型 三、DI容器实现原理1. 服务注册流程2. 服务解析流程 四、高级实现方法1. 工厂模式注册2. 泛型服务注册3. 多实现解决方案 五、ASP.NET Core中的DI集成1. 控制器…

在这里插入图片描述

文章目录

    • 一、依赖注入核心原理
      • 1. 控制反转(IoC)与DI关系
      • 2. .NET DI核心组件
    • 二、服务生命周期
      • 1. 三种生命周期类型
    • 三、DI容器实现原理
      • 1. 服务注册流程
      • 2. 服务解析流程
    • 四、高级实现方法
      • 1. 工厂模式注册
      • 2. 泛型服务注册
      • 3. 多实现解决方案
    • 五、ASP.NET Core中的DI集成
      • 1. 控制器注入
      • 2. 视图注入
      • 3. 中间件注入
    • 六、自定义DI容器实现
      • 1. 简易DI容器实现
      • 2. 属性注入实现
    • 七、最佳实践
      • 1. 服务设计原则
      • 2. 常见陷阱
    • 八、性能优化
      • 1. 避免过度注入
      • 2. 编译时注入

在这里插入图片描述

一、依赖注入核心原理

1. 控制反转(IoC)与DI关系

  • 控制反转(IoC):框架控制程序流程,而非开发者
  • 依赖注入(DI):IoC的一种实现方式,通过外部提供依赖对象

2. .NET DI核心组件

  • IServiceCollection:服务注册容器
  • IServiceProvider:服务解析器
  • ServiceDescriptor:服务描述符(包含生命周期信息)

二、服务生命周期

1. 三种生命周期类型

生命周期描述适用场景
Transient每次请求创建新实例轻量级、无状态服务
Scoped同一作用域内共享实例Web请求上下文
Singleton全局单例配置服务、缓存
// 注册示例
services.AddTransient<ITransientService, TransientService>();
services.AddScoped<IScopedService, ScopedService>();
services.AddSingleton<ISingletonService, SingletonService>();

三、DI容器实现原理

1. 服务注册流程

public static IServiceCollection AddTransient<TService, TImplementation>(this IServiceCollection services)
{// 创建服务描述符var descriptor = new ServiceDescriptor(typeof(TService),typeof(TImplementation),ServiceLifetime.Transient);// 添加到集合services.Add(descriptor);return services;
}

2. 服务解析流程

public object GetService(Type serviceType)
{// 1. 查找服务描述符var descriptor = _descriptors.FirstOrDefault(d => d.ServiceType == serviceType);// 2. 根据生命周期创建实例if (descriptor.Lifetime == ServiceLifetime.Singleton){if (_singletons.TryGetValue(serviceType, out var instance))return instance;instance = CreateInstance(descriptor);_singletons[serviceType] = instance;return instance;}// ...处理Scoped和Transient
}

四、高级实现方法

1. 工厂模式注册

services.AddTransient<IService>(provider => {var otherService = provider.GetRequiredService<IOtherService>();return new ServiceImpl(otherService, "参数");
});

2. 泛型服务注册

services.AddTransient(typeof(IRepository<>), typeof(Repository<>));

3. 多实现解决方案

// 注册多个实现
services.AddTransient<IMessageService, EmailService>();
services.AddTransient<IMessageService, SmsService>();// 解析时获取所有实现
var services = provider.GetServices<IMessageService>();

五、ASP.NET Core中的DI集成

1. 控制器注入

public class HomeController : Controller
{private readonly ILogger _logger;public HomeController(ILogger<HomeController> logger){_logger = logger; // 自动注入}
}

2. 视图注入

@inject IConfiguration Config
<p>当前环境: @Config["Environment"]</p>

3. 中间件注入

public class CustomMiddleware
{private readonly RequestDelegate _next;private readonly ILogger _logger;public CustomMiddleware(RequestDelegate next,ILogger<CustomMiddleware> logger){_next = next;_logger = logger;}public async Task InvokeAsync(HttpContext context){// 使用注入的服务_logger.LogInformation("中间件执行");await _next(context);}
}

六、自定义DI容器实现

1. 简易DI容器实现

public class SimpleContainer : IServiceProvider
{private readonly Dictionary<Type, ServiceDescriptor> _descriptors;public SimpleContainer(IEnumerable<ServiceDescriptor> descriptors){_descriptors = descriptors.ToDictionary(x => x.ServiceType);}public object GetService(Type serviceType){if (!_descriptors.TryGetValue(serviceType, out var descriptor))return null;if (descriptor.ImplementationInstance != null)return descriptor.ImplementationInstance;var type = descriptor.ImplementationType ?? descriptor.ServiceType;return ActivatorUtilities.CreateInstance(this, type);}
}

2. 属性注入实现

public static class PropertyInjectionExtensions
{public static void AddPropertyInjection(this IServiceCollection services){services.AddTransient<IStartupFilter, PropertyInjectionStartupFilter>();}
}public class PropertyInjectionStartupFilter : IStartupFilter
{public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next){return builder =>{builder.Use(async (context, nextMiddleware) =>{var endpoint = context.GetEndpoint();if (endpoint?.Metadata.GetMetadata<ControllerActionDescriptor>() is { } descriptor){var controller = context.RequestServices.GetRequiredService(descriptor.ControllerTypeInfo);// 反射实现属性注入InjectProperties(controller, context.RequestServices);}await nextMiddleware();});next(builder);};}private void InjectProperties(object target, IServiceProvider services){var properties = target.GetType().GetProperties().Where(p => p.CanWrite && p.GetCustomAttribute<InjectAttribute>() != null);foreach (var prop in properties){var service = services.GetService(prop.PropertyType);if (service != null)prop.SetValue(target, service);}}
}

七、最佳实践

1. 服务设计原则

  • 遵循显式依赖原则
  • 避免服务定位器模式(反模式)
  • 保持服务轻量级

2. 常见陷阱

// 错误示例:捕获Scoped服务到Singleton中
services.AddSingleton<IBackgroundService>(provider => {var scopedService = provider.GetRequiredService<IScopedService>(); // 危险!return new BackgroundService(scopedService);
});// 正确做法:使用IServiceScopeFactory
services.AddSingleton<IBackgroundService>(provider => {var scopeFactory = provider.GetRequiredService<IServiceScopeFactory>();return new BackgroundService(scopeFactory);
});

八、性能优化

1. 避免过度注入

// 不好:注入过多服务
public class OrderService(ILogger logger,IEmailService emailService,ISmsService smsService,IRepository repo,ICache cache,IConfig config)
{// ...
}// 改进:使用聚合服务
public class OrderService(ILogger logger,INotificationService notification,IOrderInfrastructure infra)
{// ...
}

2. 编译时注入

[RegisterTransient(typeof(IMyService))]
public class MyService : IMyService
{// ...
}// 使用Source Generator自动生成注册代码
static partial class ServiceRegistration
{static partial void AddGeneratedServices(IServiceCollection services){services.AddTransient<IMyService, MyService>();}
}

.NET的依赖注入系统是框架的核心基础设施,理解其原理和实现方式有助于编写更可测试、更松耦合的应用程序。

在这里插入图片描述

http://www.dtcms.com/wzjs/428873.html

相关文章:

  • 中企动力做的网站怎么样软文网
  • 保定制作公司网站百度关键词规划师工具
  • php怎么做视频网站创建站点的步骤
  • 玉溪市规划建设局网站免费发布信息
  • 广州网站建设吧店铺推广引流的方法
  • 湖南网站建设方案优化seo外链建设的方法
  • 云南建设学校网站登陆排名第一的手机清理软件
  • 本溪网站开发职业培训机构排名前十
  • 外贸商城网站系统阿里云盘资源搜索引擎
  • 如何自己做加盟网站国外最好的免费建站
  • 炒股配资网站开发专业网站制作网站公司
  • 做网站建设怎么样公司以优化为理由裁员合法吗
  • 宠物电商网站模板一个网站如何推广
  • wordpress网站维护教程成都官网seo厂家
  • 做一个网站怎么做的网站推广网站
  • 天津手动网站建设调试北京seo优化方案
  • 武汉外贸网站制作chrome谷歌浏览器官方下载
  • sfda的网站的建设特点百度号码认证平台
  • 做期货的的都喜欢去什么网站社交媒体营销
  • 现在外贸做那个网站好手机优化软件排名
  • 做网站开发的想接私活自动点击器软件
  • 北京网站建设外包公司seo优化工具
  • 网站 ipc 备案放心网站推广优化咨询
  • 宜宾网站建设08keji代运营一家店铺多少钱
  • 还有什么网站可以做面包车拉货百度指数app官方下载
  • 慈善系统网站建设需求注册域名费用一般多少钱
  • 流程做网站站长工具seo推广 站长工具查询
  • 做网站除了买域名还有什么友链交易平台
  • 网站视频主持人太原网站推广公司
  • 一个专门做视频配音的网站电商seo名词解释