Asp.Net工厂模式--工厂类的获取
工厂模式是基本的设计模式之一,我们在开发XueBa UI的过程中,对BLL层进行了工厂化的包装
namespace XueBa.SqlServer { public class Factory { public DocumentManager DocumentManager { get; private set; } public QuestionManager QuestionManager { get; private set; } public TagManager TagManager { get; private set; } private static Factory instance = null; private static object loc = new object(); private Factory() { DocumentManager = new DocumentManager(); QuestionManager = new QuestionManager(); TagManager = new TagManager(); } public static Factory getInstance() { lock (loc) { if (instance == null) { instance = new Factory(); } } return instance; } } }
那么,该如何使得每个Asp.Net页面都能够获得Factory类呢?我采用了重载System.Web.UIControls.Page基类的方法
using System; using System.Collections.Generic; using System.Linq; using System.Web; using XueBa.SqlServer; /// <summary> /// 所有asp.net页面的基类 /// </summary> public class BasePage : System.Web.UI.Page { public Factory Factory { get { return Factory.getInstance(); } } }
这样,让每个页面继承BasePage类,就可以实现对工厂类的访问了