Forum C# no static properties and methods

no static properties and methods

Postby GBurg » March 4th, 2011, 4:33 pm

Hi all,

I just finished writing my server side code, and I use static properties and methods. To give 1 simple example, I use the singleton pattern, which uses a static propertie and method to create the singleton class.

To give another example, I use a static variable called instanceCount, that counts how many instances are made of that class.

However PlayerIO is disallowing me to use the static keyword...?

2 questions:

1) WHY, it's a perfect legal way of coding, right, how would this lead to unmanagable code, or resource hungry code?
2) How to work around this restriction (without buying the enterprise scheme, because I am not a big enterprise yet ;))
GBurg
Paid Member
 
Posts: 78
Joined: February 9th, 2011, 10:27 am

Re: no static properties and methods

Postby Henrik » March 4th, 2011, 7:07 pm

Hey GBurg,

You have to remember that your serverside code runs inside our game servers, and the servers will create instances of rooms when they are created. You should think of each room as a self-contained entity, and all code that runs inside one room has to be self-contained and threadsafe. Also, when a room is closed, all resources associated with it should be destroyed.

Allowing static fields breaks this model, since the rooms that happen to be created on the same server suddenly share data, and those resources are not tied to a single room. We also recycle appdomains, so static fields are not really global, they make it possible to write code that isn't threadsafe, and they're on a completely different lifecycle than rooms. And therefore they're not allowed.

Instead, if you need instances of other classes you create, attach them to the room, that way they'll get created and destroyed together with the room.

We do allow static methods, so you can have helper classes that have functions that return something based on their input, and you can share those across roomtypes if you need, but if some piece of code is specific to a roomtype, just put it on the room. Or the player, if that makes more sense.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: no static properties and methods

Postby GBurg » March 4th, 2011, 8:08 pm

Thanks, I already came up with that idea. However, I will need to give an instance of the room to each of my objects, which is not so good OOP practice off course. But I understand your arguments ;) I will work around it.
GBurg
Paid Member
 
Posts: 78
Joined: February 9th, 2011, 10:27 am


Return to C#