0

I have a Windows App and Web App.

I have a common business logic layer that I want to use to call some settings from.

In the Web app I want to call settings from the web config If its a Windows app I want to call them from the registry.

MyApp.Windows

MyApp.WebApp

These both call the business logic app

MyApp.BusinessLogic

Is there any way of identifying the calling assembly ?

Welsh King
  • 3,178
  • 11
  • 38
  • 60
  • Yes, there is, but you should not do this if you can avoid it if you don't want to run into trouble later. Why not having the App layer tell the business layer which kind of settings (web config or registry) it wants? – Doc Brown Sep 05 '11 at 12:56
  • See http://stackoverflow.com/questions/6551954/how-to-find-out-which-assembly-handled-the-request/6552237#6552237 – John Saunders Sep 05 '11 at 12:58

2 Answers2

4

You should not make this kind of decision in the library. The library should take it's setting via passing in some kind of config object, to a constructor or a method. THen you just populate that config object from the registry in the windows app and the config file in the web app. That way the library does not need to know where it is getting called from.

Ben Robinson
  • 21,601
  • 5
  • 62
  • 79
0

The settings are external to the BusinessLogic, as Ben Robinson pointed. The Business Logic does not need to know, or care, who called it and how. You can accomplish this via Dependecy Injection. Your code might look something like this:

//in MyApp.BusinessLogic
public class MyBusinessObject
{

   private IMyConfiguration _configuration;

   public MyBusinessObject(IMyConfiguration configuration)
   {
     _configuration = configuration;
   }

   //.. code that uses the configuration to do what's needed
}

//in MyApp.Entities (any project that is visible anywhere)
public interface IMyConfiguration 
{
   //whatever configurable properties are needed
}

//in MyApp.Windows
public class MyRegistryConfiguration : IMyConfiguration 
{
  //class that loads settings from the registry
}


// somewhere in the code
IMyConfiguration configuration = new MyRegistryConfiguration ();
MyBusinessObject business = new MyBusinessObject(configuration);
// use MyBusinessObject to do businessy things

I prefer this approach, as it is more flexible, but you could also use the app.config route for Windows applications, that will enable you to read configuration files from both windows and web applications

SWeko
  • 30,434
  • 10
  • 71
  • 106