仓酷云

标题: 来一篇关于NET的.net中Attribuge的具体使用(四) [打印本页]

作者: 莫相离    时间: 2015-1-16 14:20
标题: 来一篇关于NET的.net中Attribuge的具体使用(四)
数据挖掘有点高深的,主要估计就是使用一些算法提取一些实用的数据。学好数据挖掘的话可以应聘baidu或者google,但是一般人家对算法的要求听高的。你最好还是学点应用型的吧。这种主要是研究型的。SqlCommandGenerator类的计划

SqlCommandGEnerator类的计划思绪就是经由过程反射失掉办法的参数,利用被SqlCommandParameterAttribute标志的参数来拆卸一个Command实例。

援用的定名空间:
//SqlCommandGenerator.cs

usingSystem;
usingSystem.Reflection;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingDebug=System.Diagnostics.Debug;
usingStackTrace=System.Diagnostics.StackTrace;

类代码:
namespaceDataAccess
{
publicsealedclassSqlCommandGenerator
{
//公有机关器,不同意利用无参数的机关器机关一个实例
privateSqlCommandGenerator()
{
thrownewNotSupportedException();
}

//静态只读字段,界说用于前往值的参数称号
publicstaticreadonlystringReturnValueParameterName="RETURN_VALUE";
//静态只读字段,用于不带参数的存储历程
publicstaticreadonlyobject[]NoValues=newobject[]{};


publicstaticSqlCommandGenerateCommand(SqlConnectionconnection,
MethodInfomethod,object[]values)
{
//假如没有指定办法称号,从仓库帧失掉办法称号
if(method==null)
method=(MethodInfo)(newStackTrace().GetFrame(1).GetMethod());

//猎取办法传出去的SqlCommandMethodAttribute
//为了利用该办法来天生一个Command工具,请求有这个Attribute。
SqlCommandMethodAttributecommandAttribute=
(SqlCommandMethodAttribute)Attribute.GetCustomAttribute(method,typeof(SqlCommandMethodAttribute));

Debug.Assert(commandAttribute!=null);
Debug.Assert(commandAttribute.CommandType==CommandType.StoredProcedure||
commandAttribute.CommandType==CommandType.Text);

//创立一个SqlCommand工具,同时经由过程指定的attribute对它举行设置。
SqlCommandcommand=newSqlCommand();
command.Connection=connection;
command.CommandType=commandAttribute.CommandType;

//猎取command的文本,假如没有指定,那末利用办法的称号作为存储历程称号
if(commandAttribute.CommandText.Length==0)
{
Debug.Assert(commandAttribute.CommandType==CommandType.StoredProcedure);
command.CommandText=method.Name;
}
else
{
command.CommandText=commandAttribute.CommandText;
}

//挪用GeneratorCommandParameters办法,天生command参数,同时增加一个前往值参数
GenerateCommandParameters(command,method,values);
command.Parameters.Add(ReturnValueParameterName,SqlDbType.Int).Direction
=ParameterDirection.ReturnValue;

returncommand;
}

privatestaticvoidGenerateCommandParameters(
SqlCommandcommand,MethodInfomethod,object[]values)
{

//失掉一切的参数,经由过程轮回逐一举行处置。

ParameterInfo[]methodParameters=method.GetParameters();
intparamIndex=0;

foreach(ParameterInfoparamInfoinmethodParameters)
{
//疏忽失落参数被标志为[NonCommandParameter]的参数

if(Attribute.IsDefined(paramInfo,typeof(NonCommandParameterAttribute)))
continue;

//猎取参数的SqlParameterattribute,假如没有指定,那末就创立一个并利用它的缺省设置。
SqlParameterAttributeparamAttribute=(SqlParameterAttribute)Attribute.GetCustomAttribute(
paramInfo,typeof(SqlParameterAttribute));

if(paramAttribute==null)
paramAttribute=newSqlParameterAttribute();

//利用attribute的设置来设置一个参数工具。利用那些已界说的参数值。假如没有界说,那末就从办法
//的参数来揣度它的参数值。
SqlParametersqlParameter=newSqlParameter();
if(paramAttribute.IsNameDefined)
sqlParameter.ParameterName=paramAttribute.Name;
else
sqlParameter.ParameterName=paramInfo.Name;

if(!sqlParameter.ParameterName.StartsWith("@"))
sqlParameter.ParameterName="@"+sqlParameter.ParameterName;

if(paramAttribute.IsTypeDefined)
sqlParameter.SqlDbType=paramAttribute.SqlDbType;

if(paramAttribute.IsSizeDefined)
sqlParameter.Size=paramAttribute.Size;

if(paramAttribute.IsScaleDefined)
sqlParameter.Scale=paramAttribute.Scale;

if(paramAttribute.IsPrecisionDefined)
sqlParameter.Precision=paramAttribute.Precision;

if(paramAttribute.IsDirectionDefined)
{
sqlParameter.Direction=paramAttribute.Direction;
}
else
{
if(paramInfo.ParameterType.IsByRef)
{
sqlParameter.Direction=paramInfo.IsOut?
ParameterDirection.Output:
ParameterDirection.InputOutput;
}
else
{
sqlParameter.Direction=ParameterDirection.Input;
}
}

//检测是不是供应的充足的参数工具值
Debug.Assert(paramIndex<values.Length);

//把响应的工具值赋于参数。
sqlParameter.Value=values[paramIndex];
command.Parameters.Add(sqlParameter);


paramIndex++;
}

//检测是不是有过剩的参数工具值
Debug.Assert(paramIndex==values.Length);
}
}
}

