.net 在n层体系结构中将System.Web引用添加到业务逻辑层

.net 在n层体系结构中将System.Web引用添加到业务逻辑层,.net,web-applications,asp.net-profiles,n-layer,.net,Web Applications,Asp.net Profiles,N Layer,我正在使用TableProfileProvider在n层体系结构中使用ASP.NET配置文件系统。 UI层是一个web应用程序,因此我必须公开profilecommon类才能使用配置文件。 以下是我的架构的简化模式: UI:ASP.NET Web应用程序。 业务实体:纯POCO类。持久性Igronace. BLL:业务逻辑层。 DAL:数据访问层 常见的定义是: public class ProfileCommon : ProfileBase { public virtual Pro

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

常见的定义是:

 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,因此我将Profile Common定义移动到了我的BusinessEntities库(必须在BusinessEntities库中添加对System.Web assembly的引用),并定义了新的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是否违反了n层体系结构规则?如果是这样,您有什么建议来实现这一点?

您不应该从业务层访问System.Web。这将您与使用web应用程序联系在一起。如果您想在不同类型的应用程序中重用业务层,该怎么办


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

您可以通过实现接口来打破对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

@skaffman在Web应用程序中自由实现
IProfile
接口:这不是一个与n层相关的问题。这与n层有关。n层意味着在硬件层中有单独的层。对抽象和松耦合有很好的理解。非常感谢。