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

How to understand Lambda expressions and dependency inversion

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to understand Lambda expressions and dependency inversion, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Lambda expressions, dependency inversion

The implementation of the principle of dependency inversion will also be roughly explained at the end. There is no core theme, and if there is a mandatory definition, it is that these contents are basic knowledge, laying the groundwork for the subsequent study of the MVC framework.

1 Lambda

Lambda expressions are very common in daily development, using Lambda expressions can freely define the function body and reduce the amount of code, so what is the Lambda expression?

Lambda expressions are anonymous functions, and anonymous functions are delegates, so lambda expressions are delegates. (there is a slight difference when compiled into an intermediate language, but it is roughly the same.)

1.1 definition of Lambda

Now that Lambda is a delegate, there is a type. Here we use the Fun generic delegate provided to us by .NET, and Fun is a delegate type with a return value.

1.2 know each other

Sample code 1.1-1

1 private bool Comparison (int num1, int num2) 2 {3 if (num1 > num2) 4 {5 return true; 6} 7 else 8 {9 return false;10} 11} 1 Func ComparisonNum = new Func (Comparison); 2 Comparison (5,3); / / return true

In the above example, it is obvious that a delegate of type Func is simply defined. ComparisonNum means a function with two parameters of type int and a return value of type bool. This is the most original version, and the following will demonstrate how to transition to Lambda expressions.

Sample code 1.1-2

1 Func ComparisonNum= 2 delegate (int num1,int num2) 3 {4 if (num1 > num2) 5 {6 return true; 7} 8 else 9 {10 return false 11} 12}; 1 ComparisonNum (3,5); / / return false

As you can see from code 1.1-2, there is no big difference between using anonymous delegates and the 1.1-1 above, but anonymous delegates are a little easier. Let's take a look at an example of using Lambda expressions.

Sample code 1.1-3

1 Func ComparisonNum = (num1, num2) = > {return num1 > num2;}

The "(num1,num2)" on the left side of the code 1. 1-3 is the parameter to be used, defined according to Func, and should actually be written as

Sample code 1.1-4

1 Func ComparisonNum = (int num1,int num2) = > {return num1 > num2;}

Because of the support of such a powerful environment as VS, you can automatically set the delegate type defined by the previous variable to the Lambda expression parameter type in order to meet the definition of the previous type, while on the right side of = > is the function body of the Lambda expression, which is the same as anonymous delegation. This section is only a very simple example of Lambda, intended to let the reader basic understanding of this aspect of the content, the reason for the length is not said.

two。 Principle of dependency inversion

Design principles are followed in both design patterns and framework design. This section covers dependency injection, one of the implementations of the dependency inversion principle.

In learning at work, it is common to see abstract-oriented programming, relying on abstraction and not relying on concrete. The examples in this section all involve these concepts. A very simple example gives you an understanding.

1 public class Entity2 {3}

An entity class is defined here, which is only used as a demonstration and has no specific functions.

1 public class ObjectFactory2 {3 public Entity CreateObject () 4 {5 return new Entity (); 6} 7}

Then define a factory that is used to get an instance of the Entity type

1 public class IocController2 {3 public static Entity GetEntity () 4 {5 ObjectFactory entityFactory = new ObjectFactory (); 6 return entityFactory.CreateObject (); 7} 8}

This is a controller, and the client gets the only dependency of the Entity type, and only IocController.GetEntity () on the client; this gets the Entity instance. What we need to do is to reduce the coupling between IocController and ObjectFactory, and we don't need to control the rest.

Figure 1

At this time, the dependency is as shown in the figure above, so it is necessary to rely on the abstraction to decouple.

1 public interface IObjectFactory 2 {3 Entity CreateObject (); 4} 5 public class ObjectFactory:IObjectFactory 6 {7 public Entity CreateObject () 8 {9 return new Entity (); 10} 11}

Yes, the ObjectFactory type is abstracted, with the IObjectFactory interface type.

Figure 2

Whether the image in mind at this time should be as shown in figure 2, the imagination is really beautiful, but the reality is not.

1 public class IocController2 {3 public static Entity GetEntity () 4 {5 IObjectFactory entityFactory = new ObjectFactory (); 6 return entityFactory.CreateObject (); 7} 8}

At this time, the dependency should be like figure 3.

Figure 3

Whether it feels bad or not, it doesn't matter. With a little modification, the decoupling can be completed by relying on reasonable injection.

1 public class IocController 2 {3 private static IObjectFactory objectFactory; 4 5 public static void SetObjectFactory (IObjectFactory objectfactory) 6 {7 objectFactory=objectfactory; 8} 9 10 public static Entity GetEntity () 11 {12 return objectFactory.CreateObject (); 13} 14}

The relational dependency graph at this time is like that shown in figure 2. What I want to say here is that both private static fields and static functions in IocController can be converted to instances, define a static IocController type in IocController to implement its own singleton pattern, and then call the instance method. Because it is an example, the interpretation focusing on dependency injection is not perfect, so to describe it in language, the idea of such a design is similar to what ControllerBuilder looks like in ASP.NETMVC, that is, constructor injection.

After reading the above, have you mastered how to understand Lambda expressions and dependency inversion? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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