当前位置: 移动技术网 > IT编程>开发语言>c# > Reface.NPI 方法名称解析规则详解

Reface.NPI 方法名称解析规则详解

2020年04月10日  | 移动技术网IT编程  | 我要评论
在上次的文章中简单介绍了 [Reface.NPI] 中的功能。 本期,将对这方法名称解析规则进行详细的解释和说明, 以便开发者可以完整的使用 [Reface.NPI] 中的各种功能。 基本规则 方法名称以 Insert , Delete , Select , Update 开头 方法名以数个单词构成 ...

在上次的文章中简单介绍了 reface.npi 中的功能。
本期,将对这方法名称解析规则进行详细的解释和说明,
以便开发者可以完整的使用 reface.npi 中的各种功能。


基本规则

  • 方法名称以 insert , delete , select , update 开头
  • 方法名以数个单词构成,关键字、字段、操作符、参数名都是一个单词
  • 每个单词以大写开头,其余全小写
    • username 会被认作两个单词
    • username 会被认作一个单词
  • 使用入参填充 sql参数 时,不计大小写,你可以用 id 填充 @id 的值

1 insert 规则

仅使用 insert 将使用实体中的所有字段的值写入。

void insert(entity entity);

开发者也可以通过 without 关键字排除一些字段的写入,特别是那些依赖数据库本身的字段。

下面的例子不会对 idcreatetime 字段写入

bool insertwithoutidcreatetime(user user);

2 delete 规则

delete 方法名包含一个 条件规则

条件规则 允许开发者定义一个 where 子语。

2.1 使用 by 声明一个 条件规则

要在方法名中声明一个条件,必须以 by 作为开始。

下面的方法,将会生成 where id = @id , 并以入参的 id 作为 @id 的值。

void deletebyid(int id);

2.2 使用 andor 合并多个条件

你也可以使用 andor 合并多个条件

下面两个方法将会生成 where id = @id and/or name = @name 并以入参的 idname 分别作用 @id@name 的值。

bool deletebyidandname(int id, string name)

bool deletebyidorname(int id, string name)

注意

  • 因为没有想到一个好的方法可以对条件进行分组,所以目前不可以声明条件组,形如 where ( id = @id and name = @name ) or ( state = @state and loginname = @loginname )

如果您有好的想法,您也可以告诉我,共同完善 library 。非常期待您的分享,感谢!

2.3 更多的操作符

很明显,我们不可能总是用 = 作为条件的判断操作,
我们还有 大于,小于,like等等。

你可以在字段名后面加上操作符来实现此功能。

下面的例子会生成 where name like @name 的条件。

int deletebynamelike(string name)

目前系统中支持的操作符有

sql method
= is , equal , equals
> greaterthan , gt
>= greaterthanandequals , gteq
< lessthan , lt
<= lessthanandequals , lteq
like like , likes
in in

2.4 自定义参数名

在上面的例子中,参数名直接与字段名相同。

我们也可以在操作符后加上参数名来改名这个默认的参数名。

下面的例子会生成 where password like @badpassworda or password like @badpasswordb

int deletebypasswordlikebadpasswordaorpasswordlikebadpasswordb(string badpassworda, string badpasswordb);

3 update 规则

update 规则由两个部分组成

  • 条件规则 ( 与 delete 规则相同)
  • set 规则

set 规则

3.1 指定 set 的字段

update 关键字后接的部分,一直到 by 之前,都是 set 的字段。

下面的例子会生成 set password = @password where id = @id

void updatepasswordbyid(int id, string password);

3.2 多个 set

你可以用 and 连接多个 set

下面的例子会生成 set password = @password , changingtime = @changingtime where id = @id

bool updatepasswordandchangingtimebyid(int id, string password, datetime changingtime);

3.3 自定义 set 参数

条件规则 一样,生成的语句中,会默认使用字段名作为参数名。

你也可以在字段后添加 equals 再加上 参数名称 来自定义参数名。

下面的例子会生成 set count = @newcount where id = @id and count = @oldcount

int updatecountequalsnewcountbyidandcountisoldcount(int id, int oldcount, int newcount);

3.4 不指定 set 子句

当没有 set 子句的时候,
会以排除了 by 子句的条件后的所有字段作为 set 子句。

下面的例子会生成 update [user] set name = ?, password = ? where id = ?

// user : id, name, password
int updatebyid(int id, user user);

若表中还有一些字段在 update 时即不是条件,也不打算更新。可以使用 without 关键字指定。
下面的例子中,user 包含四个属性

  • id
  • name
  • password
  • createtime
// 下面的语句不会对 createtime 进行更新
void updatewithoutcreatetimebyid(int id, user user);

4 select 规则

select规则 包含以下三个规则

  • 条件规则 ( 与 deleteupdate 相同 )
  • 输出字段规则
  • 排序规则
  • 分页查询

4.1 输出字段

这个规则比较简单,
只要把字段列在 select 后即可,
多个字段可以用 and 连接。
输出字段是可选的,你可以跳过这个部分直接编写条件。

ilist<entity> selectidandnameandcreatetime();

4.2 条件规则

updatedelete 一样,使用 by 关键字开始条件子句

user selectbyid(int id);

4.3 排序规则

4.3.1 orderby 子句

排序规则是由关键字 orderby 开头的 ( 除了 o 都是 小写 )。

下面的例子会生成 order by id asc / desc

ilist<entity> selectorderbyid();
ilist<eneity> selectorderbyiddesc();

4.3.2 多个排序

多个排序不需要使用 and 连接,直接拼接即可。

ilist<user> selectorderbyusernamecreatetime();

4.3.3 分页查询

select 方法前加上 paging 就可以使用分页查询功能。

注意

  • 使用分页查询时,必须提供类型为 paging 的参数。
ilist<order> pagingselectbycreatetimegt(datetime createtime);

使用基于 reface.appstarter 开发的 reface.appstarter.npi 你可以不避手动创建代理类,
只需要通过构造函数注入那些实现了 npi 的接口,就可以直接对数据库进行增删改查的操作了。

在后面的文章中,会介绍 reface.appstarter.npi 的功能和使用方法。


关注公众平台【清水潭】,可以查阅更多资料

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网