[Conditional] question

  • Thread starter Thread starter Marc Jennings
  • Start date Start date
M

Marc Jennings

Hi there,

I am trying to get some conditional functionality into some of my
apps. I can use the format :

[Conditional("DEBUG")]
private void doStuff()
{
//do something
}

as expected, but I would like to makr it a little more useful in a
multi-machine environment.

I want to make tweaks to an application based up whether it is running
on my development laptop, the stage server or the live server, and
conditionals seem to be the most efficient way forward. However, I am
not sure how to go about defining an environment variable to trigger
the consitional method.

Here's what I *think* I want to do...
**snip unnecessary stuff**

string outputMessage;
string productionServer;

private string statusMessage()
{
outputMessage = "This application is running on " + Environment.HostName + ". ";
productionServer = "This is the production server. ";
isDevBox();
return outputMessage + productionServer;
}

[Conditional( ***HERE IS WHERE I AM COMING UNSTUCK****)]
private void checkIsDevBox()
{
//reset things like connection strings, data table names, development queries, and whatever else I need
productionServer = "This is one of the development of stage servers. ";
}
**snip unnecessary stuff**

It seems that I need to check if Environment.Hostname matches one of
my two development server names, and if so, set a flag that
[Conditional] can work with. It's all very well to know this is what
I need to do, but I have not the first clue about how to go about that
bit.

Can anyone help, please?

Thanks
Marc.
 
Marc,

You aren't going to be able to do this. The reason is that the
Conditional attribute is evaluated at compile-time, not run-time. It is
strictly for pre-processor values, not environment values.

If you want to do something based on the environment, you are going to
have to write code that will detect the environment you are in, and then
make the appropriate calls.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc Jennings said:
Hi there,

I am trying to get some conditional functionality into some of my
apps. I can use the format :

[Conditional("DEBUG")]
private void doStuff()
{
//do something
}

as expected, but I would like to makr it a little more useful in a
multi-machine environment.

I want to make tweaks to an application based up whether it is running
on my development laptop, the stage server or the live server, and
conditionals seem to be the most efficient way forward. However, I am
not sure how to go about defining an environment variable to trigger
the consitional method.

Here's what I *think* I want to do...
**snip unnecessary stuff**

string outputMessage;
string productionServer;

private string statusMessage()
{
outputMessage = "This application is running on " + Environment.HostName
+ ". ";
productionServer = "This is the production server. ";
isDevBox();
return outputMessage + productionServer;
}

[Conditional( ***HERE IS WHERE I AM COMING UNSTUCK****)]
private void checkIsDevBox()
{
//reset things like connection strings, data table names, development
queries, and whatever else I need
productionServer = "This is one of the development of stage servers.
";
}
**snip unnecessary stuff**

It seems that I need to check if Environment.Hostname matches one of
my two development server names, and if so, set a flag that
[Conditional] can work with. It's all very well to know this is what
I need to do, but I have not the first clue about how to go about that
bit.

Can anyone help, please?

Thanks
Marc.
 
Hi,

I dont think that will work, you are detecting the environment at runtime,
the conditionals are evaluated at compile time.

A possible solution would be define a preprocessor in the project
configuration, if you have a different project definition file in each
machine you can do what yuoui want, how factible this is would depend of
your project, if you add a new file or a new reference to one of the
projects you will need to do the same thing in the others, that is the only
( HUGE ) drawback I see. you can copy the source code though.

It probably is not a solution but may be a work around, it's the best I can
think of right now.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Marc Jennings said:
Hi there,

I am trying to get some conditional functionality into some of my
apps. I can use the format :

[Conditional("DEBUG")]
private void doStuff()
{
//do something
}

as expected, but I would like to makr it a little more useful in a
multi-machine environment.

I want to make tweaks to an application based up whether it is running
on my development laptop, the stage server or the live server, and
conditionals seem to be the most efficient way forward. However, I am
not sure how to go about defining an environment variable to trigger
the consitional method.

Here's what I *think* I want to do...
**snip unnecessary stuff**

string outputMessage;
string productionServer;

private string statusMessage()
{
outputMessage = "This application is running on " + Environment.HostName
+ ". ";
productionServer = "This is the production server. ";
isDevBox();
return outputMessage + productionServer;
}

[Conditional( ***HERE IS WHERE I AM COMING UNSTUCK****)]
private void checkIsDevBox()
{
//reset things like connection strings, data table names, development
queries, and whatever else I need
productionServer = "This is one of the development of stage servers.
";
}
**snip unnecessary stuff**

It seems that I need to check if Environment.Hostname matches one of
my two development server names, and if so, set a flag that
[Conditional] can work with. It's all very well to know this is what
I need to do, but I have not the first clue about how to go about that
bit.

Can anyone help, please?

Thanks
Marc.
 
Thanks for that. I guess I would have seen this if I had read the
docs properly.

It's not a problem, though. I'll just have to go back to the old
switch(Environment.MachineName) kind of thing (Which I probably prefer
anyway...)

Thanks again, both responders...
 
I would even suggest refactor the code into some type of factory pattern to
make the code more managable.
 
Alternatively, you could just use different config files on you different
systems that contain the proper configuration for the environment. That way
you don't have to hard-code machine names or the such.

Ken
 
To add to Daniel's reply,

Create an object that encapsulates the differences in functionality that you
need between your dev environment and your production environment (and any
others). You can have multiple child classes, one for each environment, all
inheriting from the same interface.

Then, when your app starts, call a factory method to return a child object
that matches the environment. The factory method can either refer to the
config file to detect the environment or use machine name or whatever you
want (I prefer config file, but it's your call). After that, none of your
code needs to ever write a line of different code for "production" vs "dev
environment" .

--
--- 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.
--
 
Back
Top