一个小例子:
using System;
namespace IndependenceReverseTest1
{
class Program
{
static void Main(string[] args)
{
//通过传入不同的对象new PowerSupply() 与 new BadPowerSupply()
//DeskFan 中的IpowerSupply _powerSupply 可以实现不同的方法
//DeskFan 的构造函数也是IpowerSupply 类型, 可以传入继承IpowerSupply的一切方法
var test1 = new DeskFan(new PowerSupply());
var test2 = new DeskFan(new BadPowerSupply());
Console.WriteLine(test1.Work());
Console.WriteLine("--------------------");
Console.WriteLine(test2.Work());
Console.ReadKey();
}
interface IpowerSupply
{
int GetPower();
}
class PowerSupply : IpowerSupply
{
public int GetPower()
{
return 100;
}
}
class BadPowerSupply : IpowerSupply
{
public int GetPower()
{
return 500;
}
}
class DeskFan
{
private IpowerSupply _powerSupply;
public DeskFan(IpowerSupply powerSupply)
{
_powerSupply = powerSupply;
}
public string Work()
{
int power = _powerSupply.GetPower();
if (power <= 0)
{
return "Can not work!";
}
else if (power < 100)
{
return "Slow!";
}
else if (power < 200)
{
return "Work fine!";
}
else
{
return "FBI Waring!";
}
}
}
}
}
举例说明: 实际应用中, 现在有2张表, CargoType和Member:
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码已从模板生成。
//
// 手动更改此文件可能导致应用程序出现意外的行为。
// 如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------
namespace CMS.Models
{
using System;
using System.Collections.Generic;
public partial class CargoType
{
public int Id { get; set; }
public string Name { get; set; }
public string Tag { get; set; }
public System.DateTime InsertDate { get; set; }
public int MemberId { get; set; }
public string MemberName { get; set; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码已从模板生成。
//
// 手动更改此文件可能导致应用程序出现意外的行为。
// 如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------
namespace CMS.Models
{
using System;
using System.Collections.Generic;
public partial class Member
{
public int Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public int Role { get; set; }
public System.DateTime InsertDate { get; set; }
}
}
使用常规方法, 使用EntityFramework和接口实现数据库CRUD
建立IProvider接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
/// <summary>
/// 接口
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IProvider<T> where T : class
{
/// <summary>
/// 查询所有记录
/// </summary>
/// <returns></returns>
List<T> Select();
/// <summary>
/// 插入一条记录
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
int Insert(T t);
/// <summary>
/// 修改一条记录
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
int Update(T t);
/// <summary>
/// 删除一条记录
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
int Delete(T t);
}
}
建立CargoTypeProvider
using CMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
//alt + enter
public class CargoTypeProvider : IProvider<CargoType>
{
private CargoEntities db = new CargoEntities();
public int Delete(CargoType t)
{
if (t == null) return 0;
var model = db.CargoType.ToList().FirstOrDefault(item => t.Id == item.Id);
if (model == null) return 0;
db.CargoType.Remove(model);
int count = db.SaveChanges();
return count;
}
public int Insert(CargoType t)
{
if (t == null) return 0;
if (string.IsNullOrEmpty(t.Name)||string.IsNullOrEmpty(t.MemberName)) return 0;
if (t.InsertDate == null) return 0;
db.CargoType.Add(t);
int count = db.SaveChanges();
return count;
}
public List<CargoType> Select()
{
return db.CargoType.ToList();
}
public int Update(CargoType t)
{
if (t == null) return 0;
return 0;
}
}
}
Member Provider:
using CMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class MemberProvider : IProvider<Member>
{
private CargoEntities db = new CargoEntities();
public int Delete(Member t)
{
if (t == null) return 0;
var model = db.Member.ToList().FirstOrDefault(item => item.Id == t.Id);
if (model != null)
{
db.Member.Remove(model);
return db.SaveChanges();
}
return 0;
}
public int Insert(Member t)
{
if (t == null) return 0;
db.Member.Add(t);
return db.SaveChanges();
}
public List<Member> Select()
{
return db.Member.ToList();
}
public int Update(Member t)
{
if (t == null) return 0;
var model = db.Member.ToList().FirstOrDefault(item => item.Id == t.Id);
if (model != null)
{
model.Password = t.Password;
model.Role = t.Role;
int count = db.SaveChanges();
return count;
}
return 0;
}
}
}
如果使用依赖反转的思想:
建立IProvider1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMS.Models2
{
public interface IProvider1
{
void Select();
void Insert();
void Update();
void Delete();
}
}
建立CargoTypeProvider1与MemberProvider1
using CMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMS.Models2
{
class CargoTypeProvider1 : IProvider1
{
private CargoEntities db = new CargoEntities();
//接收增删改 成功/失败
private int _count;
public int Count { get => _count; set => _count = value; }
//接收查询结果
List<CargoType> _cargoTypes;
public List<CargoType> CargoTypes { get => _cargoTypes; set => _cargoTypes = value; }
//接收外部输入对象
private CargoType _t;
public CargoTypeProvider1(CargoType t)
{
_t = t;
}
public CargoTypeProvider1()
{
}
public void Delete()
{
if (_t == null) _count = 0;
var model = db.CargoType.ToList().FirstOrDefault(item => _t.Id == item.Id);
if (model == null) _count = 0;
db.CargoType.Remove(model);
int count = db.SaveChanges();
_count = count;
}
public void Insert()
{
if (_t == null) _count = 0;
if (string.IsNullOrEmpty(_t.Name) || string.IsNullOrEmpty(_t.MemberName)) _count = 0;
if (_t.InsertDate == null) _count = 0;
db.CargoType.Add(_t);
int count = db.SaveChanges();
_count = count;
}
public void Select()
{
_cargoTypes = db.CargoType.ToList();
}
public void Update()
{
if (_t == null) _count = 0;
var model = db.CargoType.ToList().FirstOrDefault(item => _t.Id == item.Id);
if (model == null) _count = 0;
model.MemberName = _t.MemberName;
model.Name = _t.Name;
model.Tag = _t.Tag;
model.MemberId = _t.MemberId;
int count = db.SaveChanges();
_count = count;
}
}
}
using CMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMS.Models2
{
public class MemberProvider1 : IProvider1
{
private CargoEntities db = new CargoEntities();
private int _count;
public int Count { get => _count; set => _count = value; }
private List<Member> _members;
public List<Member> Members { get => _members; set => _members = value; }
//接收外部数据
private Member _t;
public MemberProvider1(Member t)
{
_t = t;
}
public MemberProvider1()
{
}
public void Delete()
{
if (_t == null) _count = 0;
var model = db.Member.ToList().FirstOrDefault(item => _t.Id == item.Id);
if (model == null) _count = 0;
db.Member.Remove(model);
int count = db.SaveChanges();
_count = count;
}
public void Insert()
{
if (_t == null) _count = 0;
if (_t.InsertDate == null) _count = 0;
db.Member.Add(_t);
int count = db.SaveChanges();
_count = count;
}
public void Select()
{
Members = db.Member.ToList();
}
public void Update()
{
if (_t == null) _count = 0;
var model = db.Member.ToList().FirstOrDefault(item => _t.Id == item.Id);
if (model == null) _count = 0;
model.Role = _t.Role;
model.Name = _t.Name;
model.Password = _t.Password;
int count = db.SaveChanges();
_count = count;
}
}
}
建立UserProvider,再次封装, 统一调用, 只需要将MemberProvider1 或者 CargoTypeProvider1 输入 UserProvider, 由于接口为父类, 可以实现方法的多态化.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMS.Models2
{
public class UserProvider
{
private IProvider1 _provider1;
public UserProvider(IProvider1 provider1)
{
_provider1 = provider1;
}
public void Delete()
{
_provider1.Delete();
}
public void Select()
{
_provider1.Select();
}
public void Update()
{
_provider1.Update();
}
public void Insert()
{
_provider1.Insert();
}
}
}
测试 :
using CMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMS.Models2
{
public class Test1
{
public Test1(CargoType t)
{
var t1 = new UserProvider(new CargoTypeProvider1(t));
//增删改
t1.Insert();
t1.Delete();
t1.Update();
//查询
List<CargoType> cargoSelect = new CargoTypeProvider1().CargoTypes;
}
public Test1(Member t)
{
var t2 = new UserProvider(new MemberProvider1(t));
t2.Insert();
t2.Delete();
t2.Update();
List<Member> memberSelct = new MemberProvider1().Members;
}
}
}
接口隔离:
using System;
namespace DependencyInjection
{
class Program
{
static void Main(string[] args)
{
var driver = new Driver(new HeavyTank());
driver.Drive();
//接口隔离的好处就是, 有选择性的继承对象的一部分属性
//在这里, driver并没有Fire方法, 只有Diver()
var dv1 = driver as ITank;
dv1.Fire();
//此处的dv1 有了Fire() 因为传进来的HeavyTank 继承了 ITank, 而ITank继承了IWeapon
var dv2 = (ITank)driver;
dv2.Fire();
//此处的dv2 也有了Fire()
Console.ReadKey();
}
}
class Driver
{
private IVehicle _vehicle; //此处使用了接口隔离,
public Driver(IVehicle vehicle)
{
_vehicle = vehicle;
}
public void Drive()
{
_vehicle.Run();
}
}
interface IVehicle
{
public void Run();
}
interface IWeapon
{
public void Fire();
}
interface ITank:IVehicle,IWeapon
{
}
//interface ITank
//{
// void Fire();
// void Run();
//}
class Car : IVehicle
{
public void Run()
{
Console.WriteLine("Car is running");
}
}
class Truck : IVehicle
{
public void Run()
{
Console.WriteLine("Truck is running");
}
}
class LightTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!");
}
public void Run()
{
Console.WriteLine("Ka ka ka!");
}
}
class MediumTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!!");
}
public void Run()
{
Console.WriteLine("Ka ka ka!!");
}
}
class HeavyTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!!!");
}
public void Run()
{
Console.WriteLine("Ka ka ka!!!");
}
}
}