EFDAL层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dulei.IDAL;
using Dulei.Model;
namespace Dulei.DAL
{
public class UserInfoEFDal:IUserInfoDal 继承IUserInfoDal接口
{
public UserInfo ADD(UserInfo userInfo) 使用IUserInfoDal实现接口
{
DataModelContainer db = new DataModelContainer();
db.UserInfo.Add(userInfo);
db.SaveChanges();
return userInfo;
}
}
}
AdoNetDal层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dulei.IDAL;
namespace Dulei.AdoNetDAL
{
public class UserInfoAdoNetDal:IUserInfoDal 继承IUserInfoDal接口
{
public Model.UserInfo ADD(Model.UserInfo userInfo) 使用IUserInfoDal实现接口
{
throw new NotImplementedException();
}
}
}
1、建立接口层IDAL
2、IDal层创建了UserInfo实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dulei.Model;
namespace Dulei.IDAL
{
public interface IUserInfoDal
{
UserInfo ADD(UserInfo userInfo);
}
}
BLL层调用接口实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dulei.AdoNetDAL;
using Dulei.DAL;
using Dulei.IDAL;
using Dulei.Model;
namespace Dulei.BLL
{
public class UserInfoService
{
IDAL.IUserInfoDal userInfoDal =new UserInfoEFDal(); IDAL.IUserInfoDal实例。只需要改new UserInfoEFDal();new IUserInfoDal();就可以更换数据DAL层
public UserInfo AddUserInfo(UserInfo userInfo)
{
return userInfoDal.ADD(userInfo);
}
}
}
总结:更换数据层时就可以不用在去更改业务层代码了。只需要更改new的实例。
如果要改数据库还需要改new实例
二、new实例
上面已经对实例进行了接口的改良。下面对NEW 实例就行改良。
DalFactory类库增加类名为DALFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dulei.DAL;
namespace Dulei.DalFactory
{
public class DALFactory
{
public static IDAL.IUserInfoDal GetUserInfoDal()
{
return new UserInfoEFDal();
}
}
更改BLL层NEW的实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dulei.AdoNetDAL;
using Dulei.DAL;
using Dulei.IDAL;
using Dulei.Model;
namespace Dulei.BLL
{
public class UserInfoService
{
private IDAL.IUserInfoDal userInfoDal = DalFactory.GetUserInfoDal(); 通过DalFactory.GetUserInfoDal();拿到NEW的新实例
public UserInfo AddUserInfo(UserInfo userInfo)
{
return userInfoDal.ADD(userInfo); 以后更换DAL时只需要改return后面的代码,更换ado的数据库UserInfoAdoNetDal
}
}
}
虽然看上去简单,但是这里体现了面向对向的恩想。
新的问题要更数据库还要更改UserInfoDAL
传程序集进来。
namespace CZBK.OADemo.DalFactory
{
public class DALSimpleFactory
{
public static IDAL.IUserInfoDal GetUserInfoDal()
{
//如果直接new 的话那么必须 改代码的才能切换不同的数据库访问层。
//非常希望能做到:只改配置就能够创建出实例出来。也就是改变数据库访问层的实现。
//return GetInstences("CZBK.OADemo.DAL", "CZBK.OADemo.DAL." + "UserInfoDal") as IUserInfoDal;
return new UserInfoAdoNetDal();
}
public static Object GetInstences(string assemblyName, string typeName)
{
return Assembly.Load(assemblyName).CreateInstance(typeName);
//return null;
}
}
}
同过程序集来判断程序集的类型名字。
在去webconfig下
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="AssemblyName" value="Dulei.Dal" />
<add key="NameSpace" value="Dulei.Dal" />
</appSettings>
增加了
<add key="AssemblyName" value="Dulei.Dal" />
<add key="NameSpace" value="Dulei.Dal" />
通过更改appsettings来修改使用的数据库