DI和IOC是差不多的概念。
一个重要特征是接口依赖,是把对象关系推迟到运行时去确定.
DI是一个初始化实例的过程,分为三种1.setter based 2.constructor based 3.interface based,在spring 中就是应用前两种;但又不仅仅是初始化实例,而且是运用接口的概念去实现这种注入依赖。静态地看,只是依赖一个接口,但实际运行起来,是依赖一个实现了该接口的具体类。
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DIApp { //Model类 public class Movie { public int MovieID { get; set; } public string MovieName { get; set; } public decimal MovieCost { get; set; } public decimal MoviePrice { set; get; } } //模型数据类 public static class Movies { //模型数据数组,这里为了简化数据库数据获取,用静态数组替代,实际可以实现get/set方法 public static Movie[] movies = { new Movie {MovieID = 1, MovieName = "猩猿崛起II", MovieCost = 80000000.00M, MoviePrice = 80.00M}, new Movie {MovieID = 2, MovieName = "生化危机", MovieCost = 5000000.00M, MoviePrice = 50.00M}, new Movie {MovieID = 3, MovieName = "大话西游", MovieCost = 3000000.00M, MoviePrice = 35.00M}, new Movie {MovieID = 4, MovieName = "叶问II", MovieCost = 58000000.00M, MoviePrice = 80.00M}, new Movie {MovieID = 5, MovieName = "魔戒", MovieCost = 180000000.00M, MoviePrice = 60.00M}, new Movie {MovieID = 6, MovieName = "哈利波特", MovieCost = 170000000.00M, MoviePrice = 60.00M}, new Movie {MovieID = 7, MovieName = "高考1977", MovieCost = 9000000.00M, MoviePrice = 60.00M}, new Movie {MovieID = 8, MovieName = "纵横天下", MovieCost = 3150000.00M, MoviePrice = 40.00M}, new