何时使用静电 [英] when to use static

查看:87
本文介绍了何时使用静电的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,它提供了SQL数据访问的基础

函数,子类构建在此基础上,通过选择查询字符串创建对表的访问权限。


我创建了子类为static,因为它们包含的是(对于

示例)一个select查询字符串,以及一个返回结果的方法

SqlSelect查询。 (SqlSelect是我自己的类,只包含创建

的DataSet等)。


所以子类中的所有内容都是静态的。


命名空间DataAccess

{

公共类EmployeeTDG:DataAccess.TDG

{

private static string selectString =" SELECT [name] FROM [Employee] ORDER BY

[name] ASC" ;;

public static SqlSelect getEmployees(SqlConnection sc)

{

返回TDG.executeSelectQuery(EmployeeTDG.selectString,sc);

}

}

}


我的问题是,这可以吗?我担心的是,也许我已经忽略了

后来可能会在后面咬我的东西!


干杯

Rich 。

I have an abstract class that provides the basis for SQL data access
functions, with the subclasses building on this to create access to tables
via select query strings.

I have created the subclasses as static as all they contain are (for
example) a select query string, and a method to return the results of a
SqlSelect query. (SqlSelect is my own class that simply wraps the creation
of DataSet etc).

So everything in the subclass is static.

namespace DataAccess
{
public class EmployeeTDG : DataAccess.TDG
{
private static string selectString = "SELECT [name] FROM [Employee] ORDER BY
[name] ASC";
public static SqlSelect getEmployees(SqlConnection sc)
{
return TDG.executeSelectQuery(EmployeeTDG.selectString, sc);
}
}
}

My question is, is this ok? My concerns are that maybe I''ve overlooked
something that might bite me in the backside later!

Cheers
Rich.

推荐答案

Rich,


我的意见是没关系。但是:

使用字符串作为命令至少在

安全方面不是一个好主意。

我理解你应该尝试在查询中使用存储过程或参数

以避免SQL注入攻击等等。


我不是这方面的专家所以这只是一个想法但是你可能想看看




Picho

" RichGK" < SS **** @ rrr.rr>在留言中写道

新闻:Ba ******************* @ text.news.blueyonder.co .uk ...
Rich,

my opinion is it''s ok. but:
using a string as the command is not such a good idea at least in the
security aspect.
for my understanding you should try and use stored proceadures or parameters
in the query to avoid SQL injection attacks and so on.

im no expert at this so this is just a thought but you might want to look
into that.

Picho
"RichGK" <ss****@rrr.rr> wrote in message
news:Ba*******************@text.news.blueyonder.co .uk...
我有一个抽象类,它提供了SQL数据访问功能的基础,子类构建在此基础上,通过选择查询字符串创建对表的访问。
我已经将子类创建为静态,因为它们包含的是(例如)选择查询字符串,以及返回SqlSelect查询结果的方法。 (SqlSelect是我自己的类,它只包含DataSet的创建等)。

所以子类中的所有东西都是静态的。

命名空间DataAccess
{
公共类EmployeeTDG:DataAccess.TDG
私有静态字符串selectString =" SELECT [name] FROM [Employee] ORDER
BY
[name] ASC" ;;
公共静态SqlSelect getEmployees(SqlConnection sc)
返回TDG.executeSelectQuery(EmployeeTDG.selectString,sc);
}
}
}

我的问题是,这样可以吗?我担心的是,也许我忽略了一些可能会在后面咬我的东西!

干杯
Rich。
I have an abstract class that provides the basis for SQL data access
functions, with the subclasses building on this to create access to tables
via select query strings.

I have created the subclasses as static as all they contain are (for
example) a select query string, and a method to return the results of a
SqlSelect query. (SqlSelect is my own class that simply wraps the creation
of DataSet etc).

So everything in the subclass is static.

namespace DataAccess
{
public class EmployeeTDG : DataAccess.TDG
{
private static string selectString = "SELECT [name] FROM [Employee] ORDER
BY
[name] ASC";
public static SqlSelect getEmployees(SqlConnection sc)
{
return TDG.executeSelectQuery(EmployeeTDG.selectString, sc);
}
}
}

