Overview
We have been familiar with the entity layer and used it in subsequent projects when learning the three-layer architecture. We also know that, objects or object sets are transmitted between layers. how do you build your own object sets? It refers to an array, a set provided by the system, or a set created for each object (each set is a set of Code), or other methods.
In this blog, I will explain two methods to you through examples. Both methods are to create their own entity collection classes, but these two methods are also very different.
Entity collection classes with poor reusability
This method creates an object collection class for each object class, and the functions in each object collection class are the same. So what are the differences between each object collection class? Their difference is that their specific service object types are fixed. The following is an example.
Entity class
Using System; using System. collections. generic; using System. linq; using System. text; using set. set. ORMapping with poor flexibility; namespace TableToCollection. entity {public class Person {public string strID {get; set;} public string strName {get; set;} public int intAge {get; set ;}}}
Entity collection class
Using System; using System. collections. generic; using System. linq; using System. text; using System. collections; using TableToCollection. entity; namespace TableToCollection. set. set of poor flexibility {public class PersonCollection_bad: IEnumerable {private ArrayList list; public int Count {get {return list. count ;}# region constructor public PersonCollection_bad () {list = new ArrayList () ;}# endregion # region Add public void Add (Person p) {this. list. add (p);} public void AddAr (Person [] ps) {this. list. addRange (ps) ;}# endregion # region Delete public void Remove (Person p) {this. list. remove (p);} public void RemoveAt (int index) {this. list. removeAt (index);} public void RemoveRange (int index, int count) {this. list. removeRange (index, count);} public void Clear () {this. list. clear () ;}# endregion # region custom indexer public Object this [int index] {get {return list [index];} set {list [index] = value ;}# endregion # region supports the foreach statement to traverse public IEnumerator GetEnumerator () {return list. getEnumerator () ;}# endregion }}
Entity collection class with good reusability
We solve the disadvantages of the entity collection class above through generics, that is, the post-type binding method. The specific idea is to create a basic entity collection class, and then inherit the specific collection class, and specify the corresponding type, so as to achieve the code reuse number and program security number. The object class uses the above Entity class.
Basic entity collection class
Using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace set. Set. Set with good flexibility {public class BaseCollection
: IEnumerable
, IEnumerator
, IList
{Private List
List; // define the index private int position; public int Count {get {if (list! = Null) {return list. Count;} return-1 ;}}///
/// Determine the index of a specific object in the Set //////
Specific Object///
If it is found in the list, it is the index of the item; otherwise it is-1
Public int IndexOf (T TObject) {return this. list. IndexOf (TObject);} # region constructor public BaseCollection () {this. list = new List
(); This. position =-1 ;}# endregion # Add a region ///
/// Add the object to the end of the Set //////
Object To be added to the end of the SetPublic void Add (T TObject) {this. list. Add (TObject );}///
/// Add the elements in the specified set to the end of the set //////
Set object to be added to the end of the SetPublic void AddRange (ICollection
TCollection) {this. list. AddRange (TCollection) ;}# endregion # Delete region ///
/// Remove the first matching item of a specific object from the set //////
TObject///
True: removed successfully; false: failed to remove
Public bool Remove (T TObject) {return this. list. Remove (TObject );}///
/// Remove the object at the specified index from the set //////
Index value, starting from 0Public void RemoveAt (int index) {this. list. RemoveAt (index );}///
/// Delete all objects from the set ///Public void Clear () {this. list. Clear () ;}# endregion # insert region ///
/// Insert an object in a specific position of the Set //////
Index, starting from 0///
Inserted objectPublic void Insert (int index, T TObject) {this. list. Insert (index, TObject);} # endregion # region value ///
/// Get or set the element at the specified index //////
Index value starts from 0///
TObject
Public T this [int index] {get {return this. list [index];} set {this. list [index] = value ;}# endregion # region implements ICollection
Because IList
Inherit ICollection
// Obtain a value indicating System. Collections. Generic. ICollection
Read-only or not. Public bool IsReadOnly {get {return true ;}// confirm System. Collections. Generic. ICollection
Whether the specified value is included. Public bool Contains (T item) {return this. list. Contains (item);} // start from the specified System. Array index and set System. Collections. Generic. ICollection
To a public void CopyTo (T [] array, int arrayIndex) {this. list. copyTo (array, arrayIndex);} # endregion # region supports foreach statement traversal # region implements IEnumerable
Interface System. Collections. Generic. IEnumerator
System. Collections. Generic. IEnumerable
. GetEnumerator () {return (System. Collections. Generic. IEnumerator
) This ;}# region implements the IEnumerable interface, because IEnumerable
Inherit from IEnumerable System. Collections. IEnumerator System. Collections. IEnumerable. GetEnumerator () {return (System. Collections. IEnumerator) this ;}# endregion # region implements IEnumerator
Interface // obtain the current element in the set. T System. Collections. Generic. IEnumerator
. Current {get {try {return this. list [position];} catch (IndexOutOfRangeException) {throw new InvalidOperationException () ;}}# region implements the IEnumerator interface because IEnumerator
Inherit from IEnumerator // get the current element in the set. Object System. collections. IEnumerator. current {get {try {return this. list [position];} catch (IndexOutOfRangeException) {throw new InvalidOperationException () ;}}// pushes the number of enumerations to the next element of the set. Bool System. collections. IEnumerator. moveNext () {this. position ++; return (this. position <this. list. count);} // set the number of enumerations to its initial position before the first element in the set. Void System. Collections. IEnumerator. Reset () {this. position =-1 ;}# endregion # region implements the IDisposable interface because IEnumerator
Inherit from IDisposable public void Dispose () {this. position =-1 ;}# endregion # endregion }}
Object collection class
Using System; using System. Collections. Generic; using System. Linq; using System. Text; using TableToCollection. entity; namespace set. Set. Flexible set {public class PersonBaseCollection: BaseCollection
{}}
Client code
Using System; using System. collections. generic; using System. linq; using System. text; using TableToCollection. entity; using TableToCollection. set. set with poor flexibility; set with using. set. set with good flexibility; set with using. ORMapping with poor flexibility. ORMapping; namespace collection {class Program {static void Main (string [] args) {# sets with poor region flexibility // PersonCollection_bad personCollection = new PersonCollection_bad (); //// add element to the set // for (int I = 0; I <10; I ++) // {// Person p = new Person () {strID = I. toString (), strName = "zhang", intAge = I}; // personCollection. add (p); // Console. writeLine ("number of elements:" + personCollection. count); // output set // Console. writeLine ("use foreach output"); // foreach (var item in personCollection) // {// Console. writeLine (Person) item ). strID + (Person) item ). strName + (Person) item ). intAge); //} // Console. writeLine ("use for output"); // for (int I = 0; I <personCollection. count; I ++) // {// Console. writeLine (Person) personCollection [I]). strID + (Person) personCollection [I]). strName + (Person) personCollection [I]). intAge); //} // # endregion # Set PersonBaseCollection personBaseCollection = new PersonBaseCollection (); DoAdd (personBaseCollection); DoForPrint (personBaseCollection); DoInsert (0, personBaseCollection); DoFo REachPrint (personBaseCollection); personBaseCollection. removeAt (1); DoForEachPrint (personBaseCollection); personBaseCollection. clear (); DoForEachPrint (personBaseCollection); # endregion Console. readKey ();} static void DoAdd (PersonBaseCollection personBaseCollection) {personBaseCollection. add (new Person () {strID = "1", strName = "qs1 (Add)", intAge = 21}); Person [] persons = {new Person () {strI D = "2", strName = "qs2 (AddRange)", intAge = 22}, new Person () {strID = "3", strName = "qs3 (AddRange )", intAge = 23 }}; personBaseCollection. addRange (persons);} static void DoInsert (int index, PersonBaseCollection personBaseCollection) {personBaseCollection. insert (index, new Person () {strID = "4", strName = "qs4 (Insert)", intAge = 24});} static void DoForPrint (PersonBaseCollection personBa SeCollection) {Console. WriteLine ("For statement output:"); if (personBaseCollection. Count = 0) {Console. WriteLine ("no element in the set! "); Return;} for (int I = 0; I <personBaseCollection. count; I ++) {Console. writeLine (personBaseCollection [I]. strID + "" + personBaseCollection [I]. strName + "" + personBaseCollection [I]. intAge) ;}} static void DoForEachPrint (PersonBaseCollection personBaseCollection) {Console. writeLine ("ForEach statement output:"); if (personBaseCollection. count = 0) {Console. writeLine ("there are no elements in the set! "); Return;} foreach (var item in personBaseCollection) {Console. writeLine (item. strID + "" + item. strName + "" + item. intAge );}}}}
Summary
There is another piece of content mentioned above, that is, the iterator design mode. In fact, the above program has already been used, but the corresponding interface is defined by Microsoft, why have Microsoft defined it? Because Microsoft also needs to use (specific collection classes provided by. net framework ).