当前位置: 移动技术网 > IT编程>开发语言>.net > ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用

ABP开发框架前后端开发系列---(5)Web API调用类在Winform项目中的使用

2019年06月14日  | 移动技术网IT编程  | 我要评论

九游战歌网,铸铁平台厂家,淘宝1212购物券

最近没有更新abp框架的相关文章,一直在研究和封装相关的接口,总算告一段落,开始继续整理下开发心得。上次我在随笔《abp开发框架前后端开发系列---(5)web api调用类在winform项目中的使用》中介绍了字典模块的管理,以及实现了常规的获取所有记录,获取条件查询记录,创建、更新、删除这些接口。本篇继续深入介绍abp框架在实际项目中使用的情况,本篇随笔整理对abp基础接口,以及展示完成的省份城市行政区管理模块的内容。

1、abp常规处理接口

根据abp框架默认提供的一些接口,我们可以在服务端封装好相关的web api接口(由于动态api的便利,其实是完成applicationservice层即可),前面介绍了获取条件查询记录,创建、更新、删除这些接口的实现和处理,以及可以扩展自己的自定义业务接口,如下是字典模块的接口关系。

字典管理界面,列出字典类型,并对字典类型下的字典数据进行分页展示,分页展示利用分页控件展示。

新增或者编辑窗体界面如下

或者是批量的字典数据录入

这个精确或者模糊查询,则是在应用服务层里面定义规则的,在应用服务层接口类里面,重写createfilteredquery可以设置getall的查询规则,重写applysorting则可以指定列表的排序顺序。

 

2、abp常规查询接口的细化

