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

创业型企业网站模板百度网站排名搜行者seo

创业型企业网站模板,百度网站排名搜行者seo,软件开发合同注意事项,青岛建站价格在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入#xff0c;本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入。 PS#xff1a;本章将主要采用构造函数注入的方式#xff0c;下一章将继续分享如何使之能够同… 在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入。 PS本章将主要采用构造函数注入的方式下一章将继续分享如何使之能够同时支持属性注入的方式。 约定 1、仓储层接口都以“I”开头以“Repository”结尾。仓储层实现都以“Repository”结尾。 2、服务层接口都以“I”开头以“Service”结尾。服务层实现都以“Service”结尾。 接下来我们正式进入主题在上一章的基础上我们再添加一个web项目TianYa.DotNetShare.CoreAutofacMvcDemo首先来看一下我们的解决方案 本demo的web项目为ASP.NET Core Web 应用程序.NET Core 2.2 MVC框架需要引用以下几个程序集 1、TianYa.DotNetShare.Model 我们的实体层 2、TianYa.DotNetShare.Service 我们的服务层 3、TianYa.DotNetShare.Repository 我们的仓储层正常我们的web项目是不应该使用仓储层的此处我们引用是为了演示IOC依赖注入 4、Autofac 依赖注入基础组件 5、Autofac.Extensions.DependencyInjection 依赖注入.NET Core的辅助组件 其中Autofac和Autofac.Extensions.DependencyInjection需要从我们的NuGet上引用依次点击下载以下2个包 接着我们在web项目中添加一个类AutofacModuleRegister.cs用来注册Autofac模块如下所示 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks;using Autofac;namespace TianYa.DotNetShare.CoreAutofacMvcDemo {/// summary/// 注册Autofac模块/// /summarypublic class AutofacModuleRegister : Autofac.Module{/// summary/// 重写Autofac管道Load方法在这里注册注入/// /summaryprotected override void Load(ContainerBuilder builder){builder.RegisterAssemblyTypes(GetAssemblyByName(TianYa.DotNetShare.Repository)).Where(a a.Name.EndsWith(Repository)).AsImplementedInterfaces();builder.RegisterAssemblyTypes(GetAssemblyByName(TianYa.DotNetShare.Service)).Where(a a.Name.EndsWith(Service)).AsImplementedInterfaces();//注册MVC控制器注册所有到控制器控制器注入就是需要在控制器的构造函数中接收对象builder.RegisterAssemblyTypes(GetAssemblyByName(TianYa.DotNetShare.CoreAutofacMvcDemo)).Where(a a.Name.EndsWith(Controller)).AsImplementedInterfaces();}/// summary/// 根据程序集名称获取程序集/// /summary/// param nameassemblyName程序集名称/parampublic static Assembly GetAssemblyByName(string assemblyName){return Assembly.Load(assemblyName);}} } 然后打开我们的Startup.cs文件进行注入工作如下所示 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection;using Autofac; using Autofac.Extensions.DependencyInjection;namespace TianYa.DotNetShare.CoreAutofacMvcDemo {public class Startup{public Startup(IConfiguration configuration){Configuration configuration;}public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container.public IServiceProvider ConfigureServices(IServiceCollection services){services.ConfigureCookiePolicyOptions(options {// This lambda determines whether user consent for non-essential cookies is needed for a given request.options.CheckConsentNeeded context true;options.MinimumSameSitePolicy SameSiteMode.None;});services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);return RegisterAutofac(services); //注册Autofac}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}else{app.UseExceptionHandler(/Home/Error);}app.UseStaticFiles();app.UseCookiePolicy();app.UseMvc(routes {routes.MapRoute(name: default,template: {controllerHome}/{actionIndex}/{id?});});}/// summary/// 注册Autofac/// /summaryprivate IServiceProvider RegisterAutofac(IServiceCollection services){//实例化Autofac容器var builder new ContainerBuilder();//将services中的服务填充到Autofac中builder.Populate(services);//新模块组件注册 builder.RegisterModuleAutofacModuleRegister();//创建容器var container builder.Build();//第三方IoC容器接管Core内置DI容器 return new AutofacServiceProvider(container);}} } PS需要将自带的ConfigureServices方法的返回值改成IServiceProvider 接下来我们来看看控制器里面怎么弄 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using TianYa.DotNetShare.CoreAutofacMvcDemo.Models;using TianYa.DotNetShare.Service; using TianYa.DotNetShare.Repository;namespace TianYa.DotNetShare.CoreAutofacMvcDemo.Controllers {public class HomeController : Controller{/// summary/// 定义仓储层学生抽象类对象/// /summaryprotected IStudentRepository StuRepository;/// summary/// 定义服务层学生抽象类对象/// /summaryprotected IStudentService StuService;/// summary/// 通过构造函数进行注入/// 注意参数是抽象类而非实现类因为已经在Startup.cs中将实现类映射给了抽象类/// /summary/// param namestuRepository仓储层学生抽象类对象/param/// param namestuService服务层学生抽象类对象/parampublic HomeController(IStudentRepository stuRepository, IStudentService stuService){this.StuRepository stuRepository;this.StuService stuService;}public IActionResult Index(){var stu1 StuRepository.GetStuInfo(10000);var stu2 StuService.GetStuInfo(10001);string msg $学号10000姓名{stu1.Name}性别{stu1.Sex}年龄{stu1.Age}br /;msg $学号10001姓名{stu2.Name}性别{stu2.Sex}年龄{stu2.Age};return Content(msg, text/html, System.Text.Encoding.UTF8);}public IActionResult Privacy(){return View();}[ResponseCache(Duration 0, Location ResponseCacheLocation.None, NoStore true)]public IActionResult Error(){return View(new ErrorViewModel { RequestId Activity.Current?.Id ?? HttpContext.TraceIdentifier });}} } 至此完成处理接下来就是见证奇迹的时刻了我们来访问一下/home/index看看是否能返回学生信息。 可以发现返回了学生的信息说明我们注入成功了。 至此本章就介绍完了如果你觉得这篇文章对你有所帮助请记得点赞哦谢谢 demo源码 链接https://pan.baidu.com/s/1un6_wgm6w_bMivPPRGzSqw 提取码lt80   版权声明如有雷同纯属巧合如有侵权请及时联系本人修改谢谢 转载于:https://www.cnblogs.com/xyh9039/p/11379831.html
http://wiki.neutronadmin.com/news/427527/

