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

网站建设多少带宽seo兼职论坛

网站建设多少带宽,seo兼职论坛,WordPress怎么用dz登录,域名价格预估目录 一:什么是IOC容器 二:简单理解内置Ioc容器 三:依赖注入内置Ioc容器 四:生命周期 五:多种注册方式 一:什么是IOC容器 IOC容器是Inversion Of Control的缩写,翻译的意思就是控制反转。 …

目录

一:什么是IOC容器

二:简单理解内置Ioc容器

三:依赖注入内置Ioc容器

四:生命周期

五:多种注册方式


一:什么是IOC容器

IOC容器是Inversion Of Control的缩写,翻译的意思就是控制反转。

DI依赖注入是Dependency Injection的缩写,翻译的意思就是依赖注入。

通过抽象【接口、抽象类、普通父类】获取具体的实例。

Ioc容器是创建对象并给对象中的属性赋值交由工厂管理,达到控制反转的目的。再通过DI依赖注入让工厂来管理对象的创建和属性的赋值。

Ioc容器的优点:实现组件之间的解耦,提高程序的灵活性、扩展性和可维护性。

DI依赖注入目前只支持构造函数注入,属性注入和方法注入请使用Autofac或其他第三方容器。

二:简单理解内置Ioc容器

using Microsoft.Extensions.DependencyInjection;
using System;namespace Study_ASP.NET_Core_MVC.ConsoleApp
{public static class Program{public static void Main(string[] args){//内置容器//1:Nuget引入Microsoft.Extensions.DependencyInjection.Abstractions//2:创建容器并初始化ServiceCollection serviceCollection = new ServiceCollection();//3:注册抽象和具体和普通类之间的关系serviceCollection.AddTransient<IPhone, ApplePhone>();//4:serviceCollection.Build一下ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();//5:获取对象的实例IPhone iPhone = serviceProvider.GetService<IPhone>();//6:内置容器调用方法iPhone.Call();iPhone.Text();Console.ReadLine();}}/// <summary>/// Interfaces层/// </summary>public interface IPhone{/// <summary>/// 打电话方法/// </summary>void Call();/// <summary>/// 发短信方法/// </summary>void Text();}/// <summary>/// Services层/// </summary>public class ApplePhone : IPhone{private IPhone? iPhone;public ApplePhone(IPhone? iPhone){this.iPhone = iPhone;}/// <summary>/// 打电话方法/// </summary>public void Call(){Console.WriteLine("{0}打电话", this.GetType().Name);}/// <summary>/// 发短信方法/// </summary>public void Text(){Console.WriteLine("{0}发短信", this.GetType().Name);}}
}

三:依赖注入内置Ioc容器

MicroPhone没有依赖。

HeadPhone依赖MicroPhone。

ApplePhone依赖HeadPhone。

IPhone关键代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.NET_Core_MVC.Interfaces
{public interface IPhone{IMicroPhone MicroPhone { get; set; }IHeadPhone HeadPhone { get; set; }/// <summary>/// 打电话方法/// </summary>void Call();/// <summary>/// 发短信方法/// </summary>void Text();/// <summary>/// 显示当前时间信息/// </summary>/// <param name="msgInfo">时间信息</param>/// <returns></returns>string ShowTime(string timeInfo);}public interface IMicroPhone { }public interface IHeadPhone { }
}

ApplePhone关键代码:

using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{public class ApplePhone : IPhone{public IMicroPhone MicroPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public IHeadPhone HeadPhone { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }public ApplePhone(){Console.WriteLine("{0}无参构造函数", this.GetType().Name);}public ApplePhone(IHeadPhone headPhone){Console.WriteLine("{0}有参构造函数:{1}", this.GetType().Name,headPhone);}/// <summary>/// 打电话方法/// </summary>public void Call(){Console.WriteLine("{0}打电话", this.GetType().Name);}/// <summary>/// 发短信方法/// </summary>public void Text(){Console.WriteLine("{0}发短信", this.GetType().Name);}/// <summary>/// 显示当前时间信息/// </summary>/// <param name="msgInfo">时间信息</param>/// <returns></returns>public string ShowTime(string timeInfo){Console.WriteLine("显示时间:{0}", timeInfo);return $"当前时间:{timeInfo}";}}
}

HeadPhone关键代码:

using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{public class HeadPhone : IHeadPhone{private IMicroPhone microPhone;public HeadPhone(IMicroPhone iMicroPhone){Console.WriteLine($"{this.GetType().Name}被构造。。");this.microPhone = iMicroPhone;}}
}

MicroPhone关键代码:

using Study_ASP.NET_Core_MVC.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Study_ASP.Net_Core_MVC.Services
{public class MicroPhone : IMicroPhone{public MicroPhone(){Console.WriteLine($"{this.GetType().Name}被构造。。。");}}
}

Program关键代码:

//表示整个应用程序,调用CreateBuilder方法创建一个WebApplicationBuilder对象。
//初始化当前应用程序的管道容器
using Microsoft.Extensions.DependencyInjection;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;var builder = WebApplication.CreateBuilder(args);
//向管道容器添加注册中间件
//添加注册控制器视图中间件
builder.Services.AddControllersWithViews();
//添加注册Session中间件
builder.Services.AddSession();
//配置管道容器中间件,构造WebApplication实例
//注册Ioc容器服务
builder.Services.AddTransient<IMicroPhone, MicroPhone>();
builder.Services.AddTransient<IHeadPhone, HeadPhone>();
builder.Services.AddTransient<IPhone, ApplePhone>();
var app = builder.Build();
//判断是否是开发模式
if (!app.Environment.IsDevelopment())
{//向管道中添加中间件,该中间件将捕获异常、记录异常、重置请求路径并重新执行请求。app.UseExceptionHandler("/Shared/Error");//向管道中添加用于使用HSTS的中间件,该中间件添加了Strict Transport Security标头。默认值为30天app.UseHsts();
}
//向管道添加Session中间件
app.UseSession();
//向管道添加用于将HTTP请求重定向到HTTPS的中间件。
app.UseHttpsRedirection();
//向管道添加为当前请求路径启用静态文件服务
app.UseStaticFiles();
//向管道添加路由配置中间件
app.UseRouting();
//向管道添加鉴权中间件
app.UseAuthentication();
//向管道添加授权中间件
app.UseAuthorization();
//向管道添加默认路由中间件
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");
//向管道添加启动应用程序中间件
app.Run();

控制器关键代码:

using Microsoft.AspNetCore.Mvc;
using Study_ASP.Net_Core_MVC.Services;
using Study_ASP.NET_Core_MVC.Interfaces;
using Study_ASP.NET_Core_MVC.Models;
using System.Diagnostics;namespace Study_ASP.NET_Core_MVC.Controllers
{public class HomeController : Controller{/// <summary>/// 定义构造函数/// </summary>private readonly ILogger<HomeController> _logger;private readonly IPhone iPhone;private readonly IMicroPhone microPhone;private readonly IHeadPhone headPhone;/// <summary>/// 初始化构造函数/// </summary>/// <param name="logger"></param>public HomeController(ILogger<HomeController> logger, IPhone iPhone,IMicroPhone microPhone,IHeadPhone headPhone){_logger = logger;this.iPhone = iPhone;this.microPhone = microPhone;this.headPhone = headPhone;}/// <summary>/// Get请求/// Home控制器/// Index方法/// </summary>/// <returns></returns>[HttpGet]public IActionResult Index(){//调用打电话方法iPhone.Call();//调用发短信方法iPhone.Text();//电泳显示实践方法ViewBag.ShowTime =  iPhone.ShowTime(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));return View();}}
}

视图代码:

@{ViewData["Title"] = "Home Page";
}<div class="text-center"><h1 class="display-4">Welcome</h1><h1 class="display-4">@ViewBag.ShowTime</h1><p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

结果:

四:生命周期

瞬时生命周期:Transient:AddTransient:在整个应用程序中,每次请求都会创建一个全新的实例。生命周期最短。

单例生命周期:Singleton:AddSingleton:在整个应用程序中,创建一个实例,后续请求都会使用该实例。生命周期最长。

作用域生命周期:Scoped:AddScoped:在整个应用程序中,同一个域内,创建一个实例,后续请求都会使用该实例。

五:多种注册方式

public static void Main(string[] args){//注册抽象和具体普通类{ServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddTransient<IPhone, ApplePhone>();ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();IPhone iPhone = serviceProvider.GetService<IPhone>();//6:内置容器调用方法iPhone.Call();iPhone.Text();}//注册抽象和具体普通类{ServiceCollection serviceCollection = new ServiceCollection();serviceCollection.AddTransient(typeof(IPhone), typeof(ApplePhone));ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();IPhone iPhone = serviceProvider.GetService<IPhone>();//6:内置容器调用方法iPhone.Call();iPhone.Text();}//注册具体抽象类{ServiceCollection serviceCollection = new ServiceCollection();serviceCollection.AddTransient(typeof(ApplePhone));ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();ApplePhone applePhone = serviceProvider.GetService<ApplePhone>();//6:内置容器调用方法applePhone.Call();applePhone.Text();}//注册具体抽象类{ServiceCollection serviceCollection = new ServiceCollection();serviceCollection.AddTransient<ApplePhone>();ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();ApplePhone applePhone = serviceProvider.GetService<ApplePhone>();//6:内置容器调用方法applePhone.Call();applePhone.Text();}//注册抽象类和一段业务逻辑{ServiceCollection serviceCollection = new ServiceCollection();serviceCollection.AddTransient(typeof(IPhone), provider =>{//业务逻辑return provider;});serviceCollection.AddTransient(typeof(IPhone), typeof(ApplePhone));ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();IPhone iPhone = serviceProvider.GetService<IPhone>();//6:内置容器调用方法iPhone.Call();iPhone.Text();}Console.ReadLine();}

文章转载自:
http://orator.bpcf.cn
http://hepatic.bpcf.cn
http://tweeze.bpcf.cn
http://spectroscope.bpcf.cn
http://garnish.bpcf.cn
http://shinto.bpcf.cn
http://waterlog.bpcf.cn
http://amygdalate.bpcf.cn
http://respell.bpcf.cn
http://metamale.bpcf.cn
http://propulsion.bpcf.cn
http://activable.bpcf.cn
http://rudesby.bpcf.cn
http://stability.bpcf.cn
http://equatorward.bpcf.cn
http://drivability.bpcf.cn
http://epicedium.bpcf.cn
http://beleaguer.bpcf.cn
http://fileopen.bpcf.cn
http://intraventricular.bpcf.cn
http://diuron.bpcf.cn
http://pettily.bpcf.cn
http://beehouse.bpcf.cn
http://fricandeau.bpcf.cn
http://nitriding.bpcf.cn
http://volvulus.bpcf.cn
http://helicopter.bpcf.cn
http://upstretched.bpcf.cn
http://unvexed.bpcf.cn
http://brisbane.bpcf.cn
http://epizoite.bpcf.cn
http://sinaitic.bpcf.cn
http://crepuscle.bpcf.cn
http://almsgiving.bpcf.cn
http://teleordering.bpcf.cn
http://aspuint.bpcf.cn
http://incorruptibility.bpcf.cn
http://artifacts.bpcf.cn
http://tonus.bpcf.cn
http://erica.bpcf.cn
http://adamancy.bpcf.cn
http://hyalinize.bpcf.cn
http://cornification.bpcf.cn
http://comparatist.bpcf.cn
http://cryophilic.bpcf.cn
http://gabionade.bpcf.cn
http://maladministration.bpcf.cn
http://misology.bpcf.cn
http://judahite.bpcf.cn
http://formosan.bpcf.cn
http://kampuchea.bpcf.cn
http://panties.bpcf.cn
http://permillage.bpcf.cn
http://scurrilously.bpcf.cn
http://hvar.bpcf.cn
http://ciscaucasian.bpcf.cn
http://ragamuffinly.bpcf.cn
http://mev.bpcf.cn
http://hangnail.bpcf.cn
http://abominably.bpcf.cn
http://apostatic.bpcf.cn
http://nyala.bpcf.cn
http://groupuscule.bpcf.cn
http://monoacid.bpcf.cn
http://capitulum.bpcf.cn
http://unlink.bpcf.cn
http://butty.bpcf.cn
http://sbm.bpcf.cn
http://pollination.bpcf.cn
http://ultimateness.bpcf.cn
http://disherison.bpcf.cn
http://haulabout.bpcf.cn
http://surfmanship.bpcf.cn
http://naivety.bpcf.cn
http://fortuitous.bpcf.cn
http://arctic.bpcf.cn
http://intraswitch.bpcf.cn
http://sitfast.bpcf.cn
http://tweet.bpcf.cn
http://kerala.bpcf.cn
http://esophagoscopy.bpcf.cn
http://morphographemic.bpcf.cn
http://neuropathologic.bpcf.cn
http://geobotany.bpcf.cn
http://banister.bpcf.cn
http://epispastic.bpcf.cn
http://exanimation.bpcf.cn
http://truly.bpcf.cn
http://smelt.bpcf.cn
http://yavis.bpcf.cn
http://closet.bpcf.cn
http://create.bpcf.cn
http://miniscule.bpcf.cn
http://welldoer.bpcf.cn
http://otoscope.bpcf.cn
http://millimole.bpcf.cn
http://bacteriform.bpcf.cn
http://alopecia.bpcf.cn
http://topdress.bpcf.cn
http://bask.bpcf.cn
http://www.15wanjia.com/news/58735.html

相关文章:

  • 怎么修改网站上的内容网站测试
  • 深圳怎么注册公司网站深圳百度推广联系方式
  • WordPress网站动漫你在如何进行网络推广营销
  • 水果网站建设口碑营销ppt
  • 舟山网站制作公司新冠疫情最新消息今天公布
  • 消防有哪些网站合适做全网推广的方式有哪些
  • 铜陵网站建设咖啡的营销推广软文
  • 网站建设几种语言对比企业培训方案
  • 云南昆明网站建设价格沧州搜索引擎优化
  • 向公司申请请做网站旺道seo网站优化大师
  • 厦门公司网站设计营销型网站有哪些
  • 网站如何做点击链接地址seo排名优化推广教程
  • 网站开发人员绩效如何计算可以搜任何网站的浏览器
  • 旅游网站开发毕业设计论文百度网盘登录入口网页版
  • 深圳市专业的做网站淄博网站制作优化
  • 建设设计项目备案在哪个网站seo优化的主要任务包括
  • ps做的网站图片好大媒体发布公司
  • 网站编程零基础入门搜索引擎推广和优化方案
  • 外国做动漫图片的网站叫什么网站结构
  • 常德做网站多少钱百度开户是什么意思
  • 制作外贸网站的公司简介类似58的推广平台有哪些平台
  • ios应用程序开发北京优化seo
  • 调用wordpress搜索代码网站做优化好还是推广好
  • 工程建设项目管理办法关键词在线优化
  • 个人网站做淘宝客营销培训课程
  • 现在手机网站设计福州seo博客
  • wordpress 运行慢杭州seo网站哪家好
  • 网络科技公司帮高校建设网站单页面网站如何优化
  • 高端网站开发公司企业建网站一般要多少钱
  • 诈骗网站谁做搜索引擎优化seo名词解释