目前,我的第一个 ASP.Net MVC 应用程序大约有两个半星期的时间,到目前为止,我很喜欢它。
这个当前项目是 ASP.Net WebForms 项目的一个端口,我正在努力维护功能。一切顺利。
然而,我发现自己在重复……我自己。
例如,在我的 BaseController 类、BaseViewPage、BaseViewUserControl 和 BaseViewMasterPage 中,我有以下代码:
protected LanLordzApplicationManager AppManager
{
get
{
if(Session["Application"] == null)
{
Session["Application"] = new LanLordzApplicationManager(Server.MapPath("~/"));
}
return (LanLordzApplicationManager)Session["Application"];
}
}
protected User CurrentUser
{
get
{
if (Session["UserID"] == null && this.Request.Cookies["AutoLogOnKey"] != null && !string.IsNullOrEmpty(this.Request.Cookies["AutoLogOnKey"].Value))
{
this.CurrentUser = this.AppManager.RecallUser(this.Request.Cookies["AutoLogOnKey"].Value, Request.UserHostAddress);
}
if (Session["UserID"] == null)
{
return null;
}
else
{
return this.AppManager.GetUserByUserID((long)Session["UserID"]);
}
}
set
{
Session["UserID"] = value.UserID;
}
}
现在,这不是漂亮的代码。我想稍微修一下,但截至目前,我在四个地方修了它。事实上,它是几个错误的来源,这意味着要再次在所有四个地方修复它。
你会如何建议我保持这个系统干燥? 请注意,这两个对象都必须保留在会话中,原因不止两个。