Reputation: 825
I have two projects and a portable library. Both projects are referencing this library. Is there any way that the library can provide two different implementations of a method for each of the two projects?
For example, the projects are Server and Client and the library has a class with a method called SendItem.
Server's implementation of SendItem:
Send(ID);
Send(Name);
Send(Price);
Client's implementation of SendItem:
Send(ID); //Server will retrieve rest of information from database
I'd rather not use an additional parameter indicating which project is calling the method since this method is called by many classes and things will get ugly and complicated. Is there any way that this can be done using compilation conditions? (Possible to compile library multiple times for each project that's referencing it?).
If it's not possible to achieve this using compilation conditions, is there any clean way to do this without messing other things up?
Upvotes: 1
Views: 65
Reputation: 5772
Look at the service locator or Inversion-of-Control (IoC) patterns. Daniel has a pretty good write up about how you would apply this to a portable library: http://blogs.msdn.com/b/dsplaisted/archive/2012/08/27/how-to-make-portable-class-libraries-work-for-you.aspx.
Upvotes: 1
Reputation: 6039
You can do something like this with C# Preprocessor Directives, similar to the way it is done in C/C++, but code written in this way is harder to maintain and more brittle as time goes on. It would be better to write a couple of different functions than clutter up the code with:
#if (ANYTHING)
Console.WriteLine("ANYTHING is defined");
#else
Console.WriteLine("ANYTHING is NOT defined");
#endif
You get the idea.
Here is a reference to the MSFT Preprocessor info so you can read up on it: http://msdn.microsoft.com/en-us/library/ed8yd1ha(v=vs.71).aspx
Upvotes: 0