我目前正在构建基于Web的系统,并尝试在SOA架构中使用DTO实现N层实体框架4.0。我在理解如何实现数据访问层(DAL),业务逻辑层(BLL)和表示层时遇到了问题。
我们假设我有一个“useraccount”实体具有以下内容:
编号
姓
名字
AuditFields_InsertDate
AuditFields_UpdateDate
在DAL中,我创建了一个类“UserAccountsData.cs”,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrderSystemDAL
{
public static class UserAccountsData
{
public static int Insert(string firstName, string lastName, DateTime insertDate)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
return Insert(db, firstName, lastName, insertDate);
}
}
public static int Insert(OrderSystemEntities db, string firstName,
string lastName, DateTime insertDate)
{
return db.UserAccounts_Insert(firstName, lastName, insertDate, insertDate).ElementAt(0).Value;
}
public static void Update(int id, string firstName, string lastName,
DateTime updateDate)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
Update(db, id, firstName, lastName, updateDate);
}
}
public static void Update(OrderSystemEntities db, int id, string firstName,
string lastName, DateTime updateDate)
{
db.UserAccounts_Update(id, firstName, lastName, updateDate);
}
public static void Delete(int id)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
Delete(db, id);
}
}
public static void Delete(OrderSystemEntities db, int id)
{
db.UserAccounts_Delete(id);
}
public static UserAccount SelectById(int id)
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
return SelectById(db, id);
}
}
public static UserAccount SelectById(OrderSystemEntities db, int id)
{
return db.UserAccounts_SelectById(id).ElementAtOrDefault(0);
}
public static List<UserAccount> SelectAll()
{
using (OrderSystemEntities db = new OrderSystemEntities())
{
return SelectAll(db);
}
}
public static List<UserAccount> SelectAll(OrderSystemEntities db)
{
return db.UserAccounts_SelectAll().ToList();
}
}
}
在BLL中,我创建了一个类“UserAccountEO.cs”,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using OrderSystemDAL;
namespace OrderSystemBLL
{
public class UserAccountEO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime InsertDate { get; set; }
public DateTime UpdateDate { get; set; }
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
public bool Save(ref ArrayList validationErrors)
{
ValidateSave(ref validationErrors);
if (validationErrors.Count == 0)
{
if (Id == 0)
{
Id = UserAccountsData.Insert(FirstName, LastName, DateTime.Now);
}
else
{
UserAccountsData.Update(Id, FirstName, LastName, DateTime.Now);
}
return true;
}
else
{
return false;
}
}
private void ValidateSave(ref ArrayList validationErrors)
{
if (FirstName.Trim() == "")
{
validationErrors.Add("The First Name is required.");
}
if (LastName.Trim() == "")
{
validationErrors.Add("The Last Name is required.");
}
}
public void Delete(ref ArrayList validationErrors)
{
ValidateDelete(ref validationErrors);
if (validationErrors.Count == 0)
{
UserAccountsData.Delete(Id);
}
}
private void ValidateDelete(ref ArrayList validationErrors)
{
//Check for referential integrity.
}
public bool Select(int id)
{
UserAccount userAccount = UserAccountsData.SelectById(id);
if (userAccount != null)
{
MapData(userAccount);
return true;
}
else
{
return false;
}
}
internal void MapData(UserAccount userAccount)
{
Id = userAccount.Id;
FirstName = userAccount.FristName;
LastName = userAccount.LastName;
InsertDate = userAccount.AuditFields_InsertDate;
UpdateDate = userAccount.AuditFields_UpdateDate;
}
public static List<UserAccountEO> SelectAll()
{
List<UserAccountEO> userAccounts = new List<UserAccountEO>();
List<UserAccount> userAccountDTOs = UserAccountsData.SelectAll();
foreach (UserAccount userAccountDTO in userAccountDTOs)
{
UserAccountEO userAccountEO = new UserAccountEO();
userAccountEO.MapData(userAccountDTO);
userAccounts.Add(userAccountEO);
}
return userAccounts;
}
}
}
在PL中,我创建了一个网页,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrderSystemBLL;
using System.Collections;
namespace OrderSystemUI
{
public partial class Users : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadUserDropDownList();
}
}
private void LoadUserDropDownList()
{
ddlUsers.DataSource = UserAccountEO.SelectAll();
ddlUsers.DataTextField = "FullName";
ddlUsers.DataValueField = "Id";
ddlUsers.DataBind();
}
}
}
以上方式是使用EF4在n层架构中实现DTO模式的正确方法???
感谢您的帮助 感谢。
答案 0 :(得分:2)
Daniel Simmons发布的网上有一篇很棒的帖子,展示了如何实现N层应用
使用自我实体和DTO实体框架,我认为这会对你有所帮助
http://msdn.microsoft.com/en-us/magazine/3fc94394-227a-4781-a4aa-6ca95ff0107a,并查看
此帖http://msdn.microsoft.com/en-us/magazine/acc3017a-9ec8-4c0e-81e3-db9099e7e4e4
和http://msdn.microsoft.com/en-us/magazine/acc3017a-9ec8-4c0e-81e3-db9099e7e4e4
祝你好运!答案 1 :(得分:1)
DTO应该只有属性,而不是像UserAccountEO这样的方法。您应该将DTO代码与将实体映射到DTO的逻辑分开。
其他一切似乎都是正确的。
BTW:EntitiesToDTOs是一个从您的Entity Framework EDMX文件生成DTO的工具,它可以帮助您节省大量的开发时间和精力。