需要的事情终究完成了。SqlCommandGenerator中的代码都加上了正文,以是其实不难读懂。上面我们进进最初的一步,那就是利用新的办法来完成上一节我们一入手下手显现个谁人AddCustomer的办法。

重构新的AddCustomer代码:
[SqlCommandMethod(CommandType.StoredProcedure)]
publicvoidAddCustomer([NonCommandParameter]SqlConnectionconnection,
[SqlParameter(50)]stringcustomerName,
[SqlParameter(20)]stringcountry,
[SqlParameter(20)]stringprovince,
[SqlParameter(20)]stringcity,
[SqlParameter(60)]stringaddress,
[SqlParameter(16)]stringtelephone,
outintcustomerId)
{
customerId=0;//必要初始化输入参数
//挪用Command天生器天生SqlCommand实例
SqlCommandcommand=SqlCommandGenerator.GenerateCommand(connection,null,newobject[]
{customerName,country,province,city,address,telephone,customerId});

connection.Open();
command.ExecuteNonQuery();
connection.Close();

//必需明白前往输入参数的值
customerId=(int)command.Parameters["@CustomerId"].Value;
}

代码中必需注重的就是out参数,必要事前举行初始化,并在Command实行操纵今后,把参数值传回给它。受害于Attribute,使我们挣脱了那种编写大批单调代码编程生活。我们乃至还可使用Sql存储历程来编写天生全部办法的代码,假如那样做的话,可就年夜小节省了你的工夫了,上一节和这一节中所示的代码,你能够把它们独自编译成一个组件,如许就能够在你的项目中不休的重用它们了。捆绑编译器。用户不需要受制于厂家,自己就能将程序在新平台上编译运行。除了牛B轰轰的linux,估计也没有系统捆绑c/c++的编译器,而且许多新平台都无法支持复杂的c/c++编译器在上面直接运行。
作者: 简单生活    时间: 2015-1-18 13:14
ASP.net的速度是ASP不能比拟的。ASP.net是编译语言,所以,当第一次加载的时候,它会把所有的程序进行编译(其中包括worker进程,还有对语法进行编译,形成一个程序集),当程序编译后,执行速度几乎为0。
作者: 精灵巫婆    时间: 2015-1-18 13:14
Servlet却在响应第一个请求的时候被载入,一旦Servlet被载入,便处于已执行状态。对于以后其他用户的请求,它并不打开进程,而是打开一个线程(Thread),将结果发送给客户。由于线程与线程之间可以通过生成自己的父线程(ParentThread)来实现资源共享,这样就减轻了服务器的负担,所以,JavaServlet可以用来做大规模的应用服务。
作者: 仓酷云    时间: 2015-1-24 18:59
Servlet的形式和前面讲的CGI差不多,它是HTML代码和后台程序分开的。它们的启动原理也差不多,都是服务器接到客户端的请求后,进行应答。不同的是,CGI对每个客户请求都打开一个进程(Process)。
作者: 飘灵儿    时间: 2015-2-2 12:41
JSP/Servlet虽然在国内目前的应用并不广泛,但是其前途不可限量。
作者: 因胸联盟    时间: 2015-2-23 10:16
代码的可重用性差:由于是面向结构的编程方式,并且混合html,所以可能页面原型修改一点,整个程序都需要修改,更别提代码重用了。
作者: 分手快乐    时间: 2015-3-7 08:24
ASP(ActiveServerPages)是Microsfot公司1996年11月推出的WEB应用程序开发技术,它既不是一种程序语言,也不是一种开发工具,而是一种技术框架,不须使用微软的产品就能编写它的代码。
作者: 莫相离    时间: 2015-3-14 18:42
ASP在执行的时候,是由IIS调用程序引擎,解释执行嵌在HTML中的ASP代码,最终将结果和原来的HTML一同送往客户端。
作者: 不帅    时间: 2015-3-21 14:27
ASP.Net和ASP的最大区别在于编程思维的转换,而不仅仅在于功能的增强。ASP使用VBS/JS这样的脚本语言混合html来编程,而那些脚本语言属于弱类型、面向结构的编程语言,而非面向对象。




欢迎光临 仓酷云 (http://ckuyun.com/) Powered by Discuz! X3.2