Attribute在.NET编程中的应用(三)
在编写多层应用程序的时候,你是否为每次要写大量类似的数据访问代码而感到枯燥无味?比如我们需要编写调用存储过程的代码,或者编写T_SQL代码,这些代码往往需要传递各种参数,有的参数个数比较多,一不小心还容易写错。有没有一种一劳永逸的方法?当然,你可以使用MS的Data Access Application Block,也可以使用自己编写的Block。这里向你提供一种另类方法,那就是使用Attribute。
下面的代码是一个调用AddCustomer存储过程的常规方法:
public int AddCustomer(SqlConnection connection, string customerName, string country, string province, string city, string address, string telephone) { SqlCommand command=new SqlCommand("AddCustomer", connection); command.CommandType=CommandType.StoredProcedure; command.Parameters.Add("@CustomerName",SqlDbType.NVarChar,50).Value=customerName; command.Parameters.Add("@country",SqlDbType.NVarChar,20).Value=country; command.Parameters.Add("@Province",SqlDbType.NVarChar,20).Value=province; command.Parameters.Add("@City",SqlDbType.NVarChar,20).Value=city; command.Parameters.Add("@Address",SqlDbType.NVarChar,60).Value=address; command.Parameters.Add("@Telephone",SqlDbType.NvarChar,16).Value=telephone; command.Parameters.Add("@CustomerId",SqlDbType.Int,4).Direction=ParameterDirection.Output; connection.Open(); command.ExecuteNonQuery(); connection.Close(); int custId=(int)command.Parameters["@CustomerId"].Value; return custId; }
上面的代码,创建一个Command实例,然后添加存储过程的参数,然后调用ExecuteMonQuery方法执行数据的插入操作,最后返回CustomerId。从代码可以看到参数的添加是一种重复单调的工作。如果一个项目有100多个甚至几百个存储过程,作为开发人员的你会不会要想办法偷懒?(反正我会的:-))。
下面开始我们的代码自动生成工程:
我们的目的是根据方法的参数以及方法的名称,自动生成一个Command对象实例,第一步我们要做的就是创建一个SqlParameterAttribute, 代码如下:
SqlCommandParameterAttribute.cs using System; using System.Data; using Debug=System.Diagnostics.Debug; namespace DataAccess { // SqlParemeterAttribute 施加到存储过程参数 [ AttributeUsage(AttributeTargets.Parameter) ] public class SqlParameterAttribute : Attribute { private string name; //参数名称 private bool paramTypeDefined; //是否参数的类型已经定义 private SqlDbType paramType; //参数类型 private int size; //参数尺寸大小 private byte precision; //参数精度 private byte scale; //参数范围 private bool directionDefined; //是否定义了参数方向 private ParameterDirection direction; //参数方向 public SqlParameterAttribute() { } public string Name { get { return name == null ? string.Empty : name; } set { _name = value; } } public int Size { get { return size; } set { size = value; } } public byte Precision { get { return precision; } set { precision = value; } } public byte Scale { get { return scale; } set { scale = value; } } public ParameterDirection Direction { get { Debug.Assert(directionDefined); return direction; } set { direction = value; directionDefined = true; } } public SqlDbType SqlDbType { get { Debug.Assert(paramTypeDefined); return paramType; } set { paramType = value; paramTypeDefined = true; } } public bool IsNameDefined { get { return name != null && name.Length != 0; } } public bool IsSizeDefined { get { return size != 0; } } public bool IsTypeDefined { get { return paramTypeDefined; } } public bool IsDirectionDefined { get { return directionDefined; } } public bool IsScaleDefined { get { return _scale != 0; } } public bool IsPrecisionDefined { get { return _precision != 0; } } ...以上定义了SqlParameterAttribute的字段和相应的属性,为了方便Attribute的使用,我们重载几个构造器,不同的重载构造器用于不用的参数:
... // 重载构造器,如果方法中对应于存储过程参数名称不同的话,我们用它来设置存储过程的名称 // 其他构造器的目的类似 public SqlParameterAttribute(string name) { Name=name; } public SqlParameterAttribute(int size) { Size=size; } public SqlParameterAttribute(SqlDbType paramType) { SqlDbType=paramType; } public SqlParameterAttribute(string name, SqlDbType paramType) { Name = name; SqlDbType = paramType; } public SqlParameterAttribute(SqlDbType paramType, int size) { SqlDbType = paramType; Size = size; } public SqlParameterAttribute(string name, int size) { Name = name; Size = size; } public SqlParameterAttribute(string name, SqlDbType paramType, int size) { Name = name; SqlDbType = paramType; Size = size; } } }
为了区分方法中不是存储过程参数的那些参数,比如SqlConnection,我们也需要定义一个非存储过程参数的Attribute:
//NonCommandParameterAttribute.cs using System; namespace DataAccess { [ AttributeUsage(AttributeTargets.Parameter) ] public sealed class NonCommandParameterAttribute : Attribute { } }
我们已经完成了SQL的参数Attribute的定义,在创建Command对象生成器之前,让我们考虑这样的一个事实,那就是如果我们数据访问层调用的不是存储过程,也就是说Command的CommandType不是存储过程,而是带有参数的SQL语句,我们想让我们的方法一样可以适合这种情况,同样我们仍然可以使用Attribute,定义一个用于方法的Attribute来表明该方法中的生成的Command的CommandType是存储过程还是SQL文本,下面是新定义的Attribute的代码:
//SqlCommandMethodAttribute.cs using System; using System.Data; namespace Emisonline.DataAccess { [AttributeUsage(AttributeTargets.Method)] public sealed class SqlCommandMethodAttribute : Attribute { private string commandText; private CommandType commandType; public SqlCommandMethodAttribute( CommandType commandType, string commandText) { commandType=commandType; commandText=commandText; } public SqlCommandMethodAttribute(CommandType commandType) : this(commandType, null){} public string CommandText { get { return commandText==null ? string.Empty : commandText; } set { commandText=value; } } public CommandType CommandType { get { return commandType; } set { commandType=value; } } } }
我们的Attribute的定义工作已经全部完成,下一步就是要创建一个用来生成Command对象的类。(待续)
相关推荐
猜你还喜欢这些内容,不妨试试阅读一下评论与留言
以下内容均由网友提交发布,版权与真实性无法查证,请自行辨别。微信订阅号
扫码关注「任霏博客」微信订阅号- 你写得非常清晰明了,让我很容易理解你的观点。
- 感谢分享!拿走了~
- 您是说 UCClient 类接收来自Discuz的UCenter的消息吧,请求是来自 Discuz 的 UCenter吗?code 为 null 说明请求URL地址中没有 code 参数 (?code=xxx) ,确定是 UCenter 发起的请求吗?
- String code = request.getParameter("code"); code一直是null 这是为什么啊
- 你好,我想问一下如果是分析型的数据库要怎么制作docker镜像呢 是修改V008R003C002B0320版本号吗
- 可以的,我也正在开发分享的程序,可以邮件或群联系我都可以,关于页面里有联系方式:https://www.renfei.net/page/about 。
- 有破解软件的需要可以私下联系您吗?
- 您好,手机APP只是个客户端,用于数据呈现展示,数据均保存在服务器上,只留个APP没有任何用处,无能为力哦。
- 老哥 看你弄了这么多软件好厉害啊。 我有个软件 我买过几个小会员 没用几天 然后商家跑路了,软件服务器关闭了,连不上去 用不了。 你能做成一个打补丁版本可以本地用的么? 方便看下么?https://haodezhe.lanzouw.com/iD0f30h9joza 谢谢老哥!
- 您好,由于版权投诉和我国知识产权法的完善,我已经下架所有破解软件的下载链接了。
- 生花妙笔信手来 – 基于 Amazon SageMaker 使用 Grounded-SAM 加速电商广告素材生成 [1]
- github.renfei.net 不再完整代理 Github 页面改为代理指定文件
- 优雅的源代码管理(三):本地优雅的使用 Git Rebase 变基
- 优雅的源代码管理(二):Git 的工作原理
- 优雅的源代码管理(一):版本控制系统 VCS(Version Control System)与软件配置管理 SCM(Software Configuration Management)
- ChatGPT 开发商 OpenAI 买下极品域名 AI.com
- 火爆的 AI 人工智能 ChatGPT 国内注册教程、使用方式和收费标准
- 解决 SpringCloud 中 bootstrap.yml 不识别 @activatedProperties@ 参数
- Cron表达式书写教程搞定Linux、Spring、Quartz的定时任务
- 阿里云香港可用区C发生史诗级故障