-7

I'm using dependency injection in my project and wondered if it would be easier to just register all 30+ of my classes that inherit a certain interface using a for each, that way it works dynamically.

I've looked it up on Google, SO, RJ, YHA, and more Q&A sites but nothing helps. It just explains if you already have a collection of them classes, and none show you how to do it from just an interface.

For example, I need to get a list of Class1, 2 and 3, and any other clases that inherit IClass.

class Class1 : IClass {}
class Class2 : IClass {}
class Class3 : IClass {}

If I were to later on declare these, they would also be found.

class Class4 : IClass {}
class Class5 : IClass {}
class Class6 : IClass {}
fskdjwe
  • 157
  • 8
  • So, are you going to use it? Do you want to bind `Class1` and all others to itself? Are you going to request `Class1` from DI container? – Yeldar Kurmangaliyev Aug 24 '18 at 18:25
  • @Phiter I wouldn't say that it is a full duplicate, but this is a helpful article if the OP decides to use reflection to achieve it. – Yeldar Kurmangaliyev Aug 24 '18 at 18:26
  • I'm going to use it yes, @Phiter I don't want to use LINQ. I also need the class as a TImplementation – fskdjwe Aug 24 '18 at 18:27
  • 2
    `I don't want to use LINQ`; ... why not? – Stefan Aug 24 '18 at 18:28
  • I don't need to explain that, its off topic to the question. If I explained my reason, many would want to change my mind, so I don't see the point in explaining. – fskdjwe Aug 24 '18 at 18:29
  • You can use refection for that. But might be quite slow and you will need to preload all assemblies that might contain implementations of that interface. – ZorgoZ Aug 24 '18 at 18:29
  • 1
    @fskdjwe: it's not off topic because you add it as restriction and could be used in an answer. Nevertheless if you know your reasons than that's okay. But, keep in mind; a lot of these `I don't want to use ... because it's ...` is seldom actually correct, that's why people are fishing. – Stefan Aug 24 '18 at 18:47

1 Answers1

2

DI-containers usually provide some functionality for such purposes. For example, with Autofac you can use Assembly Scanning like this:

var dataAccess = Assembly.GetExecutingAssembly();

builder
   .RegisterAssemblyTypes(dataAccess)
   .Where(t => t.Name.EndsWith("Repository"))
   .AsImplementedInterfaces();

This example shows how to register all repositories in executing assembly. Because your components implement the same interface, I guess you can find some naming pattern to register them like that.

  • I'm using MS's IServiceCollection, I don't think it supports this, although I could be wrong. – fskdjwe Aug 24 '18 at 18:48
  • 2
    If not, you can use reflection as shown in the question [Getting all types that implement an interface](https://stackoverflow.com/questions/52009908/getting-a-list-of-classes-that-inherit-a-specific-interface/52010071#52010071) that has already mentioned by @Phiter – Andrei Prigorshnev Aug 24 '18 at 18:54