依赖注入(ID)是一种实现对象及其合作者或者依赖想之间松散耦合的技术
对于传统的方法来说,获取类的方法通常用new如下

 public class DIController : Controller
{
public IActionResult Index()
{
MyServices my = new MyServices();
my.getName();
return View();
}
}

但是问题来了,如果我后期要修改MyServices类的时候,就需要在整个项目中来搜索修改

一:接口注入

为了实现解耦,我们需要建立IServices的接口,然后类实现接口,在控制器中进行构造函数,初始化接口,然后使用接口来进行操作,这样就达到了解耦的目的,俗称依赖注入

具体类代码如下

 namespace CoreWeb2.Services
{
public class MyServices:IServices
{
public string getName()
{
return "张三";
}
}
public class OServices:IServices
{
public string getName()
{
return "李四";
}
}
public interface IServices {
string getName();
}
}

控制器代码如下:

 namespace CoreWeb2.Controllers
{
public class DIController : Controller
{
/// <summary>
/// 通过构造函数初始化接口
/// 在外界使用时候直接new出具体类,这样就达到了程序解耦的目的,俗称依赖注入
/// </summary>
public IServices _services;
public DIController(IServices services)
{
this._services = services;
}
public IActionResult Index()
{
//通过接口调用
_services.getName();
return View();
}
}
}

但是问题又来了,因为控制器默认构造函数是一个无参的,现在增加了参数传递,直接访问会报错误,因而就需要在Startup.cs中的ConfigureServices方法中进行程序注入,将构造函数的参数注入到容器中,当程序访问时会自动在容器中找参数,才可以使用

依赖注入有三种方式:

1、AddTransient

  每个请求创建一个

2、AddScoped

  一个域创建一个

3、AddSingleton

  单例,整个应用程序生命周期以内只创建一个实例

 //每个请求创建一个
services.AddTransient<IServices, MyServices>();
//一个域创建一个
services.AddScoped<IServices, MyServices>();
//单例,整个应用程序生命周期以内只创建一个实例
services.AddSingleton<IServices, MyServices>();

当程序进行依赖注入后,控制器中就可以访问到当前的实例对象了,

 二:泛型注入

泛型接口

/// <summary>
/// 数据仓储
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public interface IRepository<TEntity> where TEntity : class
{
DbContext DbContext { get; } DbSet<TEntity> Entities { get; }
IQueryable<TEntity> Table { get; }
TEntity GetById(object id);
void Insert(TEntity engine, bool isSave = true);
void Update(TEntity engine, bool isSave = true);
void Delete(TEntity engine, bool isSave = true);
}

泛型接口实现

  public class EfRepository<TEntity> : IRepository<TEntity> where TEntity:class
{
private GeneralDbContext _dbContext;
public EfRepository(GeneralDbContext generalDbContext)
{
_dbContext = generalDbContext;
} public DbContext DbContext => _dbContext; public DbSet<TEntity> Entities => _dbContext.Set<TEntity>(); public IQueryable<TEntity> Table => Entities; public void Delete(TEntity engine, bool isSave = true)
{
Entities.Remove(engine);
if (isSave)
{
_dbContext.SaveChanges();
}
} public TEntity GetById(object id)
{
return _dbContext.Set<TEntity>().Find(id);
} public void Insert(TEntity engine, bool isSave = true)
{
Entities.Add(engine);
if (isSave)
{
_dbContext.SaveChanges();
}
} public void Update(TEntity engine, bool isSave = true)
{
Entities.Update(engine);
if (isSave)
{
_dbContext.SaveChanges();
}
}

泛型注入方式

//将泛型注入 services.AddScoped(typeof(IRepository<>),typeof(EfRepository<>));

关联类GeneralDbContext

 public class GeneralDbContext : DbContext
{
public GeneralDbContext(DbContextOptions options) : base(options)
{ }
public DbSet<Category> Categorys{ get; set; }
}

最新文章

  1. Visual Studio2015使用tinyfox2.x作为Owin Host调试教程
  2. 动画效果interpolator
  3. mysql集群数据一致性校验
  4. c# WebClient Get Post 方法
  5. TCP/IP三次握手和HTTP过程
  6. 阅读javascript高级程序设计
  7. 跨浏览器事件EventUtil
  8. C++模板元编程 - 挖新坑的时候探索到了模板元编程的新玩法
  9. ADO.NET学习系列(二)
  10. 160826、浏览器渲染页面过程描述,DOM编程技巧以及重排和重绘
  11. 【腾讯Bugly干货分享】手游热更新方案xLua开源:Unity3D下Lua编程解决方案
  12. Window vagrant 安装部署【转】
  13. ubuntu下编译源码级QT
  14. C#委托简介
  15. SpringMVC 快速入门
  16. windows server 2008使用nginx转发API异常解决办法
  17. AJAX编程实践
  18. Python-异常处理-66
  19. appium 与 selenium python解决python &#39;WebElement&#39; object does not support indexing 报错问题问题
  20. Mysql数据库基础学习笔记

热门文章

  1. jenkins+windows+springboot+.net项目自动化部署图文教程
  2. [Maven实战-许晓斌]-[第二章]-2.7-2.8 Mave安装的最优建议和安装小结
  3. [bzoj2816][ZJOI2012]网络(LCT,splay)
  4. PHP内核研究 静态变量
  5. [ActionScript 3.0] flash中的颜色
  6. JMeter—系统性能分析思路
  7. python在一堆目录中寻找json文件
  8. SPOJ - COT 路径构造主席树
  9. system命令
  10. 那些H5用到的技术(2)——音频和视频播放