DEV Community

Cover image for Episode 004 - The Program and Startup classes - ASP.NET Core: From 0 to overkill

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

João Antunes on October 27, 2018

In this post/video we take a step back to look at some important infrastructure parts of an ASP.NET Core application - the Program and the Startup ...
Collapse
 
dyagzy profile image
dyagzy

Thanks for this post.

Let me ask a question based on this current episode before I read the other episodes(I know their will be changes).

  1. What happens when you have more than one interface (IGroupIDGenerator) and more than one Class implementation(GroupIdGenerator) how do you register them in the IOC container?

2.Do you have to register all the interface in your project with the IOC?

  1. What determines which interface to be registered with the IOC how do I know when to register it?

**
My apologies if my questions sounds foolish (am still a beginner and this whole concept is still alien to me).

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 🙂

Collapse
 
jeikabu profile image
jeikabu

I've got things mostly working with host containers but sometimes something doesn't work when I think it should. So, I enjoy reading posts on ASP.NET Core.
You reminded me that I definitely don't "get" middleware yet.

Collapse
 
joaofbantunes profile image
João Antunes

I have a post planned to go a bit deeper on middlewares, but when I deliver it, you probably already read about it and understand it 🙂

Collapse
 
vekzdran profile image
Vedran Mandić

Seems like gists are buggy on dev.to or you have misplaced them in this post? Good series.

Collapse
 
joaofbantunes profile image
João Antunes

Thanks for the heads-up!
Probably a bug, as it looked ok in the post's source, but saving again seems to have fixed it.