我的DAL设计--- ASP.NET MVC





3.00/5 (5投票s)
带有通用接口和依赖注入的存储库模式。
为什么我们需要DAL?
以前,使用ADO.NET访问和操作数据库。DAL用于标准化和打包数据库操作类,其主要目标是实现各种数据库的操作分解。在我们迁移到Entity Framework操作数据库之后,我们使用ORM来操作数据库。但我认为仍然有必要将业务逻辑层和数据库操作方法(例如EF)解耦。毕竟,也许将来会出现一种新的技术来替代EF。你无法想象你会检查你所有的BLL层cs文件,用一种新兴的技术来替换所有DbContext对象和其他EF的东西。在另一种情况下,根据一些不同的需求环境,你可能需要在你的项目中将EF更改为NHibernate或ADO.NET。所以,我想创建一个持久性数据库访问层来操作数据。
引言
ASP.NET MVC框架基于泛型和依赖注入。
通常我采用接口仓储模式来构建DAL。在WebForm时代,我们通常从数据库设计开始初始化一个项目。使用MVC时,我们是面向模型进行编码。因此,在cshtml文件和DAL cs文件中,我们都需要知道我们想要获取或发送什么表或模型数据。所以,泛型是一个很好的使用方法。依赖注入是一个更好的系统方法来帮助我们定位实现类。当然,你也可以像过去我们使用的方法那样自己编写代码。
说了这么多,展示我的代码
一、在public class Startup中获取配置
如果你想使用配置文件来识别你想要的数据库操作方式,你可能会将代码添加到你的配置文件或json文件中。如果你想使用系统依赖注入,你可以忽略这段代码。
"Dboperation": {
"Stylename": "Entityframeworkdal" /* Adonetdal、nhibernatedal(whatever Name You like)*/
},
// 1.config file way------------get config DBOStyle= Configuration["DBOperation:stylename"]; apppath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; appname = Assembly.GetExecutingAssembly().GetName().Name; //2.system injection way--- ---creat your container of dependency injection container = new UnityContainer(); container.RegisterType(typeof(IBaseDAL<>), typeof(EntityFrameworkDAL<> ) );
二、定义控制台、接口和实现类
public class SetDBOStyle<T> where T : class //---------identify the dbo style
{ private static readonly string DBOStylename = Startup.DBOStyle;
private SetDBOStyle() { }
public static object CreateDAL()
{
try
{
string a = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;
string b = a + '.' + DBOStylename + "`1[" + typeof(T).ToString() + "]";
Type DBOStyleClass = Type.GetType(b);
object pp = Activator.CreateInstance(DBOStyleClass);
return pp ;
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
} public interface IBaseDAL <T> where T :class
{
IList<basemodel> ReturnATableData();
}
public class EntityFrameworkDAL<T> : IBaseDAL<T> where T : class
{
//-----------according to EntityFrameworkDAL parameter<T>to gain data of a table-----------
public IList<basemodel> ReturnATableData()
{
try
{
var dbContext = new MyContext();
IDatabaseInitializer<MyContext> dbInitializer = null;
if (dbContext.Database.Exists())
{
dbInitializer = new DropCreateDatabaseIfModelChanges<MyContext>();
}
else
{
dbInitializer = new DropCreateDatabaseAlways<MyContext>();
}
Database.SetInitializer(dbInitializer);
List<T> resultlist = new List<T>();
Type Tablemodelclass = System.Type.GetType(typeof(T).ToString(), true);
DbSet aa = dbContext.Set(Tablemodelclass);
foreach (var item in aa)
{
resultlist.Add((T)item);
}
dbContext.Dispose();
//---------------convert model to basemodel-----------
List<basemodel> pp = new List<basemodel>();
BaseLibrary<basemodel, T>.DALmodeltoVIEWmodel(ref pp, resultlist);
return (pp);
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
public class ADONETDAL<T> : IBaseDAL<T> where T : class
{//repeat above function define with ado.net techique}
public class NHIBERNATEDAL<T> : IBaseDAL<T> where T : class
{/repeat above function define with NHIBERNATE techique}
}
三、在控制器中调用
public IActionResult Index( )--------------controller
{
//-----------------------invoke method one------
//* Idboperation = (IBaseDAL<people>)SetDBOStyle<people>.CreateDAL();*/
//-----------------------invoke method two
Idboperation = Startup.container.Resolve<IBaseDAL<people>>();
if (Idboperation != null)
{
IList<basemodel> temp = Idboperation.ReturnATableData();
List<people> PP = new List<people>();
BaseLibrary<people, basemodel>.DALmodeltoVIEWmodel(ref PP, temp);
return View(PP);
}
return View();
}
结论
通过使用DAL层,我们统一和标准化了数据库访问库,避免代码直接使用大量EF对象并导致大量重复的数据库操作工作。当我们需要修改和替换数据库访问类时,我们只需要修改我们的DAL代码。依赖注入是识别接口实现类的一种非常好的方法。