2009-08-10 121 views
5

不知道我是否擁有正確的術語,但我對如何設置我的3層系統有點困惑。數據訪問層和業務對象

可以說我有我的數據庫中的用戶表。

在我的DAL中,我有一個UserDB類,它調用存儲過程到DB中插入,更新,刪除。 我也有UserDetails類用於UserDB返回並傳入對象。

所以現在我不知道如何在我的業務邏輯層中使用它。我是否需要另一個用戶的BLL對象類?如果是這樣,這不是多餘的? 或者我只是在我的BLL中使用UserDetails類?

回答

3

查找一個名爲「域驅動設計」的概念 - 使用所謂的存儲庫模式(如您的UserDB類)作爲數據庫的適配器以及工廠的最大用處。您的業​​務對象或域對象將業務邏輯合併到自身中,並可處理與其他業務對象的交互。

您使用什麼技術?像ActiveRecord這樣的東西可能會幫助你很多。

2

您通常會在BLL中執行業務規則。例如,您可能允許定期的呼叫中心員工提供新服務的10%折扣,但允許經理提供20%的折扣。你會在你的BLL是去像一個業務規則:

// Pseodocode 
double Discount 
{ 
    set 
    { 
     if (value > 10% AND Employee Is Not Manager) then throw Exception 
     if (value > 20%) then throw Exception 
     discount = value; 
    } 
} 
0

您可以使用下面的設計:

DAL: 

namespace DAL.Repository 
{ 
    public class UsersRepository 
    { 
     public static IList GetUser(string UserId) 
     { 
      using(MyDBEntities context=new MyDBEntities()) 
      { 
       // it calls SP in DB thru EF to fetch data 
       //here you can also context.user to fetch data instead of SP 
       return context.GetUser(UserId).ToList(); 

      } 
     } 
    } 
} 
BLL 

namespace BLL 
{ 
    public class User 
    { 
     public static IList GetUser(string UserId) 
     { 
      return DAL.Repository.UserRepository.GetUser(UserId); 
     } 
    } 
} 
PL 

    ddlUser.DataTextField = "UserName"; 
    ddlUser.DataValueField = "UserId"; 
    ddlUser.DataSource= BLL.User.GetUser(string.Empty); 
    ddlUser.DataBind() 

注意:當從BL將數據發送到PL轉換DB實體向業務實體如果你想循環使用PL中的數據,那麼這是必需的。

相關問題