在n层架构中添加System.Web对业务逻辑层的引用

时间:2011-01-25 22:28:22

标签: .net web-applications asp.net-profiles n-layer

我正在使用TableProfileProvider在n层架构中使用ASP.NET配置文件系统 UI层是一个Web应用程序,所以我必须公开profilecommon类才能使用配置文件 这是我的架构的简化架构:
UI: ASP.NET Web应用程序 BusinessEntities:纯POCO课程。持久性的Igronace BLL:业务逻辑层。
DAL:数据访问层。

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:
ProfileCommon strongleyTypedProfile =(ProfileCommon)this.Context.Profile;

我希望能够从我的业务逻辑层访问Profile Common,因此我将ProfileCommon定义移至我的BusinessEntities库(必须在BusinessEntities库中添加对System.Web程序集的引用)并定义新的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();

现在,在Business Layer / BusinessEntities库中引用System.Web违反了n层架构规则吗?如果是这样,为了达到这个目的,你会建议什么?

2 个答案:

答案 0 :(得分:1)

您不应该从业务层访问System.Web。这与您使用Web应用程序有关。如果您想在不同类型的应用程序中重用业务层,该怎么办?

你应该问问自己,你想要通过这个来完成什么。然后,将该需求抽象为通用合理的业务层访问。这假设业务层应该完全了解用户。

答案 1 :(得分: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没有任何依赖性,但仍可以使用IProfile

自由地在Web应用程序中实现ProfileBase接口
相关问题