concept concrete type in category dependency injection

appears as: concrete type, concrete types, concrete types, concrete type
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

You can’t create a new instance of an interface the same way that you create a new instance of a concrete type. Code like this doesn’t compile:

Listing 12.3 Using AutoWireContainer to register HomeController
var container = new AutoWireContainer();    #1  

container.Register(    #2  
    typeof(IUserContext),    #2  
    typeof(AspNetUserContextAdapter));    #2  
    #2  
container.Register(    #2  
    typeof(IProductRepository),    #2  
    typeof(SqlProductRepository));    #2  
    #2  
container.Register(    #2  
    typeof(IProductService),    #2  
    typeof(ProductService));    #2  

container.Register(    #3  
    typeof(HomeController),    #3  
    typeof(HomeController));    #3  

container.Register(    #4  
    typeof(CommerceContext),    #4  
    () => new CommerceContext(connectionString));    #4  

#1   Creates a new container instance
#2   Registers a mapping between an Abstraction and a concrete type without having to specify the type’s Dependencies. Note that because the container uses an internal dictionary, the order in which you make the registrations is irrelevant.
#3   In the case of HomeController, the Abstraction and the concrete type are of the same type. This means that any time you request a HomeController, you get a HomeController.
#4   When a CommerceContext is requested, the Resolve method calls this delegate.

Figure 12.4 Simplified workflow for Auto-Wiring mapped to the code from listing 12.1. The registrations dictionary is queried for the concrete type, its constructor parameters get resolved, and the concrete type is created using its resolved Dependencies.

12-04.eps

Not only can Autofac resolve concrete types with parameterless constructors, it can also Auto-Wire a type with other Dependencies. All these Dependencies need to be registered. For the most part, you’ll want to program to interfaces, because this introduces loose coupling. To support this, Autofac lets you map Abstractions to concrete types.

Dependency Injection in .NET

This is an excerpt from Manning's book Dependency Injection in .NET.

By pulling the type name from the application configuration file and creating a Type instance from it, you can use Reflection to create an instance of IMessageWriter without knowing the concrete type at compile time.

Figure 3.4. Simplified workflow for AUTO-WIRING. A DI CONTAINER will recursively find concrete types and examine their constructors until it can create the entire object tree.

If you remember the discussion about resolving components with Castle Windsor, you may recall that Windsor requires you to register all relevant components before you can resolve them. This isn’t the case with StructureMap; if you request a concrete type with a default constructor, no configuration is necessary. The simplest possible use of StructureMap is this: