Use Unity Ioc Container in ASP. NET MVC

Source: Internet
Author: User

Preface

.

Translated as a repository, as its name implies, is a repository for storing things. It can be understood as a mechanism for encapsulating storage, reading, and searching behaviors. It simulates an object set. It is used to manage and solve the Coupling Degree between it and the Controller. Next we will provide a simple example step by step.

Install Unity

(ASP. NET MVC4.0), select tool-Library Program Package Manager-Package management console, and enter the "Install-Package Unity. Mvc4" command. VS2010 may need to Install NuGet first.

The latest version.. net framework version requirements. If you do not know whether a specified version is installed, you can see that we have installed version 3.0. Modify the project to version 4.5 and reinstall it.

After the project is successful, we find that "Microsoft. practices. unity and Microsoft. practices. unity. configuration, and a Bootstrapper class file, which is translated as a boot program, that is, the Ioc container.

                                 container =              DependencyResolver.SetResolver(                                         container =                                                                              }
Add service layer

                                   Id { ;            Title { ;            Author { ;            Content { ;           DateTime CreateTime { ;      }

Class, in line with the Dependency inversion principle, we can use Unity for dependency injection.

                                 IEnumerable<Article>         Article Get(                    Delete(     }

To perform basic operations.

                 List<Article> Articles =  List<Article>                                     Add( Article { Id = , Title = , Content = , Author = , CreateTime =             Add( Article { Id = , Title = , Content = , Author = , CreateTime =             Add( Article { Id = , Title = , Content = , Author = , CreateTime =                                                                                                                           Article Get(               Articles.Find(p => p.Id ==                                                                          (item ==                     ArgumentNullException(                                                                                          (item ==                     ArgumentNullException(                index = Articles.FindIndex(p => p.Id ==              (index == -                                                                                                 Delete(              Articles.RemoveAll(p => p.Id ==                    }
IArticleRepository type ing

                         container =                                          container.RegisterType<IArticleRepository, ArticleRepository>                                      }

                                                    

For configuration file configuration, see http://www.cnblogs.com/xishuai/p/3670292.html,?configuration file code:

     UnityConfigurationSection configuration =     configuration.Configure(container, );

Service injection to Controller

Dependent on the abstract IArticleRepository interface, rather than the specific implementation class of ArticleRepository.

                                                .repository =                          data =                   }
Global. asax Initialization

                                         }
Postscript

 

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.