ASP. NET MVC Autofac a little bit of dependency injection (including feature injection)

Source: Internet
Author: User

Preface

We all know the importance of the IOC. Convenience is also known. The new ASP. NET core also uses this technique extensively.

I've been holding on to an article that didn't write ASP. Or be afraid to mislead you.

This is not about the core today.

I wrote the C # development mobile app series on the first and last of the most recommended--maybe we really don't like it.

Never mind.. Don't say much nonsense. Start today's stuff.

This article does not explain why we want to use IOC. Only talk about AUTOFAC use some of the careful

Body1. Basic Injection

First we will construct a container with the following code:

// The first step: construct a AUTOFAC builder container New Containerbuilder ();

Step Two:  Inject the services and inheritance relationships you need. Similar to the following:

// inject storage Builder. Registergeneric (typeof(repository<,>)). As (typeof(irepository<,>)). Instanceperlifetimescope ();

Well.. Explain what it means here.

Registergeneric injects your service class, and later as is the interface that your service class inherits.

Instanceperlifetimescope the life cycle of the service that you inject. ( Note: Life cycle we speak behind )

2. Controller Injection

Using the method of attribute injection, injection controller

// Injection control Controller Builder. Registercontrollers (Assemblys. ToArray ()). Propertiesautowired (). Instanceperlifetimescope ();

Well.. The Assemblys in this. ToArray (). Is the assembly you want to inject into the controller.

Well, I was directly through the unified interface and then batch injection. You're at your own discretion.

Propertiesautowired () means using the attribute injection method

3. Feature Injection

When we use MVC, we will definitely use features. There must be some features that you define yourself: Then these features need to use the relevant services, how to inject it. As follows:

// injection feature builder. Registerfilterprovider ();

Yes.. You did not read it correctly. There's just a word in MVC. The desired service can be taken from the features . (Note: Webapi not, please check the Official document for details)

4. Build the container and provide it to MVC

The code is as follows:

var container =// provided to MVC Dependencyresolver.setresolver (    New Autofacdependencyresolver (container));

5. Some assorted
// inject class with this Builder. Registertype (type to inject)// inject generic class with this builder. Registergeneric (type to inject)

6. About the life cycle

Instanceperdependency ()

Instantaneous, uh.. Is that the service will return a separate instance for each request

Instanceperlifetimescope ()

A thread-or request-based singleton: Well, it's a request or a thread sharing a

SingleInstance ()

Well.. Nothing else is a single case. The entire project is a common

Instanceperrequest ()

Well.. For MVC, or ASP. Each request single example

That's the end of it.

finally

Well.. The content is not many, wins in the practical. Share it, the problem of the place to hope that the great God, also by the way is a record of their own it.

ASP. NET MVC Autofac a little bit of dependency injection (including feature injection)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.