日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

.NET Core2.1下采用EFCore比较原生IOC、AspectCore、AutoFac之间的性能

發(fā)布時(shí)間:2025/4/16 89 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET Core2.1下采用EFCore比较原生IOC、AspectCore、AutoFac之间的性能 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、前言


?ASP.NET Core本身已經(jīng)集成了一個(gè)輕量級的IOC容器,開發(fā)者只需要定義好接口后,在Startup.cs的ConfigureServices方法里使用對應(yīng)生命周期的綁定方法即可,常見方法如下

services.AddTransient<IApplicationService,ApplicationService> services.AddScoped<IApplicationService,ApplicationService> services.AddSingleton<IApplicationService,ApplicationService>

除了ASP.NETCore自帶的IOC容器外,我們還可以使用其他成熟的DI框架,如Autofac,StructureMap等(筆者只用過Unity,Ninject和Castle,Castle也是使用ABP時(shí)自帶的),以及AspectCore

二、性能比較


?1、新建三個(gè).NET Core2.1項(xiàng)目(項(xiàng)目比較源碼將會發(fā)到github上)

分別為:IOCDemo、Autofac、AspectCoreDemo

?

?2、在每個(gè)項(xiàng)目配置

?A、在每個(gè)項(xiàng)目中都添加EFCore的 mysql的nuget包。

注:如果引入的nuget包 Pomelo.EntityFrameworkCore.Mysql 是2.0以上的版本(不包含2.0),則需要再多引入 Microsoft.EntityFrameworkCore.Design

?

?

B、新建數(shù)據(jù)上下文LGDbContext(每個(gè)項(xiàng)目)

public class LGDbContext : DbContext{public LGDbContext(DbContextOptions<LGDbContext> options) : base(options) { }#region public DbSet<User> User { get; set; }#endregion}public class User{public int Id { get; set; }public string UserName { get; set; }public int WorkId { get; set; }public string Pwd { get; set; }}

?

?C、在原生的IOC項(xiàng)目中配置Startup.cs

public void ConfigureServices(IServiceCollection services){services.AddDbContext<LGDbContext>(a =>{a.UseMySql("Server=xxxxx;database=xxxx;port=3306;uid=root;pwd=xxx;Character Set=utf8;SslMode = none;");});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);}

?

D、在aspectCore項(xiàng)目中配置

1、需要添加aspectCore的nuget包。

?

2、在startup.cs 文件中替換原生的IOC

public IServiceProvider ConfigureServices(IServiceCollection services){services.AddDbContext<LGDbContext>(a =>{a.UseMySql("Server=xxx;database=xxx;port=3306;uid=root;pwd=xxxx;Character Set=utf8;SslMode = none;");});services.AddMvc();var container = services.ToServiceContainer();return container.Build();}

?

?E、在AutofacDemo項(xiàng)目中配置

1、添加autofac的nuget包

?

2、在startup.cs 文件中替換原生的IOC

public IServiceProvider ConfigureServices(IServiceCollection services){services.AddDbContext<LGDbContext>(a =>{a.UseMySql("Server=xxx;database=xxx;port=3306;uid=root;pwd=xxxx;Character Set=utf8;SslMode = none;");});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);var containerBuilder = new ContainerBuilder();//containerBuilder.RegisterModule<DefaultModule>(); containerBuilder.Populate(services);var container = containerBuilder.Build();return new AutofacServiceProvider(container);}

?

?F、三個(gè)項(xiàng)目都配置完后就在每個(gè)項(xiàng)目中配置 Controller接口,來比較所耗費(fèi)的時(shí)間

#ValuesControllerpublic class ValuesController : ControllerBase{private readonly LGDbContext _lGDbContext;public ValuesController(LGDbContext lGDbContext){_lGDbContext = lGDbContext;}// GET api/values [HttpGet]public ActionResult<string> Get(int workid, string pwd){Stopwatch sw = new Stopwatch();sw.Start();var user = _lGDbContext.User.FirstOrDefault(a => a.WorkId == workid && a.Pwd == pwd);sw.Stop();TimeSpan ts2 = sw.Elapsed;Console.WriteLine("Stopwatch總共花費(fèi){0}s.", ts2.TotalSeconds);return Ok();}}

?

?以上配置完成后,我們就可以運(yùn)行三個(gè)項(xiàng)目啦。我們來看一下各自所用的時(shí)間。

以下是分別運(yùn)行的單獨(dú)項(xiàng)目。

aspectCore耗費(fèi)時(shí)間

?

?autofac 耗費(fèi)時(shí)間

?

原生IOC耗費(fèi)時(shí)間

?

?下面是一起運(yùn)行的項(xiàng)目的耗費(fèi)時(shí)間

?

總得比較

aspectCore ?》 autofac 》 原生IOC

不過aspect core 與autofac差不了多少。具體根據(jù)你們項(xiàng)目中的使用吧。

當(dāng)然其他的DI框架,目前沒有進(jìn)行比較。如果大家有興趣可以多多比較以下。

?

github源碼:https://github.com/LouieGuo/LG_ComapreDI_Demo

?

asp.net?core 交流群:787464275?歡迎加群交流
如果您認(rèn)為這篇文章還不錯(cuò)或者有所收獲,您可以點(diǎn)擊右下角的【推薦】按鈕精神支持,因?yàn)檫@種支持是我繼續(xù)寫作,分享的最大動力!

作者:LouieGuo
聲明:原創(chuàng)博客請?jiān)谵D(zhuǎn)載時(shí)保留原文鏈接或者在文章開頭加上本人博客地址,如發(fā)現(xiàn)錯(cuò)誤,歡迎批評指正。凡是轉(zhuǎn)載于本人的文章,不能設(shè)置打賞功能,如有特殊需求請與本人聯(lián)系!

微信公眾號:歡迎關(guān)注? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?QQ技術(shù)交流群:?歡迎加群

? ? ? ? ? ? ? ??

轉(zhuǎn)載于:https://www.cnblogs.com/guolianyu/p/9981564.html

總結(jié)

以上是生活随笔為你收集整理的.NET Core2.1下采用EFCore比较原生IOC、AspectCore、AutoFac之间的性能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。