2010-06-10 97 views
0

我目前正在構建基於Web的系統並嘗試在SOA架構中實現具有DTO的N層實體框架4.0。我在理解如何實現數據訪問層(DAL),業務邏輯層(BLL)和表示層時遇到問題。實現具有DTO的N層實體框架4.0

讓我們假設我有一個「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模式?

我將不勝感激您的幫助 謝謝。

回答

1

DTO應該只有屬性,而不是像UserAccountEO這樣的方法。您應該將DTO代碼與將實體映射到DTO的邏輯分離。

其他一切似乎都是正確的。

作者:EntitiesToDTOs是一個從您的Entity Framework EDMX文件生成DTO的工具,它可以幫助您大量節省開發時間和精力。