项目结构图:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XU.Model
{
public abstract class BaseModel
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XU.Model
{
public class User :BaseModel
{
public string UserName { get; set; }
public string PassWord { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XU.Model;
namespace XU.IDAL
{
public interface IBaseDAL<T> where T : XU.Model.BaseModel, new()
{
bool Add(T model);
bool Delete(int ID);
bool Update(T model);
T GetModel(int ID);
}
public interface IUserDAL : IBaseDAL<XU.Model.User>
{
/// <summary>
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
bool Exists(string userName);
}
}
using System;
using XU.IDAL;
using XU.Model;
namespace XU.MSSQLDAL
{
public class UserDAL : XU.IDAL.IUserDAL
{
public bool Exists(string userName)
{
return true;
}
public bool Add(Model.User model)
{
return true;
}
public bool Delete(int ID)
{
return true;
}
public bool Update(Model.User model)
{
return true;
}
public Model.User GetModel(int ID)
{
return new XU.Model.User() { UserName = "MSSQL", PassWord = "123456" };
}
}
}
using System;
namespace XU.FactoryDAL
{
public class DataAccess<T> where T : class
{
//获取配置路径
private static readonly string path = "XU.MSSQLDAL";//System.Configuration.ConfigurationManager.AppSettings["DAL"];
private DataAccess() { }
/// <summary>
/// 创建实例
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static T CreateDAL(string type)
{
string className = string.Format(path + ".{0}", type);
try
{
return (T)System.Reflection.Assembly.Load(path).CreateInstance(className);
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
}
}
using System;
using XU.FactoryDAL;
namespace XU.BLL
{
public class BaseBLL<T> where T : XU.Model.BaseModel, new()
{
protected XU.IDAL.IBaseDAL<T> Dal;
public BaseBLL(string type)
{
Dal = XU.FactoryDAL.DataAccess<XU.IDAL.IBaseDAL<T>>.CreateDAL(type);
}
public virtual bool Add(T model)
{
return Dal.Add(model);
}
public virtual bool Delete(int ID)
{
return Dal.Delete(ID);
}
public virtual bool Update(T model)
{
return Dal.Update(model);
}
public virtual T GetModel(int ID)
{
return Dal.GetModel(ID);
}
}
}
using System;
using XU.Model;
namespace XU.BLL
{
public class UserBLL : BaseBLL<XU.Model.User>
{
private const string _Type = "UserDAL";
private XU.IDAL.IUserDAL _Dal;
public UserBLL()
: base(_Type)
{
_Dal = base.Dal as XU.IDAL.IUserDAL;
if (_Dal == null)
{
throw new NullReferenceException(_Type);
}
}
public bool Exists(string userName)
{
return _Dal.Exists(userName);
}
}
}
XU.BLL.UserBLL userBLL = new XU.BLL.UserBLL();
XU.Model.User user=new XU.Model.User();
int id = 1;
Console.WriteLine("添加------{0}", userBLL.Add(user));