在前面介绍了的内容汇总,基本上实现了常规数据的分页查询,我们可以看到,对于字典数据来说,分页查询条件是在dictdatapageddto里面定义,这个是我们定义的分页条件,如下代码所示。

    /// <summary>
    /// 用于根据条件分页查询
    /// </summary>
    public class dictdatapageddto : pagedresultrequestdto
    {
        /// <summary>
        /// 字典类型id
        /// </summary>
        public virtual string dicttype_id { get; set; }

        /// <summary>
        /// 类型名称
        /// </summary>
        public virtual string name { get; set; }

        /// <summary>
        /// 指定值
        /// </summary>
        public virtual string value { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public virtual string remark { get; set; }
    }

这个类文件,我们一般把这个业务模块相关的统一放在一个文件中,例如字典数据相关的dto放在一个dictdatadto文件里面,方便管理,如下所示。

上面是字典模块的一些基础介绍,实际上我们开发业务模块的时候,录入数据的时候,还需要一个判断的步骤,如不允许名称重复的情况。在创建新的记录和更新已有记录都需要进行必要的判断,保证数据的有效性和不重复性。

如对于省份管理界面来说,我们不能运行重复录入省份名称,那么就需要在录入数据或者更新数据的时候,进行必要的存在性判断。

那么上面的处理是如何实现的呢。

主要的界面实现代码如下所示。

if (string.isnullorempty(id))
{
    //判断存在条件
    var countdto = new provincepageddto() { provincename = this.txtprovince.text };
    bool isexist = await provinceapicaller.instance.count(countdto) > 0;
    if (isexist)
    {
        messagedxutil.showtips("省份名称已存在,请选择其他名称");
        this.txtprovince.focus();
        return;
    }
    else
    {
        //创建新记录
        tempinfo = await provinceapicaller.instance.create(tempinfo);
    }
}
else
{
    //判断存在条件,排除本记录同名情况
    var countdto = new provincepageddto() { provincename = this.txtprovince.text, excludeid = id.toint64() };
    bool isexist = await provinceapicaller.instance.count(countdto) > 0;
    if (isexist)
    {
        messagedxutil.showtips("省份名称已存在,请选择其他名称");
        this.txtprovince.focus();
        return;
    }
    else
    {
        //更新记录
        tempinfo = await provinceapicaller.instance.update(tempinfo);
    }
}

processdatasaved(this.btnok, new eventargs());
this.dialogresult = system.windows.forms.dialogresult.ok;

我们发现,这里增加了一个count的函数用来判断,传入的条件就是前面的分页请求条件。

bool isexist = await provinceapicaller.instance.count(countdto) > 0;

我们看看我们的应用服务层的接口实现如下所示。

        /// <summary>
        /// 获取指定条件的数量
        /// </summary>
        /// <param name="input">查找条件</param>
        /// <returns></returns>
        public async virtual task<int> count(tgetallinput input)
        {
            var query = createfilteredquery(input);
            return await task.fromresult(query.count());
        }

这里最终还是跳转到 createfilteredquery 函数里面实现判断逻辑了。

        /// <summary>
        /// 自定义条件处理
        /// </summary>
        /// <param name="input">查询条件dto</param>
        /// <returns></returns>
        protected override iqueryable<province> createfilteredquery(provincepageddto input)
        {
            return base.createfilteredquery(input)
                .whereif(input.excludeid.hasvalue, t=>t.id != input.excludeid) //不包含排除id
                .whereif(!input.provincename.isnullorwhitespace(), t => t.provincename.contains(input.provincename));             
        }

这里面包含了两个判断条件,一个是排除指定的id记录,一个是匹配省份名称。

因为我们在更新记录的时候,需要判断非本记录是否有重复的名称。

//判断存在条件,排除本记录同名情况
var countdto = new provincepageddto() { provincename = this.txtprovince.text, excludeid = id.toint64() };
bool isexist = await provinceapicaller.instance.count(countdto) > 0;

这个excludeid 我们在分页条件里面增加一个固定的属性即可。

以上的分页信息,包含了实体dto对象的一些属性,我们可以根据需要增加或者减少一部分属性。

另外我们定义的创建省份dto对象和获取到单个实体的dto对象,他们的定义和关系如下所示,方便我们在界面上进行操作。

    /// <summary>
    /// 创建全国省份表,dto对象
    /// </summary>
    public class createprovincedto : entitydto<long>
    { 
        /// <summary>
        /// 默认构造函数(需要初始化属性的在此处理)
        /// </summary>
        public createprovincedto()
        {
         }

        #region property members
        
        /// <summary>
        /// 省份名称
        /// </summary>
        [required]
        public virtual string provincename { get; set; }


        #endregion

    }

    /// <summary>
    /// 全国省份表,dto对象
    /// </summary>
    public class provincedto : createprovincedto
    {

    }

固定这些规则后,我们也可以用代码生成工具快速生成对应的dto文件了。

有了这些分页属性后,我们就可以在应用服务层里面定义自己的过滤规则了,如对于字典类型的应用服务层的筛选条件函数,如下所示。

        /// <summary>
        /// 自定义条件处理
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        protected override iqueryable<dicttype> createfilteredquery(dicttypepageddto input)
        {
            return base.createfilteredquery(input)
                .whereif(!string.isnullorempty(input.excludeid), t => t.id != input.excludeid) //不包含排除id
                .whereif(!string.isnullorempty(input.name), t => t.name.contains(input.name))
                .whereif(!string.isnullorempty(input.remark), t => t.remark.contains(input.remark))
                .whereif(!string.isnullorempty(input.code), t => t.code == input.code)
                .whereif(!string.isnullorempty(input.pid), t => t.pid == input.pid);
        }

上面是对于包含、相等或者不等于的三种情况的条件判断,如果我们还需要一个时间区间范围或者数值范围的判断,那么同样可以在这里进行管理规则,如下是针对产品应用服务层的过滤规则,如下代码所示。

        /// <summary>
        /// 自定义条件处理
        /// </summary>
        /// <param name="input">查询条件dto</param>
        /// <returns></returns>
        protected override iqueryable<product> createfilteredquery(productpageddto input)
        {
            return base.createfilteredquery(input)
                .whereif(!input.excludeid.isnullorwhitespace(), t => t.id != input.excludeid) //不包含排除id

                .whereif(!input.productno.isnullorwhitespace(), t => t.productno.contains(input.productno)) //如需要精确匹配则用equals
                .whereif(!input.barcode.isnullorwhitespace(), t => t.barcode.contains(input.barcode)) //如需要精确匹配则用equals
                .whereif(!input.materialcode.isnullorwhitespace(), t => t.materialcode.contains(input.materialcode)) //如需要精确匹配则用equals
                .whereif(!input.producttype.isnullorwhitespace(), t => t.producttype.contains(input.producttype)) //如需要精确匹配则用equals
                .whereif(!input.productname.isnullorwhitespace(), t => t.productname.contains(input.productname)) //如需要精确匹配则用equals
                .whereif(!input.unit.isnullorwhitespace(), t => t.unit.contains(input.unit)) //如需要精确匹配则用equals
                .whereif(!input.note.isnullorwhitespace(), t => t.note.contains(input.note)) //如需要精确匹配则用equals
                .whereif(!input.description.isnullorwhitespace(), t => t.description.contains(input.description)) //如需要精确匹配则用equals
                 
                 //状态
                .whereif(input.status.hasvalue, t => t.status==input.status)
                                                                                              
                 //成本价区间查询
                .whereif(input.pricestart.hasvalue, s => s.price >= input.pricestart.value)
                .whereif(input.priceend.hasvalue, s => s.price <= input.priceend.value)

                //销售价区间查询
                .whereif(input.salepricestart.hasvalue, s => s.saleprice >= input.salepricestart.value)
                .whereif(input.salepriceend.hasvalue, s => s.saleprice <= input.salepriceend.value)

                //特价区间查询
                .whereif(input.specialpricestart.hasvalue, s => s.specialprice >= input.specialpricestart.value)
                .whereif(input.specialpriceend.hasvalue, s => s.specialprice <= input.specialpriceend.value)
                .whereif(input.isusespecial.hasvalue, t => t.isusespecial == input.isusespecial) //如需要精确匹配则用equals
                                                                                                  
                //最低折扣区间查询
                .whereif(input.lowestdiscountstart.hasvalue, s => s.lowestdiscount >= input.lowestdiscountstart.value)
                .whereif(input.lowestdiscountend.hasvalue, s => s.lowestdiscount <= input.lowestdiscountend.value)

                //创建日期区间查询
                .whereif(input.creationtimestart.hasvalue, s => s.creationtime >= input.creationtimestart.value)
                .whereif(input.creationtimeend.hasvalue, s => s.creationtime <= input.creationtimeend.value);
        }

以上就是我们深入对分页查询和判断是否存在接口的细节处理,可以包含很多自定义的条件,如等于或不等于、包含或者不包含,区间查询(大于或者小于等)条件的处理。对于省份城市行政区管理模块的重复性判断,我们通过count函数来判断,同时在后台应用服务层对这些参数进行规则过滤即可。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网