Reputation: 1075
I will describe the situation in abstract terms, as the actual subject of the code is irrelevant for the time being. This question is merely about the architecture/code organisation in the project.
I am creating a set of reusable C# libraries.
Assembly A offers a set of related features.
A few of these features require an implementation of ISomething
.
The implementation of ISomething
to use is determined in a static configuration class provided by A, Config.Something
.
Assembly B contains an implementation of ISomething
, namely DefaultSomething
.
I am looking for a way to
Config.Something
should automatically be set to/assumed to be DefaultSomething
.ISomething
. In this situation, assembly B should not be required and users should be able to assign a custom object to Config.Something
.ISomething
s, so neither B nor an assignment to Config.Something
are required.While cases (2) and (3) are trivial, I am not sure how to best design the library to support case (1). I have examined various ways to solve this, but each of them has some drawbacks:
Config.Something
= new DefaultSomething()
themselves in case (1). However, I do not want to require this sort of boilerplate code. Case (1) should be really "plug-and-play", or rather, reference-assemblies and write custom code using A.DefaultSomething
by means of reflection the first time an ISomething
is required. However, especially when many assemblies are loaded, this seems quite wasteful and slow. I might be able to identify B relatively quickly by marking it with a custom attribute declared in A that is not publicly visible, but giving B access to A's internals. This sounds somewhat hacky, though.Is there any good way to do this?
Upvotes: 1
Views: 41
Reputation: 439
Without specific examples there will be a lot of ways to achieve that.
Generally speaking, looks like you need a IoC Container (like Unity/Ninject if you are using .NET) and/or a plugin system (handmade, or MEF if you are using .NET).
Upvotes: 1