Question

I've been working on refactoring an existing codebase and am stuck on how to hide / avoid referencing an external utility in ALL my projects. Specifically, i'm employing a pattern using castle's dictionary adapter to get config dependencies:

[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)]
public class AppSettingsFromConfigAttribute : Attribute, IDictionaryBehavior, IDictionaryPropertyGetter, IPropertyDescriptorInitializer, IDictionaryKeyBuilder
{
....
}

where all the specified interfaces are coming from Castle.Core. The usage of this attribute though all my projects just becomes something like:

[AppSettingsFromConfig("gpg:")]
public interface IGPGConfiguration
{
    string HomeDirectory { get; set; }
    string KeyDirectory { get; set; }
}

which nominally gets me gpg:HomeDirectory and gpg:KeyDirectory from a config file.

My problem is if the AppSettingsFromConfigAttribute is defined in my "Utility" dll, all my projects (using this feature) need to refer both Utlity.dll and Castle.Core to compile - which i just don't like, it's an explicit reference to Castle.Core that ends up coming all over my codebase.... for interfaces none of the projects care about, only the castle bootstrapper needs them.

In short - class implements a bunch of external interfaces no one cares about except for 1 (external) guy, but rest N customers end up needing the external reference to compile, and i just don't like that behaviour.

So how do i avoid it? or am i missing something trivial?

Was it helpful?

Solution 2

As indicated from the answers, you just can't do it, especially with attributes, it will always be two dependencies (which at most you could mush into a single DLL). So i've changed the structure to use:

[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false)]
public class AppSettingsFromConfigAttribute : Attribute {}

as a simple marker. Looked at the Windsor source and all the interfaces implemented were internally being used to add a PropertyDescriptor to the type when making the dictionary adapter. Windsor being windsor provides an overload to specify a PropertyDescriptor externally, so i now create that myself at runtime using the vanilla attribute, in the bootstrapper - the one place that needs Castle.

OTHER TIPS

It's not possible because IDictionaryBehavior and such are part of the definition of AppSettingsFromConfigAttribute. If you want the consumers of the attribute to not have a dependency on Castle.Core, then you need to remove that from the class definition.

You could then perhaps use Castle.Core dynamically in the attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top