2011-01-25 67 views
1

我正在使用TableProfileProvider在n層體系結構中使用ASP.NET配置文件系統。
UI層是一個Web應用程序,所以我必須公開profilecommon類才能使用配置文件。
下面是我的架構簡化模式:
UI: ASP.NET Web應用程序。
商業實體:純POCO類。持久性Igronace。
BLL:業務邏輯層。
DAL:數據訪問層。在n層體系結構中添加對業務邏輯層的System.Web引用

的的ProfileCommon定義是:
的ProfileCommon strongleyTypedProfile =(的ProfileCommon)這樣的:

public class ProfileCommon : ProfileBase 
{ 
    public virtual ProfileCommon GetProfile(string username) 
    { 
     return (ProfileCommon)ProfileBase.Create(username); 
    } 

    public virtual string FirstName 
    { 
     get 
     { 
      return (string)base.GetPropertyValue("FirstName"); 
     } 
     set 
     { 
      base.SetPropertyValue("FirstName", value); 
     } 
    } 
} 

在一個簡單的設計架構,一切都在Web應用程序項目如下定義我訪問的ProfileCommon。 Context.Profile;

我希望能夠從我的業務邏輯層訪問Profile常見,所以我感動的ProfileCommon定義,我BusinessEntities庫(不得不添加引用System.Web程序集在BusinessEntities庫),並確定新的ProfileBLL類:

public class ProfileInfo 
{ 
    public ProfileInfo(ProfileCommon profile) 
    { 
     this.Profile = profile; 
    } 

    public ProfileCommon Profile { get; set; } 

    public string GetFullName() 
    { 
     return this.Profile.FirstName + " " + this.Profile.LastName; 
    } 
} 

現在我可以從UI訪問常用的個人資料如下:

var profileInfo = new BLL.ProfileInfo((ProfileCommon)this.Context.Profile); 
txtFullName.text = profileInfo.GetFullName(); 

現在,在業務層參考的System.Web/BusinessEntities庫違反了n層結構的學科?如果是這樣,你會建議如何實現這一目標?

+0

@skaffman來實現你的web應用的IProfile界面的自由:此不是一個n層相關的問題。這與n層有關。當您在硬件級別有不同的層時,n層意味着。 – Kamyar 2011-01-25 23:05:03

回答

1

您可以通過改爲實現接口來打破ProfileBase的依賴關係。比方說

public interface IProfile 
{ 
    string FirstName { get; set; } 
    string LastName { get; set; } 

    IProfile GetProfile(string username); 
} 

public class ProfileCommon : ProfileBase, IProfile 
{ 
    public virtual IProfile GetProfile(string username) 
    { 
     return (ProfileCommon)ProfileBase.Create(username); 
    } 

    public virtual string FirstName 
    { 
     get 
     { 
      return (string)base.GetPropertyValue("FirstName"); 
     } 
     set 
     { 
      base.SetPropertyValue("FirstName", value); 
     } 
    } 
} 

public class ProfileInfo 
{ 
    public ProfileInfo(IProfile profile) 
    { 
     this.Profile = profile; 
    } 

    public IProfile Profile { get; set; } 

    public string GetFullName() 
    { 
     return this.Profile.FirstName + " " + this.Profile.LastName; 
    } 
} 

現在你沒有對System.Web.dll中任何依賴於你的業務邏輯,但仍然有使用ProfileBase

+0

非常瞭解抽象和鬆散耦合。謝謝。 – Kamyar 2011-01-27 14:32:24

1

您不應該從業務層訪問System.Web。這將您與Web應用程序聯繫起來。如果您想在不同類型的應用程序中重用業務層,該怎麼辦?

你應該問問自己你試圖完成什麼。然後,將該需求抽象爲適合業務層訪問的通用合理內容。這假設業務層應該瞭解用戶。