Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Unity control inversion

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the method of Unity control inversion". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of Unity control inversion"?

The control inversion upper layer does not rely on the lower layer, but on the third-party dependency injection container

The last SimpleFactory can be thought of as a third-party container. Student student relies on SimpleFactory instead of directly relying on details (Honor)

Our commonly used third-party container is Unity, which introduces Unity's Dll into VS through NuGet to transform our Main method.

Static void Main (string [] args) {{BasePhone honor = SimpleFactory.CreatePhone (); IPlayPhone student = SimpleFactory.CreateStudent (); student.PlayPhone (honor); IUnityContainer unityContainer = new UnityContainer (); unityContainer.RegisterType (); var studentUnity = unityContainer.Resolve () StudentUnity.PlayPhone (honor); / / Honor honor = new Honor (); / / Student student = new Student (); / / student.PlayPhone (honor); / / student.PlayPhone (lumiaPhone); / / student.PlayApplePhone (applePhone); / / student.PlayGalaxy (galaxy) / / student.PlayHonor (honor);} Console.ReadKey ();}

Then add IPerson,Iiphone,IGame under the code modification.

Public interface IPerson {Iiphone Iphone {get; set;} IGame Game {get; set;}} public class Student: BasePerson, IPerson {[Dependency] public Iiphone Iphone {get; set;} [Dependency] public IGame Game {get; set;}} public class Teacher: BasePerson, IPerson {[Dependency] public Iiphone Iphone {get; set } [Dependency] public IGame Game {get; set;}} public interface Iiphone {void UsePhone ();} public class Galaxy:BasePhone, Iiphone {public override void System () {Console.WriteLine ("ANDROID");} public void UsePhone () {Console.WriteLine ("Galaxy") }} public interface IGame {void Game ();} public class SgsGame:IGame {public void Game () {Console.WriteLine ("play three Kingdoms kill Game");}} public class LolGame:IGame {public void Game () {Console.WriteLine ("Play LoLGame") }} static void Main (string [] args) {{IUnityContainer unityContainer = new UnityContainer (); unityContainer.RegisterType (); var studentUnity = unityContainer.Resolve () StudentUnity.Iphone.UsePhone (); studentUnity.Game.Game ();}

Attribute injection in dependency injection is used here, and attribute injection is performed after constructor injection, and the feature [Dependency] needs to be added, and a reference to using Microsoft.Practices.Unity; needs to be added, so it is more convenient to use constructor injection most of the time, and the method injection project is rarely used, adding [InjectionMethod] attribute to the method.

Unity provides a more flexible way to register with configuration files to add to project configuration files

Then the Main function is modified

Static void Main (string [] args) {{IUnityContainer unityContainer = new UnityContainer (); UnityConfigurationSection configuration = (UnityConfigurationSection) ConfigurationManager.GetSection (UnityConfigurationSection.SectionName); configuration.Configure (unityContainer, "defaultContainer"); var studentUnity = unityContainer.Resolve (); studentUnity.OutputIdentity () StudentUnity.Iphone.UsePhone (); studentUnity.Game.Game ();} Console.ReadKey ();} at this point, I believe you have a better understanding of "what is the method of Unity control inversion". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report