在N层体系结构中添加System.Web引用业务逻辑层
-
24-10-2019 - |
题
我正在使用tableprofileprovider在N层架构中使用ASP.NET配置文件系统。
UI层是一个Web应用程序,因此我必须公开ProfileCommon类才能使用配置文件。
这是我体系结构的简化模式:
ui: ASP.NET Web应用程序。
业务: 纯Poco类。持久性Igronace。
BLL: 业务逻辑层。
达尔: 数据访问层。
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;
我希望能够从我的业务逻辑层访问Comman Comman,因此我将ProfileCommon定义移至我的Businessentities库(必须添加对system.web insture in 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访问Comman Comman这样的个人资料:
var profileInfo = new BLL.ProfileInfo((ProfileCommon)this.Context.Profile);
txtFullName.text = profileInfo.GetFullName();
现在,引用System.web在业务层/业务库中库违反了N层建筑学科?如果是这样,您将为实现这一目标做什么?
解决方案
您可以通过实现界面来打破对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;
}
}
现在,您对您的业务逻辑中的web.dll没有任何依赖性,但仍然可以自由实施 IProfile
使用WebApplication中的接口使用 ProfileBase
其他提示
您不应从业务层访问系统。这使您与Web应用程序合作。如果您想在其他类型的应用程序中重复使用业务层怎么办?
您应该问自己要实现的目标。然后,将其抽象为供商务层访问的通用合理的东西。这假设业务层应该完全了解用户。