• 项目案例【Net Core】如何注入多个服务实现类


    需求

    库表保存时,需要校验逻辑.

    提交时有更深层次校验.

    **状态,还有特殊校验

    接口

    写一个通用的校验接口,这里定义了校验时间.每个阶段校验可能需要考虑顺序,增加一个顺序字段.

    public interface IOrcderCheck
        {
            ECheckTime CheckTime { get; }
            bool Check(OrderModel orderModel, List<string> warnMsg);
    
             /// <summary>
            /// 校验排序,默认制定为0即可
            /// </summary>
            int SortNum { get; }
        }

    实现类2个

    public class CheckAge : IOrcderCheck
        {
            public ECheckTime CheckTime => ECheckTime.BeforeSign;
            /// <summary>
            /// 校验排序,默认制定为0即可
            /// </summary>
            public int SortNum { get { return 0; } }
            public bool Check(OrderModel orderModel, List<string> warnMsg)
            {
                //内部逻辑 如:年龄小于18岁,校验失败
    return true; } }
     public class CheckCaStatus : IOrcderCheck
        { 
            /// <summary>
            /// 校验排序,默认制定为0即可
            /// </summary>
            public int SortNum { get { return 0; } }
            public ECheckTime CheckTime => ECheckTime.BeforeSubmit;
            DataAccess _da;
            IDBContext _db;
            CfcaManager _cfcaManager;
            IUserInfoRepository _userInfoRepository;
            public CheckCaStatus(IIndex<string, DataAccess> daArr, IIndex<string, IDBContext> dbArr,CfcaManager cfcaManager,IUserInfoRepository userInfoRepository)
            {
                this._da = daArr["acs"];
                this._db = dbArr["acs"];
                this._cfcaManager = cfcaManager;
                this._userInfoRepository = userInfoRepository;
            }public bool Check(OrderModel orderModel, List<string> warnMsg)
            {
                if (CheckDelStatus(orderModel))
                {
                    warnMsg.Add("此申请已被删除,无法提交");
                    return false;
                }
                //判断是否电子签成功
                var orderId = orderModel.OrderId;
                var res= this._cfcaManager.CheckCaForSubmit(orderId, ECaCategory.Camain);
                if(res.Value==1)
                {
                    return true;//电子签成功
                }
                if(res.WarnResult!=null)
                {
                    warnMsg.Add(res.WarnResult.Message); //不完全电子签的,提示完成电子签
                }
                else
                {
                    warnMsg.Add("请完成协议签署后再提交"); 
                }
                return warnMsg.Count == 0;
            }
            
    
        }

    注册封装

    扩展IServiceCollection 接口方法 AddCheckRules,注册校验类

    校验入口
    ValidateOrderData
    传入校验实体类和校验时间
    orderModel, eCheckTime
     
      public static class OrderCheckServiceCollectionExtensions
        {
    
            public static IServiceCollection AddCheckRules(this IServiceCollection services)
            {
                if (services == null)
                {
                    throw new ArgumentNullException(nameof(services));
                }
    
                services.AddSingleton<IOrcderCheck, CheckAge>();//计算年龄+贷款 > 65
                services.AddSingleton<IOrcderCheck, CheckCaStatus>();//电子签名状态校验 判断删除状态
    
                return services;
            }
            /// <summary>
            /// 通用订单提交校验入口.
            /// </summary>
            /// <param name="rules"></param>
            /// <param name="orderModel"></param>
            /// <param name="eCheckTime"></param>
            /// <returns></returns>
            public static List<string> ValidateOrderData(this IEnumerable<IOrcderCheck> rules, OrderModel orderModel, ECheckTime eCheckTime)
            {
                List<string> warnMsg = new List<string>();
    
                var newRules = rules.Where(r => r.CheckTime <= eCheckTime).OrderBy(o => o.SortNum);
                foreach (var rule in newRules)
                {          if (!rule.Check(orderModel, warnMsg))
                            break;
                }
                return warnMsg;
            }
    
           
    
        }

    调用

    注册

     services.AddCheckRules();

    构造注入

    IEnumerable<IOrcderCheck> _orderChecks;
    public OrderCRepository(
    IEnumerable<IOrcderCheck> orderChecks
    )
    {
                this._orderChecks = orderChecks;
    }

    函数调用

    这里直接使用函数,判断校验类错误信息.

    var msg = _orderChecks.ValidateOrderData(orderModel, ECheckTime.BeforeSubmit);
    if (msg.Count > 0)
    {
    return new WarnResult(string.Join('|', msg));
    }
  • 相关阅读:
    数据库连接JOIN
    Java面试金典
    Collections.sort详解
    Java复合优先于继承
    js算术运算符与数据类型转换
    js数组类型
    js对象类型
    CSS-API(CSS编程接口),CSSOM(css对象模型)
    从零开始--单片机十字路口交通灯控制实验
    matlab用双重循环实现费诺编码
  • 原文地址:https://www.cnblogs.com/blogs2014/p/10719109.html
Copyright © 2020-2023  润新知