F-H
F-H

Reputation: 1075

Use class from specific other assembly if that assembly is loaded, performantly and without any boilerplate code

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

  1. let users use assemblies A and B by simply referencing them and accessing types from A. In this situation, Config.Something should automatically be set to/assumed to be DefaultSomething.
  2. let users use assembly A and supply their own implementation of ISomething. In this situation, assembly B should not be required and users should be able to assign a custom object to Config.Something.
  3. let users use only those features from A that do not require ISomethings, 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:

Is there any good way to do this?

Upvotes: 1

Views: 41

Answers (1)

Matteo Marciano - MSCP
Matteo Marciano - MSCP

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

Related Questions