PDA

View Full Version : Context aware class libraries


jonas_antonsson
02-16-2005, 09:55 AM
I've mentioned before that it's not easy to create a class library that is "context aware" where a configuration file is dynamically loaded before the class library is accessed / used for the first time.

As need dictates I find it necessary to build a class library that is absolutely self contained (that is - does not rely on a client application to supply or instanciate a context).

One way of doing this is to create embedded resource files in the class library (for IoC control and other context issues) and "control" access to the library via some sort of entry point. For example through a single static initializer class.

This creates problems since the class library is then dependant on being initialized in a certain way which sets up certain limitaions regarding it's usability, and makes it voulnerable to "illegal" use. OO gets a little skewed as well and it kinda isn't a nice design.

So the problem is: How to make a class library context aware no matter how it is accessed, by whom and from anywhere inside the library?

Mark Pollack
02-16-2005, 04:54 PM
Hi Jonas,

I'm scratching my head at this one..the question is more general than Spring usage. Any third party dll that wants to use configuration files and not require the client to make an explict configuration call will have this issue. We need to find a way to automatically execute code inside referenced assemblied when the main executable assembly is loaded. There is a registration facility for this, but it would be equivalent to making an explicit config call. Static ctors are only called when a reference to the type is referenced. The newsgroups have a few postings on this but I can't find a satisfatory answer. Maybe someone else will have a clever solution. I wonder what log4net does. Sorry, no answer yet - just a little feedback.

Cheers,
Mark

Aleks Seovic
02-17-2005, 09:06 AM
Hi Jonas,

.Net 2.0 will have support for "module constructors" on the CLR level, which should allow you to do exactly what you need.

In the meantime, the only solution (more of an ugly hack actually) I can think of is to create something like this in your class library:


internal class SpringContextInitializer
{
static SpringContextInitializer()
{
// initialization code -- create context and register it with ContextRegistry
}

public static void Nop()
{}
}


Then you would have to ensure that each public class in your library makes a reference to a SpringContextInitializer, either by inheriting it or by calling static Nop method within its constructor. That will ensure that your initialization code gets called only once regardless of the entry point into your library.

As I said, it's an ugly hack, but if you absolutely need this behavior it should get the job done.

Later,

Aleks

jonas_antonsson
02-17-2005, 11:35 AM
Hi Aleks.

I'm excited to learn more about this bright new feature in .Net 2.0. Do you have any links for further information about the subject?

In regard to your solution it is along the lines of what I've been doing. I quickly abandoned the "Initializer" method which I described erlier since it quickly became apparent that it wasn't suitable.

The "hack" you describe is - as far as I can see - the only "managable" way to hide class library contex setup completely from anyone that uses the class library. I've created an internal initializer class which I use under a static internal context class (initializes the

It's also true that this problem is something that all 3rd party libraries have in common when it comes to the need of context awareness for the class library - without relying on the implentor or user of the library. And sadly it seems that the only workable solution is some sort of hack. One could go with a configuration module but that can also create problems (in my case it creates huge ones) so it might not be a valid route.