The lambda expression, dependency inversion of the net MVC pre-school article

Source: Internet
Author: User

The lambda expression, dependency inversion of the net MVC pre-school article

Preface with the reading of the previous article, some friends may have doubts, such as (A.method (XXX=>XX>YY);) similar to such a function call statement, inside the Xxx=>xx>yy these exactly how to use?

The implementation of the dependency inversion principle will be briefly explained at the end of this space. This article does not have the core theme, if the definition is to be mandatory is the content is the basic knowledge, is to follow up the MVC framework to pave the way.

1 Lambda

Lambda expressions are common in everyday development, and using lambda expressions is a free way to define the body of a function and to streamline the amount of code, what is a lambda expression?

A lambda expression is an anonymous function, and an anonymous function is a delegate, so a lambda expression is a delegate. (It's a little bit different when compiled into an intermediate language, but roughly the same)

Definition of 1.1 lambda

Since Lambda is a delegate, there is a type, and here we are using. NET provides us with a fun<t> generic delegate,fun<t> is a delegate type with a return value.

1.2 Mutual understanding

Sample Code 1.1-1

1         Private BOOLComparison (intNUM1,intnum2)2         {3             if(Num1 >num2)4             {5                 return true;6             }7             Else8             {9                 return false;Ten             } One}
1 func<intintboolnew func<intintBOOL >(Comparison); 2 Comparison (53); // return True

In the example above, it is very simple to define a delegate of the type func<int,int,bool>, comparisonnum means to have two parameters of type int and return a function with a value of type bool. This is the most primitive version, and the following shows how to go over to lambda expressions.

Sample Code 1.1-2

1func<int,int,BOOL> comparisonnum=2                 Delegate(intNUM1,intnum2)3                 {4                     if(Num1 >num2)5                     {6                         return true;7                     }8                     Else9                     {Ten                         return false; One                     } A};
1 comparisonnum (35); // 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, except that anonymous delegation is a little easier. Let's look at an example of using a lambda expression below.

Sample Code 1.1-3

1  func<intintboolreturn num1 > num2; };

The "(num1,num2)" On the left side of the 1.1-3 code is the parameter to be used, as defined by func<int, int, bool>, and should actually be written as
Sample Code 1.1-4

1 func<intintbool> comparisonnum = (int num1,int  return num1 > num2; };

1.1-3 uses a simple notation, because with the support of a powerful environment like VS, it can be automatically set to the lambda expression parameter type based on the delegate type defined by the preceding variable, in order to conform to the definition of the preceding type, and the right side of the = is the function body of the lambda expression. With the anonymous delegate a truth. This section is a very simple example of lambda, intended to give the reader a basic understanding of the content of this aspect, the reason for the space is not much to say.

2. Dependency Inversion principle

Design principles are followed in the design pattern or in the framework design. This section explains one of the dependencies of the dependency inversion principle in the implementation of dependency injection.

In the work of learning, oriented to abstract programming, dependent on the abstract is not dependent on specific these words are often seen, the examples in this section are related to these concepts, a simple example to let everyone have an understanding.

1  Public class Entity 2 {3 }

Here we define an entity class, just as a demonstration, no specific function,

1      Public class objectfactory 2     {3public           Entity CreateObject ()4        { 5             return New Entity (); 6         }7     }

It then defines a factory to use as an instance of the entity type.

1      Public classIoccontroller2     {3          Public StaticEntity getentity ()4         {5Objectfactory entityfactory =Newobjectfactory ();6             returnEntityfactory.createobject ();7         }8}

This is a controller where the client obtains the only dependency on the entity type, and only ioccontroller.getentity () on the client, so that the entity instance is obtained. And what we're going to do is to reduce the coupling between Ioccontroller and Objectfactory, and the rest doesn't have to be tubes.

1

This time the dependency is as shown in this way, it relies on abstraction to decouple.

1      Public Interfaceiobjectfactory2     {3 Entity CreateObject ();4     }5      Public classobjectfactory:iobjectfactory6     {7          PublicEntity CreateObject ()8         {9             return NewEntity ();Ten         } One}

Yes, the objectfactory type is abstracted, with the Iobjectfactory interface type.

Figure 2

This time in the mind of the figure is not supposed to be 2, the picture is really good, but the reality is not so.

1      Public classIoccontroller2     {3          Public StaticEntity getentity ()4         {5Iobjectfactory entityfactory =Newobjectfactory ();6             returnEntityfactory.createobject ();7         }8}

This time the dependency should be as shown in Figure 3,

Figure 3

The feeling is not very bad, it doesn't matter, a little modification, so that rely on a reasonable injection can be done decoupling,

1      Public classIoccontroller2     {3         Private Staticiobjectfactory objectfactory;4 5          Public Static voidsetobjectfactory (iobjectfactory objectfactory)6         {7objectfactory=objectfactory;8         }9 Ten          Public StaticEntity getentity () One         { A             returnObjectfactory.createobject (); -         } -}

This time the relationship depends on the graph as shown in Figure 2. The point here is that the private static and static functions in Ioccontroller can be converted to instances, a static Ioccontroller type is defined in Ioccontroller to implement one of its own singleton patterns, and then an instance method is invoked. Because it is an example, focusing on the interpretation of dependency injection is not perfect this type, in the language described, the idea of such a design is similar to the Controllerbuilder in Asp.netmvc, that is, constructor injection. The actual application is not like this, will be explained in the next article. This is the story.

Jinyuan

Source: http://www.cnblogs.com/jin-yuan/

This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.