/*
* 代理模式:
* 代理类成为实际想调用对象的中间件,可以控制对实际调用对象的访问权限
* 可以维护实际对象的引用
*/
1.代理类
namespace Data
{
public interface IProxy
{
string InsertDB();
string UpdateDB();
string DelDB();
}
public class Proxy : IProxy
{
OperateDB operateDB;
public Proxy()
{
operateDB = new OperateDB();
}
#region IProxy 成员
public string InsertDB()
{
return operateDB.InsertDB();
}
public string UpdateDB()
{
return operateDB.UpdateDB();
}
public string DelDB()
{
return operateDB.DelDB();
}
#endregion
}
class OperateDB : IProxy
{
#region IProxy 成员
public string InsertDB()
{
NorthwindEntities db = new NorthwindEntities();
Employees e = new Employees();
e.LastName = "chuanshi_yoyo_yoyo";
e.FirstName = "zhushao";
db.AddToEmployees(e);
db.SaveChanges();
return "添加数据成功\n";
}
public string UpdateDB()
{
NorthwindEntities db = new NorthwindEntities();
Employees e = db.Employees.Where(c => c.EmployeeID == 43).FirstOrDefault();
e.LastName = "yo_shao";
db.SaveChanges();
return "修改数据成功\n";
}
public string DelDB()
{
NorthwindEntities db = new NorthwindEntities();
Employees e = db.Employees.Where(c => c.EmployeeID == 43).FirstOrDefault();
db.DeleteObject(e);
db.SaveChanges();
return "删除数据成功\n";
}
#endregion
}
}
2.调用类
Proxy proxy = new Proxy();
Console.WriteLine(proxy.InsertDB());
Console.ReadKey();