DEV Community

Discussion on: Episode 004 - The Program and Startup classes - ASP.NET Core: From 0 to overkill

Collapse
 
joaofbantunes profile image
João Antunes

Let me try to go through each point:

  1. Having multiple implementations of the same interface in the container is possible, and two approaches come to mind:
    • You really want to use the multiple ones individually, instead of receiving a single one in your class that depends on it, you receive an IEnumerable<IInterface>
    • You actually want just one instance. In this case other implementations could be decorators (I show an example of this in the 005 post) or maybe you want a different implementation depending on the receiver. In this latter case, I don't think you can do it with the default DI container of ASP.NET Core without some extra tricks.
  2. (and I think 3 as well) You just need to register the interfaces (and implementations) that you want to get automatically injected in your other classes. If you have some interfaces whose implementations you instantiate manually, those ones don't need to be registered.
    • As an example, you could have multiple domain classes that represent the result of an operation, and implement an interface with a method that should return a string with its details. Such interface and implementations shouldn't be registered, they're not services that we'd like to abstract the calling code from, but just a way to normalize how to perform an operation with a specific set of classes.

No need for apologies, that's the goal of these posts, I'm glad if they're helpful for people to learn something new.

Hope these answers help, if not, let me know what's still missing, so I can try to improve them 🙂