您的位置:首页 > 运维架构 > 网站架构

根据业务自己设计的.NET工厂模式架构

2016-04-20 19:04 956 查看
最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构

项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有appid来区分)

因为每个平台的业务处理几乎不一样,而属于同一个平台的app客户端业务又几乎一样,但也有要特殊情况处理的。

直接代码展示:

首先是接口和抽象类

/// <summary>
/// 商品接口类
/// </summary>
public interface IProductBLL
{
string GetBLLName(int appid);

string GetAction();
}

/// <summary>
/// 抽象类
/// </summary>
public abstract class BaseProductBLL : IProductBLL
{

public static IProductBLL GetProductBll(int appid)
{
//定义appid属于哪个平台,这里可以用config配置或者枚举定义
int[] A_appid = new int[] {10001,10003,10005 };
int[] B_appid = new int[] { 10002, 10004, 10006 };
int[] C_appid = new int[] { 10007, 10008, 100009 };
IProductBLL bll = null;

if (A_appid.Contains(appid))//此appid属于A平台
{
bll = BaseProduct_A_BLL.GetProductBll(appid);
}
else if (B_appid.Contains(appid))//此appid属于B平台
{
bll = BaseProduct_B_BLL.GetProductBll(appid);
}
else if (C_appid.Contains(appid))//此appid属于C平台
{
bll = BaseProduct_C_BLL.GetProductBll(appid);
}

return bll;
}

public abstract string GetBLLName(int appid);
public abstract string GetAction();
}


接下来是各平台的通用类

/// <summary>
/// A平台通用类
/// </summary>
public class BaseProduct_A_BLL : BaseProductBLL
{
private static IProductBLL _instanceA = null;
public static IProductBLL InstanceA
{
get
{
if (_instanceA == null)
{

_instanceA = new BaseProduct_A_BLL();
}
return _instanceA;
}
}
public static IProductBLL GetProductBll(int appid)
{
IProductBLL bll = null;

switch (appid)
{
case 10003:
bll=Product_10003_BLL.Instance10003;
break;
default:
bll = BaseProduct_A_BLL.InstanceA;
break;
}

return bll;
}

public override string GetBLLName(int appid)
{
return "" + appid + "调用了BaseProduct_A_BLL.GetBLLName";
}

public override string GetAction()
{
return "调用了BaseProduct_A_BLL.GetAction";
}

}

/// <summary>
/// B平台通用类
/// </summary>
public class BaseProduct_B_BLL : BaseProductBLL
{
private static IProductBLL _instanceB = null;
public static IProductBLL InstanceB
{
get
{
if (_instanceB == null)
{

_instanceB = new BaseProduct_B_BLL();
}
return _instanceB;
}
}
public static IProductBLL GetProductBll(int appid)
{
IProductBLL bll = null;

switch (appid)
{
default:
bll = BaseProduct_B_BLL.InstanceB;
break;
}

return bll;
}

public override string GetBLLName(int appid)
{
return "" + appid + "调用了BaseProduct_B_BLL.GetBLLName";
}

public override string GetAction()
{
return "调用了BaseProduct_B_BLL.GetAction";
}

}

/// <summary>
/// C平台通用类
/// </summary>
public class BaseProduct_C_BLL : BaseProductBLL
{
private static IProductBLL _instanceC = null;
public static IProductBLL InstanceC
{
get
{
if (_instanceC == null)
{

_instanceC = new BaseProduct_C_BLL();
}
return _instanceC;
}
}
public static IProductBLL GetProductBll(int appid)
{
IProductBLL bll = null;

switch (appid)
{
default:
bll = BaseProduct_C_BLL.InstanceC;
break;
}

return bll;
}

public override string GetBLLName(int appid)
{
return "" + appid + "调用了BaseProduct_C_BLL.GetBLLName";
}

public override string GetAction()
{
return "调用了BaseProduct_C_BLL.GetAction";
}
}


假如appid为10003的需要拓展,需在A平台通用类中添加case 10003 的逻辑,并创建对应的拓展类Product_10003_BLL并继承A平台通用类BaseProduct_A_BLL

/// <summary>
/// appid为10003拓展类
/// </summary>
public class Product_10003_BLL : BaseProduct_A_BLL
{
private static IProductBLL _instance10003 = null;
public static IProductBLL Instance10003
{
get
{
if (_instance10003 == null)
{

_instance10003 = new Product_10003_BLL();
}
return _instance10003;
}
}

public Product_10003_BLL()
{
}

public override string GetBLLName(int appid)
{
return "" + appid + "调用了Product_10003_BLL.GetBLLName";
}
}


如上appid为10003的拓展类重写了GetBLLName方法。

接下来就是在Controlle层中调用

/// <summary>
/// 基类控制类
/// </summary>
public class BasesController : Controller
{
public BasesController()
{
}

protected int Appid
{
get { return Convert.ToInt32(HttpContext.Request["appid"]); }
}

public IProductBLL ProductBll
{
get
{
return BaseProductBLL.GetProductBll(Appid);
}
}
}

public class ProductController : BasesController
{
//
// GET: /Product/
public ActionResult Index()
{
string bllname = ProductBll.GetBLLName(Appid);
string actionName = ProductBll.GetAction();
ViewData["bllname"] = bllname;
ViewData["actionName"] = actionName;
return View();
}

}


以上参数appid是必传,以上代码没有对不传参数且参数值得正确性做判断的异常处理,这里主要是记录对于这个系统的交互设计,另外数据访问层DAL也没有写出来,可以按照上面的设计思路进行创建

下面是我简单测试打印出来的

ViewData["bllname"]
ViewData["actionName"] 的值

访问http://localhost:37842/product/index?appid=10005

也就是参数appid为10005 时打印出 :

10005调用了BaseProduct_A_BLL.GetBLLName

调用了BaseProduct_A_BLL.GetAction

当appid为10003时打印出:

10003调用了Product_10003_BLL.GetBLLName

调用了BaseProduct_A_BLL.GetAction

当appid为10002时打印出:

10002调用了BaseProduct_B_BLL.GetBLLName

调用了BaseProduct_B_BLL.GetAction

此文仅记录笔记,各位大神可以多多指教!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: