C # comprehensive secrets-segmentation and Division Methods

Source: Internet
Author: User

In the object-oriented "Closed encapsulation" development principle, we always advocate encapsulating independent functions in a class! However, from Visual Studio 2005 development, the system provides a branch-class development method that has been controversial. Many people think that the same class of functions are distributed in different files, the "encapsulation and closure" principle is broken, and the functions of a class become difficult to manage. Most people use the Division class method only when they are helpless. However, in the winFrom class, page class, And DataSet, you can often find the segment class. When you use the Entity Framework, you will find that the object generated by each ing is generated using the Division class method, and the Division class seems to have come in handy. What are the advantages of the Division class? We will reveal them one by one.
I. Division
According to Microsoft's definition, a branch class is "splitting the definition of a class or structure, interface or method into two or more source files. Each source file contains a part of the type or method definition. All parts are combined during compilation ". When using a partial class, you must add the partial keyword to the class. Note that the accessibility of each class must be consistent. One Class is public, and the other class must also be public. If one of the classes is an abstract class, the entire class will be treated as an abstract class after merging. If one of the classes is a sealed class, the entire class will be considered as a sealed class after merging.
1 public partial class PersonManager
2 {
3 public Person GetPersonById (int id)
4 {
5}
6}
7
8 public partial class PersonManager
9 {
10 public List <Person> GetList ()
11 {
12}
13}
When merging, the overall class inherits all the base classes and features. Note that the branch class must be in the same assembly, and the keywords cannot be rushed. If one class is public and the other class is private, an error will occur. In the second branch class, you can call the fields and methods defined in the first branch class.
1 [SerializableAttribute]
2 public partial class Person {}
3
4 [ObsoleteAttribute]
5 public partial class Person {}
6
7 // equivalent
8 [SerializableAttribute]
9 [ObsoleteAttribute]
10 class Person {}
11
12
13 partial class PersonManager: Manager {}
14
15 partial class PersonManager: Collection {}
16
17 // equivalent
18 class PersonManager: Manager, Collection {}
Ii. Division Method
The partial method is very similar to the partial class. The method must contain the partial keyword. If a class does not contain the implementation of this method, and another class implements this method, the normal operation of this method will not be affected. This feature is similar to the interface, especially when using code generators, developers can use tools to generate the division method and then manually implement the method. The partial method has the following restrictions. The first method must return void and can only be private by default. The second method cannot be virtual or extern. The third method can have the ref parameter, however, the out parameter is not allowed;
1 partial void PersonChanged (Person person );
2
3 partial void PersonChanged (Person person)
4 {
5 PersonManager personManager = new PersonManager ();
6 personManager. Update (person );
7 ......
8}
The Division class and division method are described in detail on Microsoft's official website. Next, I would like to introduce the actual use of the division class and division method. This is the true purpose of my writing this chapter.
Iii. Availability of division classes and Division Methods
LINQ is a new product developed by Microsoft in Framewrok3.0. Among them, linq to SQL is a magic tool for ing objects and data tables. With the release of Framework 4.0, Entity Framework has become the main means to implement ORM in Microsoft projects. In this process, *. edmx files use implementation methods of partial classes. This is because the ing process is automatically generated, and the Code must comply with custom rules. When you need to add some additional attributes to the object and these attributes do not need to be saved to the database, the partial class is useful. We can use the partial class to provide various custom attributes for objects.
Especially when using the DDD domain-driven design, the Division class becomes a good way to implement model actions. The blood loss model and the congestion model are controversial topics of DDD for a long time. In the blood loss model, the model should not have actions, but put the actions on the Service layer, while in the congestion model, model classes should have their own methods, while "segment classes" are a good way to achieve the congestion model method.
1 // Model. Designer. cs File
2 [global: System. Data. Objects. DataClasses. EdmEntityTypeAttribute (NamespaceName = "BusinessModel", Name = "Approve")]
3 [global: System. Runtime. Serialization. DataContractAttribute (IsReference = true)]
4 [global: System. Serializable ()]
5 public partial class Approve: global: System. Data. Objects. DataClasses. EntityObject
6 {
7 /// <summary>
8 // create a new Approve object.
9 /// </summary>
10 /// <param name = "id"> the initial value of ID. </Param>
11 /// <param name = "functionType"> the initial value of FunctionType. </Param>
12 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
13 public static Approve CreateApprove (int id, int functionType)
14 {
15 Approve approve = new Approve ();
16 approve. ID = id;
17 approve. FunctionType = functionType;
18 return approve;
19}
20 /// <summary>
21 // The schema does not contain any comments for the property ID.
22 /// </summary>
23 [global: System. Data. Objects. DataClasses. EdmScalarPropertyAttribute (EntityKeyProperty = true, IsNullable = false)]
24 [global: System. Runtime. Serialization. DataMemberAttribute ()]
25 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
26 public int ID
27 {
28 get
29 {
30 return this. _ ID;
31}
32 set
33 {
34 this. OnIDChanging (value );
35 this. ReportPropertyChanging ("ID ");
36 this. _ ID = global: System. Data. Objects. DataClasses. StructuralObject. SetValidValue (value );
37 this. ReportPropertyChanged ("ID ");
38 this. OnIDChanged ();
39}
40}
41 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
42 private int _ ID;
43 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
44 partial void OnIDChanging (int value );
45 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
46 partial void OnIDChanged ();
47 // <summary>
48 // no comments of the FunctionType property exist in the schema.
49 // </summary>
50 [global: System. Data. Objects. DataClasses. EdmScalarPropertyAttribute (IsNullable = false)]
51 [global: System. Runtime. Serialization. DataMemberAttribute ()]
52 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
53 public int FunctionType
54 {
55 get
56 {
57 return this. _ FunctionType;
58}
59 set
60 {
61 this. OnFunctionTypeChanging (value );
62 this. ReportPropertyChanging ("FunctionType ");
63 this. _ FunctionType = global: System. Data. Objects. DataClasses. StructuralObject. SetValidValue (value );
64 this. ReportPropertyChanged ("FunctionType ");
65 this. OnFunctionTypeChanged ();
66}
67}
68 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
69 private int _ FunctionType;
70 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
71 partial void OnFunctionTypeChanging (int value );
72 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
73 partial void OnFunctionTypeChanged ();
74 // <summary>
75 // The architecture does not contain any comments of the attribute Title.
76 // </summary>
77 [global: System. Data. Objects. DataClasses. EdmScalarPropertyAttribute ()]
78 [global: System. Runtime. Serialization. DataMemberAttribute ()]
79 [global: System. CodeDom. Compiler. GeneratedCode ("System. Data. Entity. Design. EntityClassGenerator", "4.0.0.0")]
80 public string Title
81 {
82 get
83 {
84 return this. _ Title;
85}
86 set
87 {
88 this. OnTitleChanging (value );
89 this. ReportPropertyChanging ("Title ");
90 this. _ Title = global: System. Data. Objects. DataClasses. StructuralObject. SetValidValue (value, true );
91 this. ReportPropertyChanged ("Title ");
92 this. OnTitleChanged ();
93}
94}
95 ...............................
96}
97
98 // segment class
99 public partial class Approve
100 {
101 // Add attributes
102 public string Type
103 {
104 get; set;
105}
106
107 // Add action
108 public void AddReport (Report report)
109 {.......}
110 .................
111}
When developing the Entity Framework Model Using the Division class, you will notice the Division class. The purpose of this article is to introduce the role of the Division class in the development of the Entity Framework. Please comment.


Author: "dust and waves"

Related Article

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.