using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 封装泛型CRUD
{
public class TeacherInfoDAL<T> : BaseDAL<T> where T : new()
{
public override T Add(T obj)
{
Console.WriteLine("TeacherInfoDAL添加了一个对象");
return base.Add(obj);
}
public override int Del(T obj)
{
Console.WriteLine("TeacherInfoDAL删除了一个对象");
return base.Del(obj);
}
public override T Update(T obj)
{
Console.WriteLine("TeacherInfoDAL更新了一个对象");
return base.Update(obj);
}
public override IList<T> QueryList(Func<T, bool> whereLambada)
{
Console.WriteLine("TeacherInfoDAL查询了系列个对象");
return base.QueryList(whereLambada);
}
}
public class UserInfoDAL<T> : BaseDAL<T> where T : new()
{
public override T Add(T obj)
{
Console.WriteLine("UserInfoDAL添加了一个对象");
base.Add(obj);
return obj;
}
public override int Del(T obj)
{
Console.WriteLine("UserInfoDAL删除了一个对象");
return base.Del(obj);
}
public override T Update(T obj)
{
Console.WriteLine("UserInfoDAL更新了一个对象");
return base.Update(obj);
}
public override IList<T> QueryList(Func<T, bool> whereLambada)
{
Console.WriteLine("UserInfoDAL查询了系列个对象");
return base.QueryList(whereLambada);
}
}
public class BaseDAL<T> where T : new()
{
public virtual T Add(T obj)
{
return obj;
}
public virtual int Del(T obj)
{
return 0;
}
public virtual T Update(T obj)
{
return new T();
}
public virtual IList<T> QueryList(Func<T,bool> whereLambada )
{
IList<T> list = new List<T>();
foreach (var r in list)
{
if (whereLambada(r))
{
list.Add(r);
}
}
return list;
}
}
public abstract class BaseBLL<T> where T : new()
{
public BaseDAL<T> basedal;
public virtual T Add(T obj)
{
Console.WriteLine("基本业务处理add");
return new T();
}
public virtual int Del(T obj)
{
Console.WriteLine("基本业务处理del");
return 0;
}
public virtual T Update(T obj)
{
Console.WriteLine("基本业务处理update");
return new T();
}
public virtual IList<T> QueryList(Func<T, bool> whereLambada)
{
Console.WriteLine("基本业务处理QueryList");
return new List<T>();
}
public abstract void setDAL( );
}
public class UserInfoBLL<T>:BaseBLL<T> where T : new()
{
public UserInfoBLL()
{
setDAL();
}
public override T Add(T obj)
{
base.Add(obj);
Console.WriteLine("UserInfoBLL务处理add");
return basedal.Add(obj);
}
public override int Del(T obj)
{
base.Del(obj);
Console.WriteLine("UserInfoBLL务处理del");
return basedal.Del(obj);
}
public override T Update(T obj)
{
base.Update(obj);
Console.WriteLine("UserInfoBLL务处理update");
return basedal.Update(obj);
}
public override IList<T> QueryList(Func<T, bool> whereLambada)
{
base.QueryList(whereLambada);
Console.WriteLine("UserInfoBLL务处理querylist");
return basedal.QueryList(whereLambada);
}
public override void setDAL()
{
basedal=new UserInfoDAL<T>();
}
}
public class TeacheInfoBLL<T> : BaseBLL<T> where T : new()
{
public TeacheInfoBLL()
{
setDAL();
}
public override void setDAL()
{
basedal=new TeacherInfoDAL<T>();
}
public override T Add(T obj)
{
base.Add(obj);
Console.WriteLine("TeacheInfoBLL务处理add");
return basedal.Add(obj);
}
public override int Del(T obj)
{
base.Del(obj);
Console.WriteLine("TeacheInfoBLL务处理del");
return basedal.Del(obj);
}
public override T Update(T obj)
{
base.Update(obj);
Console.WriteLine("TeacheInfoBLL务处理update");
return basedal.Update(obj);
}
public override IList<T> QueryList(Func<T, bool> whereLambada)
{
base.QueryList(whereLambada);
Console.WriteLine("TeacheInfoBLL务处理querylist");
return basedal.QueryList(whereLambada);
}
}
class Program
{
static void Main(string[] args)
{
BaseBLL<Student> bll=new TeacheInfoBLL<Student>();
Student student=new Student();
student.StudentNo = 1;
bll.Add(new Student());
bll.Del(new Student());
bll.Update(new Student());
bll.QueryList(a => a.StudentNo != 0);
Console.WriteLine("------------------------");
BaseBLL<Teacher> bll2 = new UserInfoBLL<Teacher>();
Teacher t = new Teacher();
t.TeacherNO = 1;
bll2.Add(t);
bll2.Del(t);
bll2.Update(t);
bll2.QueryList(a => a.TeacherNO != 0);
Console.ReadKey();
}
}
public class Student
{
public int StudentNo { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string Class { get; set; }
public string DeptNO { get; set; }
}
public class Teacher
{
public int TeacherNO { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
public string DeptNo { get; set; }
}
}