我是否应该使用公共共享子和公共共享函数?他们在用户之间共享数据吗?
我正在使用 VB 与 .Net 合作,刚刚完成了一个网站项目,我正准备推出它......但当我进行最后的清理/演练时,我注意到我目前使用的是 Public共享功能 62 次,公共共享子系统 14 次,以及一些公共共享属性。
这是我从未完全清楚的一个领域,并且希望正确优化我的代码。
我读过的很多文章似乎都表明,公共共享只是使访问函数或子函数变得更容易,而无需先创建它的实例。
但后来我读到的其他地方讨论了这是否在网站上的用户之间共享。
这是我不确定/困惑的地方。如果 2 个用户同时打开,并且他们都调用相同的公共共享函数或 Sub,事情可以交叉吗?
变量/属性的工作方式相同还是不同?例如,如果我将用户对象存储为公共共享属性,则仅限于该用户,还是所有用户都可以访问它?
I'm working with .Net using VB and have just finished a website project and I'm getting ready to roll it out...but as I go through a final cleanup/walkthrough I've noticed that I currently have used a Public Shared Function 62 times and a Public Shared Sub 14 times, as well as a couple of Public Shared properties.
This is an area I've just never been entirely clear on and want to optimize my code properly.
A lot of articles I read seem to indicate that a Public Shared just makes it easier to access a function or sub without creating an instance of it first.
But then other places I read talk about whether this is Shared amongst users on the site.
This is where I'm unsure/confused. If 2 users are on at the same time and they both call the same Public Shared Function or Sub, can things cross over?
Do variables/properties work the same or differently? For instance if I'm storing a user object as a Public Shared Property is that limited to this user only, or will all users be able to access it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果只能选一个,那么重新运行就对了。但共享存在是有原因的,最好理解它,以便您可以正确地使用它。
假设您有一个仅返回数据库中所有记录的函数,那么可以将其放入共享类中。假设此函数有一个参数,可让您指定要返回的一条记录。在这种情况下,您仍然是安全的,因为每次调用该函数都会向堆栈添加另一个帧并单独跟踪该帧的变量。
但是,如果您开始在函数中使用类成员变量,那么您可能会遇到麻烦。假设您的类使用一个成员变量,其中包含每页要显示的行数。每次用户更改其偏好时,都会影响所有用户。
所以,我希望这能让你明白一点。
编辑:针对您的问题...
考虑 MessageBox 类。如您所知,您不必创建它的实例来使用它的方法。如果同时对其 Show(string text) 方法进行两次调用,则不必担心第二次调用会覆盖第一次传递的消息,因为对该方法的每次调用都会维护自己的变量集。
但是,请考虑以下类:
请注意有关此类的一些事项:
1) Show 必须通过静态类 MyMessageBox 的引用来访问 Message 属性。它不能将其引用为“this.Message”,因为没有该类的实际实例,因此不存在“this”之类的东西。
2)因为这是一个静态类,所以所有属性/字段都必须声明为静态。但是,不要误解您也可以拥有包含静态变量和方法的非静态类。从功能上来说,使你的类静态并不会改变你的代码行为方式......它只会导致编译器在你的类中强制执行某些规则。
将方法设置为静态允许您将其称为 MyClass.SomeMethod() 而不是 instanceOfMyClass.SomeMethod()。这只是语法上的差异。实际上,所有方法始终是静态的(即方法代码永远不会有多个实例......它只是在一处。它是实例化的变量。)。除了语法上的差异之外,使方法静态还强制执行规则,阻止您引用任何非静态属性/字段,这是有意义的,因为您不是从对象的实际实例调用它。
3)最重要的是,请注意,这将是一个可怕的设计此类的方式。如果对类进行两次调用,则在第一个人显示其消息之前,一个人可能将 Message 属性设置为“Hello”,而其他人则可能将其设置为“World”。然后每个人都调用 Show() 方法,您会看到两个显示“World”的消息框。
有时需要这种设计,因此您必须使用多线程技术来使后续调用者排队等待使用特定资源。但对于这么简单的事情来说,这显然是荒谬的。
If you could only pick one, then rerun would be right. But there's a reason shared exists and it's best to understand it so you can use it appropriately.
Let's say you have a function that just returns all the records in a database, then that would be fine to put in a shared class. And let's say this function has a parameter that let's you specify one record you want to return. In this case you're still safe because each call to the function will add another frame to the stack and track the variables for that frame separately.
However, if you start working with a class member variable within your function then that's where you can run into trouble. Let's say your class uses a member variable that contains the number of rows per page to display. Each time a user changes their preference it will affect all users.
So, I hope that clears it up for you a bit.
Edit: In repose to your question...
Consider the MessageBox class. As you know you do not have to create an instance of it to use its methods. And if two calls are simultaneously made to its Show(string text) method, you don't have to worry about the second call overwriting the message the first passed because each call to the method maintains its own set of variables.
However, consider the following class:
Notice a few things about this class:
1) Show has to access the Message property through a reference to the static class MyMessageBox. It can't reference it as 'this.Message' because there is no actual instance of the class and therefore no such thing as 'this'.
2) Because this is a static class, all properties/fields must be declared as static. However, don't misunderstand that you can also have a non-static class with static variables and methods. Functionally, making your class static doesn't change a darned thing about the way your code behaves... it only causes the compiler to enforce certain rules in your class.
Making a method static allows you to be able to call it as MyClass.SomeMethod() instead of instanceOfMyClass.SomeMethod(). This is only a difference in syntax. In reality, all methods are always static (i.e. there are never multiple instances of your method code... it's just in one place. It's the variables that there are instantiated.). In addition to the difference in syntax, making a method static also enforces rules that keep you from referencing any non-static properties/fields which makes sense since you aren't calling it from an actual instance of your object.
3) Most importantly, notice that this would be a horrible way to design this class. If two calls are made to the class, it's possible that one person could set the Message property to "Hello" and someone else could then set it to "World" before the first person displayed their message. Then each person calls the Show() method and you get two message boxes that say "World".
There are times when this kind of design is needed, and so you have to uses multi-threading techniques to make subsequent callers wait in line to use a particular resource. But for something as simple as this that would obviously be ridiculous.
公共共享意味着没有相关类的实例,并且可能在多线程环境中导致重大问题。共享函数还存在一些其他问题,因为它们创建了难以模拟和测试的高度耦合的环境。一般来说,最好避免使用共享函数,并且我想说,如果重构共享属性并不困难的话,请尽可能少地使用共享属性。
Public shared means there is no instance of the class related and can cause major problems in multihreaded environments. There are some other issues with with shared functions in that they create very coupled environment that is difficult to mock and test. It is best in general to avoid using shared functions and I would say to strive to use shared properties as little as possible if re factoring them out is not difficult I would.
这就是
Shared
所要传达的意思,该值由正在运行的应用程序中的每段代码共享。这也意味着,如果您的网站可供更多用户同时访问,则他们在Shared
字段或属性中将具有相同的值。一般来说,如果您了解共享方法不如实例方法灵活,那么它们并没有什么问题。例如,您不能仅更改它在应用程序的一部分中执行的操作。
至于
共享
字段和属性,在多线程环境中应该小心使用它们。在一个线程上更改它们也会在其他线程中更改它们,但由于缓存,更改可能不会立即显示。That's what
Shared
is meant to convey, the value is shared by every piece of code in the running application. That also means that if you have a website that can be accessed by more users at the same time, they will have the same value in theShared
field or property.In general, there is nothing wrong with
Shared
methods, if you understand they are less flexible than instance methods. For example, you can't change what it does only in one part of the application.As for
Shared
fields and properties, you should be careful with them in multi-threaded environment. Changing them on one thread changes them in other threads too, but the change may not show immediately due to caching.