My question is, is this ok? My concerns are that maybe I''ve overlooked
something that might bite me in the backside later!

Cheers
Rich.


Hi Rich,
Hi Rich,
我有一个抽象类,它提供了SQL数据访问功能的基础,子类构建通过选择查询字符串来创建对表格的访问。

我已经创建了子类作为静态,因为它们包含的是(例如)选择查询字符串,以及返回SqlSelect查询结果的方法。 (SqlSelect是我自己的类,它简单地包装了DataSet的创建等)。
I have an abstract class that provides the basis for SQL data access
functions, with the subclasses building on this to create access to tables
via select query strings.

I have created the subclasses as static as all they contain are (for
example) a select query string, and a method to return the results of a
SqlSelect query. (SqlSelect is my own class that simply wraps the creation
of DataSet etc).




有一个抽象类然后创建后代似乎毫无意义/>
由静态项组成。你从界面上得不到多少

抽象类的继承能力。


你没有说_why_你不能忍受使用实际对象,而不是

静态类。对象的优点是你可以更好地控制类之间的耦合,因为对象必须是在使用它的类的上下文中创建的
或者传入,其中

为您提供了代码中依赖关系的线索,以及通过使用共同祖先类型来概括的
机会和

工厂方法。


换句话说,你的信息,以及我可以从中收集的设计的一瞥,表明即使你使用的是OOP语言,你也可能没有真正实现这一目标。我鼓励你

读这个:
http://blogs.msdn.com/nickmalik/arch...21/328727.aspx

-

--- Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:此论坛中表达的观点是我自己的,而不是

代表我的雇主。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-



seems pretty pointless to have an abstract class and then create descendents
that are composed of static items. You don''t get much from the interface
inheritance abilities of the abstract class.

You didn''t say _why_ you cannot stand to use actual objects, instead of
static classes. The advantage of objects is that you are much better able
to control the coupling between classes, since the objects have to be
created in the context of the class that uses it, or passed in, which
provides you with both clues to the dependencies in the code, and
opportunities to generalize through the use of common ancestor types and
factory methods.

In other words, your message, and what small glimpse of your design that I
can glean from it, indicates that you may not have really made the leap to
OOD, even though you are using an OOP language. I would encourage you to
read this:
http://blogs.msdn.com/nickmalik/arch...21/328727.aspx
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--




Nick Malik [微软] < NI ******* @ hotmail.nospam.com>写在消息中

新闻:3IidnZ7LXvLkiY7fRVn-

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:3IidnZ7LXvLkiY7fRVn-

换句话说,你的信息,以及我可以从中收集到的设计的一瞥它表明,即使你使用的是OOP语言,你也可能没有真正实现过飞跃。我鼓励你读这个:
http://blogs.msdn.com/nickmalik/arch...21/328727.aspx




嗨尼克,


我和OOD以及以前的网络应用程序实际上都没问题我编程都遵循OOD原则,但是我注意到了一些类我过去编程的
纯粹是为了启用与数据库的连接

并且还从查询中返回值。


In在这些情况下,我认为

似乎没有任何收益,因为它们没有被用于与其他对象的任何类型的

关系。


如果父类是抽象的,这仅仅是因为没有理由创建它的实例,它是专门通过

继承。


我不得不想知道为什么C#允许将方法和数据字段声明为

static,如果那样你不应该这样做,因为它不是严格的OOD?


干杯

Rich。



Hi Nick,

I''m actually ok with OOD and previous web apps I have programmed all follow
the OOD principles, however I noticed that some of the classes I have
programmed in the past are there purely to enable connection to a database
and also return values from queries.

In these situations I was thinking that there seemed to be no gain from
having multiple intances created as they are not used in any kind of
relationship with other objects.

In the case of the parent class being abstract this is merely because there
is no reason to create an instance of it, it is there to be specialized via
inheritance.

I have to wonder why C# allows methods and data fields to be declared as
static, if then you are not supposed to do this as it is not strict OOD?

Cheers
Rich.


这篇关于何时使用静电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