Aps.Net WebApi Dependency Injection

Before we begin, let's first take a look at what is dependency injection?

In programming, an implementation of a control dependency injection is inverted and dependencies for Solving the design pattern. A dependency refers to an object may be utilized. Dependency injection is a dependent object depends transmitted to the to be used. The service will become part of the client state and deliver services to the client, and the client is not allowed to establish or seek services. Dependency injection so that our programming code becomes loose coupling and easy to manage.

  Injection of dependency types:

Constructor injection: constructor injection, the injector service (dependent) via a client class constructor.

Injecting property: injection in the property (aka Setter injection), the injector is provided by a common property-dependent client class.

The method of injection: In this type of injection, the client class implements an interface, the interface to provide dependency declaration method, and injection uses this interface to provide client-dependent class.

Benefits depend on injection:

  • Reduce dependency
    • Dependency injection can reduce or eliminate unnecessary inter-component dependencies. When the component changes to reduce the impact of components
  • Enhanced reusability
    • Reducing component dependencies can increase reusability of components. If a different implement an interface in a different context, the same or a different configuration is only achieved, the assembly can be configured to use this implementation. No code changes.
  • Code to increase testability
    • Dependency injection also increases the testability of the assembly. When the dependency can be injected components, these means may be injected into an analog implementation dependencies. Object behavior simulation for testing as an alternative to the actual implementation, and may be configured to simulate the object
  • Enhance the readability of the code
    • Dependency injection dependency may be moved to the interface component. It makes it easier to see what components have a dependency relationship, so that the code more readable.
  • Reduce dependency carrier
    • Carrier-dependent code is generated in a large amount of "noise", making it difficult to read and maintain, and makes assembly more difficult to test. Dependency injection and reduce dependency and static load using a single embodiment, may be linked together perfect assembly

1. Autofac choice needs to be installed in the Nuget, Autofac.WebApi2

 

2. Add the following code Application_Start method in Global.asax:

// * DI * / 
var Builder = new new ContainerBuilder (); 
builder.RegisterApiControllers (Assembly.GetExecutingAssembly ()); 

// injection project name (all objects BusinessRepository, MysqlDBRepository project injection)
 // var = BusinessRepository Assembly. the Load ( "BusinessRepository");
 // var = MysqlDBRepository the Assembly.Load ( "MysqlDBRepository");
 // builder.RegisterAssemblyTypes (BusinessRepository) .AsImplementedInterfaces ();
 // builder.RegisterAssemblyTypes (MysqlDBRepository) .AsImplementedInterfaces (); 

// single Object injection (in the project need to use objects must be implanted injection) 
builder.RegisterType <ProductBusinessRepository> (). of As <IProductBusinessRepository>  ();
builder.RegisterType<ProductRepository>().As<IProductRepository>();

var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;    

3. In the controller dependency injection

 

 4. Get Data Interface

5.Demo Institute Add: https: //pan.baidu.com/s/1XYOLmOQDnTMEW68oM2Q7XQ 

Extraction code: 6ski 

Guess you like

Origin www.cnblogs.com/2018clg/p/10948694.html