0

Let's say, I have an interface ISomething. I also have up to n implementations of the interface, for example ASomething and BSomething. I want to determine all the implementations of that interface and save them to a list or another useful data structure.

One solution I know is, find all implementations via a reflection. My question is: Is this the best way to do it? Is there a pattern that each implementation registers itself to the system by calling a method? If yes, how is it called? As the project grows, looping over all the classes just two find the two implementations seems inefficient to me.

The language here is vb.net but the question targets object oriented languages in general.

Edit: To be a bit more specific on the question.

I have a UI which can create projects. Previously, these projects had different data but were all of the same type. A project is generally generated by copying a template file into a folder and then filling some values.

The situation now is, that a project can either be of type A or type B. The difference is, that a different template file is copied and also some other data is loaded differently. There is also a chance, that in the future there might be the type C, but I don't know this yet. My idea was the creation of an interface and several implementations of this interface. The UI would then, upon loading, look for all implementations of that interface and give the user a choice via a dropdown or similar control.

I thought when another developer creates C, it would be good if this new functionality automatically appears in the user interface without the other developer touching the code for the UI. I was hoping to avoid endless if-else chains and also I wanted to avoid an explicit specification of each project type.

This idea lead to the question above.

  • 1
    This is a tooling question. Tooling refers to tools and utilities that work in conjunction with the compiler, or relies on information provided by the compiler to fulfill their own functionality. Reflection is one answer to your question; the highlights of this approach is that (1) it is a runtime approach that requires compiling and building an executable; (2) you can build a runtime (executable) that is different from your main application - just add it as if it were an assembly reference. Your other approach, registration, places additional runtime requirements, so they aren't comparable. – rwong Apr 11 '17 at 22:53
  • 1
    It depends entirely on the programming environment and libraries. Less code is generally preferable to more code, and the performance depends on the tools, as rwong stated. – Frank Hileman Apr 11 '17 at 23:21
  • 1
    Do you mean finding them so a programmer has a list to go make edits, or finding them during the normal course of running your program so the software can make calls on the interface? – Karl Bielefeldt Apr 12 '17 at 0:11
5

The 'software engineering' answer would be 'why are you trying to discover instances of interfaces?'

In general its not something you would do. You code should specify what class to use for the various interfaces. Otherwise you might pick up the wrong one. For example a mock used in testing. Or more generally, every IDisposable in the .net framework.

Now you might be writing a DI framework or something where it would be useful. But there are plenty out there already...

Edit in response to the extra detail:

Add the A B and C implementations to a Dictionary and populate the drop down with the string key.

When a user selects the project type you will be able to retrieve an instance of the correct type in the dictionary.

If you need new instances put a simple factory object into the dictionary instead so you can do...

IWhatever x = dict[key].CreateNew();
  • Thank you for the answer. I would not use the word "framework", it's a rather small use case where I want to integrate certain behaviors derived from an interface without a giant loop or nested ifs. – Freddy Apr 12 '17 at 5:28
  • 1
    @Freddy This sounds like it could be an XY Problem. I'd suggest putting that information into the question and being a bit more specific about the actual problem you need to solve - there may be other alternative solutions which don't require reflection. For example, Aspect Oriented programming – Ben Cottrell Apr 12 '17 at 6:52
  • @BenCottrell I updated my question with the some background information. – Freddy Apr 12 '17 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.