相关文章:

  • 网站制作人员临沂网站建站专业公司
  • 黄岗住房和城乡建设厅官方网站dede后台做两个网站
  • 优秀网站设计欣赏案例在阿里云服务器做淘客网站
  • 网站建设所需基本资料用户管理系统admin
  • 网站设计与运营wordpress商城建站教程
  • 大型门户网站建设需要哪些技术品牌策划大赛优秀作品
  • 酒店网站制作wordpress删除缓存会删掉文件吗
  • 沈阳做网站费用网页模板下载网址
  • 同一ip 网站 权重企业网络营销策划方案设计的例子
  • 传统的网站开发模式手机网站悬浮广告代码
  • 昆明网站建设首选才力广州手机网站建设报价表
  • 网站吸引人的功能建筑行业招聘网站排行榜
  • 百度官网地址企业网站seo分析
  • 吉林市做网站做网站怎么这么贵
  • 广州一起做网店网站官方培训网站完整页面
  • wordpress add_menu_pageseo优化网站教程
  • 做网站如何避免侵权网站源码小千个人网
  • 分类网站模版河南省干部任免最新公示
  • 互动科技 网站网络培训远程教育平台
  • 贵阳网站建设钟鼎网络如何在百度上找网站
  • 做淘客网站要什么样服务器山东天齐建设集团网站
  • 网站网页区别是什么网站建设服务平台
  • 企业网站建设要求标准说明全国各大网站
  • 网站怎样建设p2p理财网站开发框架
  • 建设工程网站单位名单下载 做网站的原型文件
  • 做落地页的网站学校网站 建设 价格
  • 深圳地产网站制作公司住建设部官方网站
  • 海外推广渠道网站seo主管招聘
  • 慈溪建设企业网站dede wordpress
  • 做外贸上不了国外网站学校网站查询