Before learning MVC, we need to know these knowledge points (automatic attributes, implicit type var, object initializer and set initializer, Anonymous class, extension method, Lambda expression ), if you do not know, please refer to my brief introduction below. If you already know, you can go through this blog and see the initial image of the project I created below, next we will give a brief introduction.
1. Automatic attributes
(1) Automatic attributes (Auto-Implemented Properties), C # automatic attributes can avoid manually declaring a private member variable and writing the get and set attributes.
Public class Kencery // declare a Kencery class
{
Public int ID {get; set;} // primary key ID
Public string Name {get; private set;} // read-only attribute Name
Public int Age {get; set;} // Age
}
2. Implicit var
(1) now more and more projects can see var. What does this mean? In fact, this is the hermit type var provided by C #3.0. The var keyword instructs the compiler to deduce the variable type based on the expression on the right of the initialization statement.
Var Kencery = "Good evening"; // defines the variable
Var list = new List <int> (); // defines a List set.
Var Kencery = new {ID = 1, Name = "HYL", age = 24} // type defined by object initialization
Var geners = from n in StoreContract. ProductCatalogs
Where n. IsSystem = false
Select n;
Return View (geners );
// Use of Linq, which will be used in a lot later. If you have never learned it, you can learn it by yourself.
(2) What are the disadvantages of var when it has such a large function?
1) You can use var only when declaring and initializing a local variable in the same statement. You Cannot initialize the variable as null, method group, or anonymous method.
2) var cannot be used in the class range.
3) variables declared by var cannot be used in the initialization expression. In other words, this expression is legal: int I = (I = 20 );, however, the following expression produces a compilation error: var I = (I = 20 ).
4) Multiple implicit variables cannot be initialized in the same statement.
5) if there is a type named var in the range, the var keyword will be resolved to this type name, instead of being processed as part of the implicit type local variable declaration.
3. Anonymous class
(1) encapsulate a set of read-only attributes into a single object without displaying and defining a type first. The type name is generated by the compiler and cannot be used at the source code level, the type of each attribute is inferred by the compiler. [Var]
(2) the expression used to initialize the attribute cannot be null, anonymous function, or pointer type.
Var Kencery = new {ID = 1, Name = "HYL", Age = 24}
4. Object initializer and set Initiator
(1) The object initializer uses the compiler to assign values to externally visible fields or attributes of the object in order, and calls the constructor in compilation or implicit mode, you can assign one or more values to a field or attribute.
List <Kencery> kencery = new List <Kencery> {// Kencery class
New Kencery {ID = 1, Name = "Hanyinglong", Age = 19 },
New Kencery {ID = 2, Name = "HYL "},
Null
};
Person p = new Person {ID = 1, Name = "HYL", Age = 19 };
5. Extension Method
(1) The extension method is a special static method, which is defined in a static class, but can be called in the same way as the instance method is called on other class objects. Therefore, with the extension method, we can expand the function of a type without modifying a type. This method does not generate a new type, instead, the function is extended by adding new methods to existing classes.
(2) When extending an existing class, we need to write all the extension methods in a static class, which is equivalent to storing the container of the extension method, all extension methods can be written here. The extension method is different from the declaration method of common methods. The first parameter of the extension method starts with the this keyword, followed by the extended type, and then is the real parameter list.
Public static return type Extension Method Name (this parameter name of the type to be extended [, extension method parameter list])
{
}
Public static int ToInt32 (this string s) // The extension method converts a string to an integer.
{
Return Int32.Parse (s );
}
6. Lambda expressions
(1) "Lambda expression" is an anonymous function that can contain expressions and statements and can be used to create delegates.
(2) operator =>, which is read as "goes ".
(3) Format: (input parameters) => expression
Delegate bool DelDemo (int a, int B); // defines the delegate
DelDemo Lambda = (int a, int B) => a> B // use a Lambda expression to indicate whether a is greater than B
Console. WriteLine (Lambda (1, 4 ));
7. Overall project framework Flowchart
(1) I 've talked so much about it. It's actually useless with our blog today. It's just some preparation work and a review of these knowledge points. Next we will create a new MVC4 program, how to create an MVC4 program, I believe everyone will be ready, and then build a framework and build a framework:
(2) then we design the database at the LYZJ. UserLimitMVC. Model layer and create an empty Edmx Model in it ,:
(3) We have created four class libraries and an MVC4.0 project. Let's take a closer look. If you are not clear about the role of these four class libraries, I suggest you go to the three-tier architecture or leave a message below, so you will not write it out now.