Dónde colocar constantes en un proyecto WCF

I am new to WCF, Where do I place the constants for my project?

I generally create a file with a namespace that holds all my static constants. But we cannot have static variables in WCF if the service is per-call or per-session. I don't want magic numbers in my code, I want constants that can be shared through out the project.

Muchas Gracias

preguntado el 01 de febrero de 12 a las 14:02

por qué no puedes usar const? if they change per-call or -session, then they really aren't constants. -

what is the problem with static readonly properties in that class that holds your values? -

4 Respuestas

Si ellos estan realmente constants, you would just put them in the relevant files with const keyword. Constants cannot be changed thefore they are thread safe.

If you need to change the value of a variable, then it's not a constant and you need to think who can change it. If it is a user's responsibility, then you can put variables in a .config file and read them from there. If code needs to change the variables, then you can use some locking mechanism to read/modify the values (lock, ReadWriteLock, etc).

Respondido 01 Feb 12, 18:02

he want to share this values 'out(side) the project' using constants is pretty bad cause they are evaluated at compilertime. This will force you to recompile all assemblies that depend on those constants if you change their value. - Kolja

@Kolja Is exactly right. Using const forces a recompile whenever the value has to change. Something more flexible like XML configuration or settings files is the way to go here - based on the OP requirements. - ¡Qué asco!

@Yuck as I am reading the question right he wants to avoid magic numbers not adding configuration options. IMHO the Settings are are a overkill for this. - Kolja

Settings files add one section to your config file. It's tiny. And the values are internally accessible to all parts of the project. - ¡Qué asco!

El problema con el uso de const or even classes which wrap magic numbers is that they can't be changed without re-compiling. You want to use a settings file. These aren't very intuitive to set up, so follow these steps. Right-click your project and choose Propiedades. Then find the Ajustes lengüeta:

enter image description here

Click the hyperlink which will create a Settings.settings file in your project. You can then edit the values in the designer:

enter image description here

Values are stored in your app.config or web.config (in your case, since it's a WCF project).

Respondido 01 Feb 12, 18:02

A Simple static Class with static read only properties should work.

Public static class Foo
{
    public static int Bar { get { return 1337; }}
}

Respondido 01 Feb 12, 18:02

Strange that this is your answer, when you commented on another suggestion that const requires recompilation when a value changes... This isn't immune to that short-coming either. - ¡Qué asco!

@Yuck this IS immune to the shortcoming that, const is evaluated by the compiler even if you use that value outside the declaring assembly. You just need to recompile the declaring assembly and not the using assemblies what would be the case if you use const - Kolja

You just need to recompile... IMHO, that's still a little heavy-handed just to change a value. - ¡Qué asco!

The OP wanted to know how to avoid magic values in his code. Those are pretty unlikely to change often. Also if you want to use that values in more than one assembly it is impossible with our Solution, except you copy the same file over and over again. - Kolja

OP says - I want constants that can be shared through out the project. Eso es proyecto y no solución. Could be the OP misspeaking, but as it's written a settings file works just fine. - ¡Qué asco!

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.