Trying to understand DI.
What is correct way to use services/dependency objects in custom classes?
Do i need to create each class as service and add to dependency objects?
Or should I be using [FromServices] (previously, [Active] before beta4 ) attribute.
or is there is a service object I should be passing to access them?
What trying to understand, is how i properly code my own classes to use the DI like the controllers etc.
[FromServices]
is just an MVC concept. It is not available to other parts of the ASP.NET 5 stack.If you want to pass dependencies down the chain you have a few options:
!! Don't confuse DI with Configuration. DI makes sense when you have a dependency on a contract. Configuration useful when you need some information that is specific to the current implementation.
Example: if you have an
IRepository
then you shouldn't inject the connection string because connection strings are specific to a system that you connect to. There are cases when connection strings don't make sense. For example, anInMemoryRepository
will not require a connection string and thus that is not a common dependency for all implementations.