This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21011|Reply: 0

[Console Program] C# Unity Series (1) Console programs implement interface dependency injection

[Copy link]
Posted on 2020-7-16 17:04:11 | | | |
Unity Containers (Unity) are full-featured, extensible dependency injection containers. It helps build loosely coupled applications and provides developers with the following benefits:

Simplified object creation, especially for hierarchical object structures and dependencies
requirements abstraction; This allows developers to specify dependencies in the runtime or configuration and simplifies the management of cross-cutting concerns
Increase flexibility by deferring component configuration to containers
service positioning capabilities; This allows clients to store or cache containers
Instance and type interception
Agree to register

Start by creating a new csharp-unity-demo console application.

nuget installs 2 packages with the following command:

Create a new IUser interface and a new User class, the code is as follows:



Configure the App.config file

Could not load file or assembly 'Microsoft.Practices.Unity.Configuration'
https://www.itsvse.com/thread-9220-1-1.html

Define a Unity node with containers in it, and there can be multiple containers in the collection, as follows:

The abbreviated configuration is as follows:



In the Program file entry method, call the GetUserName method of our newly created IUser interface through Unity, the code is as follows:

The results of the implementation are as follows:



As you can see, every time we use Unity, we instantiate a new User object.

Source code download:The hyperlink login is visible.





Previous:(Error) cvc-datatype-valid.1.2.1: 'xx' is not a valid value for 'gYearMonth'.
Next:Linux looks for files that contain a certain character under the folder
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com