Solved

How to prevent calling of public method by caller application type compile time?

Posted on 2014-01-23
5
232 Views
Last Modified: 2014-01-29
Hi,

i have a common library which contains several public methods. some methods include session and application objects which will be available in web projects. but because of publicitiy of those methods, a windows forms application can call those methods too. it will produce run time error because of unavailability of session objects.

what would be the best way to prevent those methods usage in compile time from windows applications? i am seeking for an easy implementation, complex solutions will not be useful for me.

thx.
0
Comment
Question by:damarbt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39802994
>>what would be the best way to prevent those methods usage in compile time from windows applications?

That implies two versions, one for web and one for windows apps.  (For one version then the answer is simple - you can't do that).
You can define a symbol and then at compile time activate or deactivate code as appropriate.

eg.
#if WINAPPS
#else
  public void Foo()
  {
  }
#endif

so at compile time the version where WINAPPS is defined will not have the function Foo available.
0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 39803071
To me it seems as though you should actually have two assemblies, one for the web and one for the desktop apps.  If they share code that you (actually) want shared then make a third assembly for them both to use.

There is no way to have the code in there (and compiled) and have public methods not callable.  However as stated above if you are recompiling with a flag that is defined in your desktop, you can surround the code as shown.

And of course, any method should do its job and handle exceptions and bad arguments/variables before using them.  So, if you need to know if session is there then test for it before using it.  ASSERT that what you need is valid...though at that point the method would likely be pointless I assume, and a desktop programmer should not be calling it.
0
 
LVL 40
ID: 39804606
If these methods need to be called only internally by the Library, declare them as internal. They will work as public in the library, but private outside of it.

If you need to use that Library with some of your own applications, then declare them as Friend assemblies, and they will be able to get access to the internal methods. See http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx
0
 

Author Comment

by:damarbt
ID: 39806555
seperate libraries is possible but not a great way i guess.

what about checking HttpContext.Current == null condition. For nonweb applications HttpContext.Current will be null but i am not sure if it is trustable or not
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39806610
As I said earlier if you wanted to implement at compile time then you do need two versions.
With one version you can never prevent a windows app calling a web specific component.

One possibility which would work is to have a singleton.  That contains an enumerated value.  This has three values: not initialised (the default), web and windows.  You set if this value is web or windows yourself.  Any call into the function and that finds the value in the singleton is not initialised then you force the function to crash with a warning to the developer that it must be initialised before the library can be used.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question