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