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

it培训机构网站开发9377将军传奇

it培训机构网站开发,9377将军传奇,手动搭建wordpress环境,inews wordpress前言:接上一篇文章,有了微软官方的依赖注入组件Microsoft.Extensions.DependencyInjection, 那么今天介绍一个新的开源的依赖注入组件Autofac 一、二者的差异Autofac和微软官方的依赖注入组件(Microsoft.Extensions.DependencyIn…

前言:接上一篇文章,有了微软官方的依赖注入组件Microsoft.Extensions.DependencyInjection, 那么今天介绍一个新的开源的依赖注入组件Autofac

一、二者的差异Autofac和微软官方的依赖注入组件(Microsoft.Extensions.DependencyInjection,简称MSDI)在功能、灵活性和适用场景上存在显著差异。以下是两者的核心区别分析:

1. 功能扩展性

Autofac
①高级特性支持:提供属性注入([Inject])、方法注入基于元数据的注册(如Keyed/Named服务)、动态代理(AOP)等特性。

②模块化设计:通过Module类实现代码分组和配置复用,适合大型项目。

③生命周期管理:支持InstancePerDependency(瞬态)、SingleInstance(单例)、InstancePerLifetimeScope(作用域),并允许自定义生命周期。

④集成能力:可与第三方库(如ASP.NET Core、WPF、Unity等)深度集成,甚至替换MSDI为底层容器。

MSDI

①基础功能为主:仅支持构造函数注入和简单的生命周期(Transient、Scoped、Singleton)。
②轻量级设计:作为.NET Core内置组件,注重开箱即用,适合小型项目或快速原型开发。
与ASP.NET Core深度集成:作为框架默认容器,与中间件、配置系统无缝协作。

使用 Autofac 实现依赖注入

一、 基本依赖注入

首先,我们来看一个简单的依赖注入示例。假设我们有一个 IProductService 接口和一个 ProductService 类,以及一个 IProductRepository 接口和一个 ProductRepository 类。

定义类

public interface IProductService
{void SaveProduct();
}public interface IProductRepository
{void GetProduct();
}

类实现

public class ProductService : IProductService
{public IProductRepository productRepository { get; set; }public void SaveProduct(){productRepository?.GetProduct();Console.WriteLine("保存ProductService商品成功");}
}public class ProductRepository : IProductRepository
{public void GetProduct(){Console.WriteLine("获取商品成功");}
}

使用 Autofac 进行属性注入

在 Autofac 中,我们可以使用 PropertiesAutowired() 方法来实现属性注入。

using Autofac;
using System.Reflection;public class Program
{public static void Main(string[] args){// 1、创建ContainerBuilderContainerBuilder containerBuilder = new ContainerBuilder();// 1.1、注册具体类型和接口containerBuilder.RegisterType<ProductService>().As<IProductService>().PropertiesAutowired(); // 属性依赖注入containerBuilder.RegisterType<ProductRepository>().As<IProductRepository>(); // 注入具体类型接口// 2、构造容器var container = containerBuilder.Build();// 3、取对象using (var scope = container.BeginLifetimeScope()) // 设定范围{IProductService productService = scope.Resolve<IProductService>();productService.SaveProduct();}}
}

在这个示例中,ProductServiceproductRepository 属性会被自动注入 ProductRepository 的实例。

二、 批量注入

在实际项目中,手动注册每个类型可能会比较繁琐。我们可以使用 Autofac 的批量注入功能来简化这个过程。

接口定义

public interface IProductService
{void SaveProduct();
}public interface IOrderService
{void GetOrders();
}public interface IProductRepository
{void GetProduct();
}public interface IOrderRepository
{void GetOrders();
}

类实现

public class ProductService : IProductService
{public IProductRepository productRepository { get; set; }public void SaveProduct(){productRepository?.GetProduct();Console.WriteLine("保存ProductService商品成功");}
}public class OrderService : IOrderService
{public IOrderRepository orderRepository { get; set; }public void GetOrders(){orderRepository?.GetOrders();Console.WriteLine($"查询订单成功, ID: {this.GetHashCode()}");}
}public class ProductRepository : IProductRepository
{public void GetProduct(){Console.WriteLine("获取商品成功");}
}public class OrderRepository : IOrderRepository
{public void GetOrders(){Console.WriteLine("获取订单成功");}
}

使用 Autofac 进行批量注入

using Autofac;
using System.Reflection;public class Program
{public static void Main(string[] args){// 1、创建ContainerBuilderContainerBuilder containerBuilder = new ContainerBuilder();// 1.1、整个项目注册containerBuilder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) // 加载整个项目.AsImplementedInterfaces() // 只要有接口都实现注册.PropertiesAutowired(); // 开发属性依赖注入// 2、构造容器var container = containerBuilder.Build();// 3、取对象using (var scope = container.BeginLifetimeScope()){IProductService productService = scope.Resolve<IProductService>();productService.SaveProduct();IOrderService orderService = scope.Resolve<IOrderService>();orderService.GetOrders();}}
}

在这个示例中,Autofac 会自动扫描当前程序集中的所有类型,并将实现了接口的类注册为相应的接口。PropertiesAutowired() 方法会自动注入所有实现了接口的属性。

三、 基于特性的对象过滤

有时候,我们希望根据某些条件来注册特定的类型。我们可以使用特性(Attributes)来实现这一点。假设我们希望只注册那些带有 AutoRegisterAttribute 特性的类。

创建特性类

using System;[AttributeUsage(AttributeTargets.Class)]
public class AutoRegisterAttribute : Attribute
{
}

在类上添加特性

[AutoRegister]
public class ProductService : IProductService
{public IProductRepository productRepository { get; set; }public void SaveProduct(){productRepository?.GetProduct();Console.WriteLine("保存ProductService商品成功");}
}[AutoRegister]
public class OrderService : IOrderService
{public IOrderRepository orderRepository { get; set; }public void GetOrders(){orderRepository?.GetOrders();Console.WriteLine($"查询订单成功, ID: {this.GetHashCode()}");}
}public class ProductRepository : IProductRepository
{public void GetProduct(){Console.WriteLine("获取商品成功");}
}public class OrderRepository : IOrderRepository
{public void GetOrders(){Console.WriteLine("获取订单成功");}
}

使用 Autofac 进行基于特性的对象过滤

using Autofac;
using System.Reflection;public class Program
{public static void Main(string[] args){// 1、创建ContainerBuilderContainerBuilder containerBuilder = new ContainerBuilder();// 1.1、整个项目注册containerBuilder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) // 加载整个项目.Where(t => t.GetCustomAttributes(typeof(AutoRegisterAttribute), false).Length > 0) // 通过特性过滤.AsImplementedInterfaces() // 只要有接口都实现注册.PropertiesAutowired(); // 开发属性依赖注入// 1.2、注册具体类型接口containerBuilder.RegisterType<ProductRepository>().As<IProductRepository>(); // 注入具体类型接口containerBuilder.RegisterType<OrderRepository>().As<IOrderRepository>(); // 注入具体类型接口// 2、构造容器var container = containerBuilder.Build();// 3、取对象using (var scope = container.BeginLifetimeScope()){IProductService productService = scope.Resolve<IProductService>();productService.SaveProduct();IOrderService orderService = scope.Resolve<IOrderService>();orderService.GetOrders();}}
}

在这个示例中,Where(t => t.GetCustomAttributes(typeof(AutoRegisterAttribute), false).Length > 0) 方法会过滤出所有带有 AutoRegisterAttribute 特性的类,并将它们注册到容器中。

四、 基于方法特性的对象过滤

如果你希望根据方法上的特性来过滤类,可以稍微修改一下我们的配置。假设我们希望只注册那些带有 AutoRegisterMethodAttribute 特性方法的类。

创建特性类

using System;[AttributeUsage(AttributeTargets.Method)]
public class AutoRegisterMethodAttribute : Attribute
{
}

在方法上添加特性

public interface IProductService
{void SaveProduct();
}public interface IOrderService
{void GetOrders();
}public interface IProductRepository
{void GetProduct();
}public interface IOrderRepository
{void GetOrders();
}public class ProductService : IProductService
{public IProductRepository productRepository { get; set; }public void SaveProduct(){productRepository?.GetProduct();Console.WriteLine("保存ProductService商品成功");}
}public class OrderService : IOrderService
{public IOrderRepository orderRepository { get; set; }[AutoRegisterMethod]public void GetOrders(){orderRepository?.GetOrders();Console.WriteLine($"查询订单成功, ID: {this.GetHashCode()}");}
}public class ProductRepository : IProductRepository
{public void GetProduct(){Console.WriteLine("获取商品成功");}
}public class OrderRepository : IOrderRepository
{public void GetOrders(){Console.WriteLine("获取订单成功");}
}

使用 Autofac 进行基于方法特性的对象过滤

using Autofac;
using System.Reflection;public class Program
{public static void Main(string[] args){// 1、创建ContainerBuilderContainerBuilder containerBuilder = new ContainerBuilder();// 1.1、基于方法特性注册containerBuilder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) // 加载整个项目.Where(t => t.GetMethods().Any(m => m.GetCustomAttributes(typeof(AutoRegisterMethodAttribute), false).Length > 0)).AsImplementedInterfaces() // 只要有接口都实现注册.PropertiesAutowired(); // 开发属性依赖注入// 1.2、注册具体类型接口containerBuilder.RegisterType<ProductRepository>().As<IProductRepository>(); // 注入具体类型接口containerBuilder.RegisterType<OrderRepository>().As<IOrderRepository>(); // 注入具体类型接口// 2、构造容器var container = containerBuilder.Build();// 3、取对象using (var scope = container.BeginLifetimeScope()){// IProductService productService = scope.Resolve<IProductService>();// productService.SaveProduct();IOrderService orderService = scope.Resolve<IOrderService>();orderService.GetOrders();}}
}

在这个示例中,Where(t => t.GetMethods().Any(m => m.GetCustomAttributes(typeof(AutoRegisterMethodAttribute), false).Length > 0)) 方法会过滤出所有包含带有 AutoRegisterMethodAttribute 特性方法的类,并将它们注册到容器中。

总结

通过本文,学习了如何使用 Autofac 实现依赖注入,包括:

属性注入:使用 PropertiesAutowired() 方法自动注入属性。
批量注入:使用 RegisterAssemblyTypes() 方法自动注册程序集中的所有类型。
基于特性的对象过滤:通过自定义特性类和 GetCustomAttributes() 方法来过滤需要注册的类。
Autofac 提供了灵活的配置选项,可以根据具体需求选择合适的注入方式。


文章转载自:

http://2AkWspXT.gwkjg.cn
http://E3bGQr67.gwkjg.cn
http://oPfKmG2I.gwkjg.cn
http://krM762GK.gwkjg.cn
http://6Zyi6Fk8.gwkjg.cn
http://TRGZcwIv.gwkjg.cn
http://IzBdv3Ee.gwkjg.cn
http://jrnEkvRK.gwkjg.cn
http://fcyS6kva.gwkjg.cn
http://TAYKTs3W.gwkjg.cn
http://I2IeWtJ8.gwkjg.cn
http://OcR8N5vw.gwkjg.cn
http://FxE7VUlj.gwkjg.cn
http://fZeALhSN.gwkjg.cn
http://OH2p8Xb3.gwkjg.cn
http://iOk8h7hi.gwkjg.cn
http://zT2uBNgI.gwkjg.cn
http://bRWvdlWQ.gwkjg.cn
http://HT6YwKmU.gwkjg.cn
http://WYhHUyTO.gwkjg.cn
http://H8WGiGpQ.gwkjg.cn
http://NJuH4W0T.gwkjg.cn
http://zdy0qBbP.gwkjg.cn
http://Knd3pemu.gwkjg.cn
http://uvMImkzL.gwkjg.cn
http://F5AXbXmV.gwkjg.cn
http://Bsj9Fe3j.gwkjg.cn
http://eYuue2SX.gwkjg.cn
http://3KlK7jml.gwkjg.cn
http://MHJg10tJ.gwkjg.cn
http://www.dtcms.com/wzjs/730219.html

相关文章:

  • 浙江网站建设价位烟台电商网站建设
  • 如何在godaddy空间做手机网站专门做淘宝主图的网站
  • 专业零基础网站建设教学培训二维码生成器app下载
  • 落地页需要建网站吗ip查询地址精准地图
  • 浙江网站建设价格怎么看网站是哪里做的
  • 锦州公司做网站温州设计集团
  • 做网站开麻烦吗北京网站优化效果怎样
  • 网站权重高+做别的关键词wordpress 后台汉化
  • 厦门公司建站全屋定制设计培训学校
  • 文化传媒网站封面网站直播间怎么做
  • 庭院设计济南seo整站优化价格
  • 支付网站搭建有没有专业做淘宝网站吗
  • 织梦网站识别网站的制作方案
  • 彩票网站怎么做系统网站建设仟金手指六六14
  • 东莞饭堂承包东莞网站建设做数据分析好看的网站
  • 自己怎么做一元购物网站高品质的网站设计制作
  • 做网站需要的大图在线制作图片网站有哪些
  • 软件工网站开发课程设计报告大江网站建设
  • 东莞专业网站设计咨询杭州建设工程信用平台
  • 文章一键导入wordpress网站栏目页 优化
  • 网站开发 家具销售 文献关键词歌曲歌词
  • 手机h5网站开发线上编程课
  • 电商网站开发报价单银川网站公司
  • 做商城网站的公司推荐WordPress文章预览篇幅
  • 做电子请帖的网站海口seo关键词优化
  • 合肥网站忧化建设部网站关于公租房
  • 女和女做网站网页设计免费网站
  • 工信部清理未备案网站专业建站公司前景
  • 部门网站建设自查报告建设网站模版
  • asp网站建设制作怎么可以联系到网站开发者