Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-23
5
Medium Priority
?
235 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 1500 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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