代码拉取完成,页面将自动刷新
IOC 与 DI
什么是 IOC
Inversion Of Control ,即控制反转 , 是一种编程思想,将对象的创建、销毁等任务交给 IOC 容器来完成,而不需要程序手动创建和销毁.
IOC 实现方式
DI
Dependency Injection , 即依赖注入,是一种实现 IOC 的技术,将所依赖对象的创建、销毁和依赖关系交给外部的 IOC 容器来管理,从而实现对象之间解耦
IOC 与 DI 意义
DI 方式
常用的 IOC 容器
不使用 IOC 和 DI
// IStudentDAL
public interface IStudentDAL
{
public void Create();
}
// StudentDAL
public class StudentDAL : IStudentDAL
{
public void Create()
{
Console.WriteLine("DAL-访问数据库,实现学员信息添加操作");
}
}
// IStudentDAL
public interface IStudentBLL
{
public void Create();
}
// StudentDAL
public class StudentBLL : IStudentBLL
{
private IStudentDAL studentDAL = new StudentDAL();
public void Create()
{
this.studentDAL.Create();
Console.WriteLine("BLL-通过调用StudentDAL实现学员添加操作");
}
}
static void Main(string[] args)
{
IStudentBLL studentBLL = new StudentBLL();
studentBLL.Create();
}
使用 IOC 和 DI
// IStudentDAL
public interface IStudentDAL
{
public void Create();
}
// StudentDAL
public class StudentDAL : IStudentDAL
{
public void Create()
{
Console.WriteLine("DAL-访问数据库,实现学员信息添加操作");
}
}
// IStudentDAL
public interface IStudentBLL
{
public void Create();
}
// StudentDAL
public class StudentBLL : IStudentBLL
{
private IStudentDAL studentDAL = new StudentDAL();
public void Create()
{
this.studentDAL.Create();
Console.WriteLine("BLL-通过调用StudentDAL实现学员添加操作");
}
}
static void Main(string[] args)
{
// IOC 容器
ServiceCollection services = new ServiceCollection();
// DI 依赖注入
services.AddTransient(typeof(StudentBLL));
services.AddTransient<IStudentDAL, StudentDAL>();
// 通过容器实例化对象
ServiceProvider provider = services.BuildServiceProvider();
StudentBLL studentBLL = provider.GetService(typeof(StudentBLL)) as StudentBLL;
studentBLL.Create();
}
Asp.net MVC 中的应用
// IStudentDAL
public interface IStudentDAL
{
public void Create();
}
// StudentDAL
public class StudentDAL : IStudentDAL
{
public void Create()
{
Console.WriteLine("DAL-访问数据库,实现学员信息添加操作");
}
}
// IStudentDAL
public interface IStudentBLL
{
public void Create();
}
// StudentDAL
public class StudentBLL : IStudentBLL
{
private IStudentDAL studentDAL = new StudentDAL();
public void Create()
{
this.studentDAL.Create();
Console.WriteLine("BLL-通过调用StudentDAL实现学员添加操作");
}
}
[Route("api/[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
private IStudentBLL studentBLL;
public StudentController(IStudentBLL studentBLL)
{
this.studentBLL = studentBLL;
}
[HttpPost]
[Route("create")]
public string Create()
{
this.studentBLL.Create();
return "学生创建成功";
}
}
builder.Services.AddSingleton<IStudentBLL, StudentBLL>();
builder.Services.AddSingleton<IStudentDAL, StudentDAL>();
public class DependenceInjectionAttribute : Attribute
{
public InstanceType InstanceType { set; get; }
public DependenceInjectionAttribute(InstanceType instanceType = InstanceType.SINGLE)
{
this.InstanceType = instanceType;
}
}
public enum InstanceType
{
SINGLE,
MULTIPLE
}
public static class IOCHelper
{
/// <summary>
/// 容器字典
/// </summary>
public static Dictionary<Type, object> IocContainer = new Dictionary<Type, object>();
/// <summary>
/// 注册特定类型到IoC容器中
/// </summary>
/// <param name="type"></param>
/// <exception cref="Exception"></exception>
public static void Register(Type type)
{
if (DoSingleInstance(type))
{
//如果该类包含定制的属性且为要求执行单实例,则直接实例化
var instance = Activator.CreateInstance(type, null);
if (instance != null)
{
IocContainer.Add(type, instance);
}
else
{
throw new Exception(string.Format("实例化对象{0}失败!", type));
}
}
}
/// <summary>
/// 判断执行单实例化还是多实例化
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static bool DoSingleInstance(Type type)
{
Attribute[] attrs = Attribute.GetCustomAttributes(type);
foreach (var attr in attrs)
{
if (attr is DependenceInjectionAttribute)
{
var di = (DependenceInjectionAttribute)attr;
if (di.InstanceType == InstanceType.SINGLE)
{
return true;
}
}
}
return false;
}
/// <summary>
/// 实例化:
/// 单实例使用默认构造函数
/// 多实例可以自定义构造函数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="args"></param>
/// <returns></returns>
public static T Resolve<T>(params object[] args)
{
if (DoSingleInstance(typeof(T)))
{
if (IocContainer.ContainsKey(typeof(T)))
return (T)IocContainer[typeof(T)];
}
else
{
return (T)Activator.CreateInstance(typeof(T), args);
}
return default(T);
}
}
[DependenceInjectionAttribute()]
public class Student
{
public void SayHi() {
Console.WriteLine("Hi");
}
}
static void Main(string[] args)
{
// 注册依赖注入类型
IOCHelper.Register(typeof(Student));
// 通过容器实例化对象
var student = IOCHelper.Resolve<Student>();
// 测试方法
student.SayHi();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。