ITPub博客

首页 > 数据库 > 数据库开发技术 > C#通过工厂模式,我把一大堆if干掉了

C#通过工厂模式,我把一大堆if干掉了

原创 数据库开发技术 作者:后端Q 时间:2021-03-27 00:00:00 0 删除 编辑

概述

之前做微信项目的时候,会涉及到一个回调,大家都知道回调事件是很多类型的,一般是根据不同的类型在进行不同的逻辑处理。

这样就会延伸出一个问题,就是入口处会有一大堆if判断。这样本身是没什么问题的,只是看起来比较别扭,那么怎么把if干掉了 。这时候工厂模式派上用场了。下面我们来看下具体如何实现。

代码实现

1、定义虚方法Notify

  #region 定义虚方法    public class Notify    {        public virtual Dingdong.Wallet.Common.Result NotifyHandle(NotifyResult Result, dynamic respData)        {            Dingdong.Wallet.Common.Result r = new Result();            return r;
} }
#endregion

2、定义Attribute属性,并且指定一个参数ServiceName

  [System.Serializable]    [System.AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = false)]    [System.Runtime.InteropServices.ComVisible(true)]    public class InterfaceAttribute : Attribute    {              private string _ServiceName;        
public InterfaceAttribute() { }
public string ServiceName { get { return _ServiceName; } set { _ServiceName = value; }
} }

3、实现一个工厂NotifyFactory,通过反射把包含Attribute的找出来

  #region Factory
public class NotifyFactory { public Notify CreateNotify(string _name) { Dictionary<string, string> dic = new Dictionary<string, string>(); string strNamespace = Assembly.GetExecutingAssembly().GetName().Name; // LogHelper.WriteInfo("命名空间:" + strNamespace); var classes = Assembly.Load(strNamespace).GetTypes(); foreach (var item in classes) { Object[] obs = item.GetCustomAttributes(typeof(InterfaceAttribute), false); foreach (InterfaceAttribute record in obs) { dic.Add(record.ServiceName, item.Name); // LogHelper.WriteInfo("接口名称:" + record.ServiceName + "-类名:" + item.Name); } } Assembly myAssembly = Assembly.GetExecutingAssembly(); Notify Notify = (Notify)myAssembly.CreateInstance(strNamespace + "." + dic[_name]); return Notify; } }
#endregion

4、前端通过传入ServiceName,实现不同的业务逻辑

      NotifyFactory Factory = new NotifyFactory();            Notify Fa = Factory.CreateNotify(Notifyresult.ServiceName);//PERSONAL_REGISTER_EXPAND            Dingdong.Wallet.Common.Result r= Fa.NotifyHandle(Notifyresult, Result);//new  NotifyResult() ,"respData"            LogHelper.WriteInfo("异步通知结束");            LogHelper.WriteInfo("异步通知结束r:"+ r.Serialize());

5、其中一个具体的方法重载NotifyHandle,并且定义   [Interface(ServiceName = "DIRECT_RECHARGE")]

  [Export]    /// <summary>    /// 代扣交易   InterfaceAttribute 接口名称  类名自定义 确保命名空间在FuYin.Wallet.Notify 下    /// </summary>    [Interface(ServiceName = "DIRECT_RECHARGE")]    public class DirectRecharge : Notify    {
public override Result NotifyHandle(NotifyResult r, dynamic respData) { // return base.NotifyHandle(Result, respData); //r.ServiceName 异步通知接口方法 // r.ResponseType //回调类型
LogHelper.WriteInfo("公共数据:" + r.Serialize()); DirectRechargeNotifyResult model = JsonConvert.DeserializeObject<DirectRechargeNotifyResult>(respData); LogHelper.WriteInfo("业务处理数据:" + model.Serialize()); using (var context = new MefContext()) { var svc = context.GetService<IDeTransactionService>(); return svc.DirectRechargeNotify(model); } } }

这样就可以把一大堆if去掉了,代码看起来也简洁多了。

原文作者:后端Q;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2023-01-22

  • 博文量
    1540
  • 访问量
    725868