Funny LINQ Part2: Create a generic operation class for the LINQ database (below)
The previous article introduced how to implement a general insert operation, that is, directly using utility. insert (XXX. Then, you may naturally think about whether you can implement General update, delete, and query operations. The answer is yes!
Next let's extend the Dal mentioned in the previous article:
- Using system;
- Using system. Collections. Generic;
- Using system. LINQ;
- Using system. text;
- Namespace dal
- {
- Public class Utility
- {
- Public static void insert <t> (T tentity) where T: Class
- {
- VaR table = tablefactory. createtable <t> ();
- Table. insertonsubmit (tentity );
- }
- Public static void Delete <t> (T tentity) where T: Class
- {
- VaR table = tablefactory. createtable <t> ();
- Table. deleteonsubmit (tentity );
- }
- Public static void Delete <t> (func <t, bool> predicate) where T: Class
- {
- VaR table = tablefactory. createtable <t> ();
- VaR dresult = where <t> (predicate );
- Table. deleteallonsubmit (dresult );
- }
- Public static void update <t> (T tentity, Action <t> action)
- {
- Action (tentity );
- Submitchanges ();
- }
- Public static void insertall <t> (ienumerable <t> tentities) where T: Class
- {
- VaR table = tablefactory. createtable <t> ();
- Table. insertallonsubmit (tentities );
- }
- Public static void deleteall <t> (ienumerable <t> tentities) where T: Class
- {
- VaR table = tablefactory. createtable <t> ();
- Table. deleteallonsubmit (tentities );
- }
- Public static ienumerable <t> selectall <t> () where T: Class
- {
- VaR table = tablefactory. createtable <t> ();
- Return table. Select (C => C). asenumerable ();
- }
- Public static ienumerable <t> where <t> (func <t, bool> predicate) where T: Class
- {
- VaR table = tablefactory. createtable <t> ();
- Return table. Where (predicate). asenumerable ();
- }
- Public static void submitchanges ()
- {
- Database. nwdb. submitchanges ();
- }
- }
- }
Similarly, we also write some test methods to verify if they are correct.
- Using system;
- Using system. Collections. Generic;
- Using system. LINQ;
- Using system. text;
- Using Dal;
- Using dlinq;
- Using toolkits;
- Namespace daltest
- {
- Class Program
- {
- Static void main (string [] ARGs)
- {
- // Selectalltest ();
- Inserttest ();
- Wheretest ();
- Updatetest ();
- Wheretest ();
- Deletetest1 ();
- Wheretest ();
- Console. writeline ("All testings are success! ");
- Console. Read ();
- }
- Private Static void inserttest ()
- {
- Customer _ customer = new customer {
- Customerid = "Bruce ",
- Contactname = "Lee ",
- CompanyName = "codingsky ",
- City = "Shenzhen "};
- Utility. insert (_ customer );
- Utility. submitchanges ();
- }
- Private Static void deletetest1 ()
- {
- Utility. Delete <customer> (C => C. customerid = "Bruce ");
- Utility. submitchanges ();
- }
- Private Static void deletetest2 ()
- {
- VaR _ result = utility. Where <customer> (C => C. customerid = "Bruce ");
- If (_ result. Count ()> 0)
- {
- Utility. Delete (_ result. First ());
- Utility. submitchanges ();
- }
- }
- Private Static void updatetest ()
- {
- VaR _ result = utility. Where <customer> (C => C. customerid = "Bruce ");
- If (_ result. Count ()> 0)
- {
- VaR _ customer = _ result. First ();
- If (_ customer! = NULL)
- {
- Utility. Update (_ customer, c =>
- {
- C. contactname = "Jack ";
- C. companyName = "Microsoft ";
- C. City = "Beijing ";
- });
- Utility. submitchanges ();
- }
- }
- }
- Private Static void selectalltest ()
- {
- VaR _ result = utility. selectall <customer> ();
- VaR _ list = _ result. tolist ();
- If (_ list. Count = 0)
- {
- Console. writeline ("no result! ");
- Return;
- }
- _ List. foreach (C => console. writeline (stringextension. tostring (c )));
- }
- Private Static void wheretest ()
- {
- VaR _ result = utility. Where <customer> (C => C. customerid = "Bruce ");
- VaR _ list = _ result. tolist ();
- If (_ list. Count = 0)
- {
- Console. writeline ("no result! ");
- Return;
- }
- Console. writeline ("query result is :");
- _ List. foreach (C => console. writeline (stringextension. tostring (c )));
- }
- }
- }
For other Code such as tablefactory, stringextension, see previous: http://blog.csdn.net/fengart/archive/2008/08/19/2798534.aspx
In the preceding test, insert a guy named Bruce in the MERs table, change his contactname to Jack, and delete him.
(The customers table contains too much data, so I commented out the selectalltest method)
The final output is as follows:
Query result is:
Class Name: customer
Customerid: Bruce (string ),
CompanyName: codingsky (string ),
Contactname: Lee (string ),
Contacttitle: (string ),
Address: (string ),
City: Shenzhen (string ),
Region: (string ),
Postalcode: (string ),
Country: (string ),
Phone: (string ),
Fax: (string ),
Orders: system. Data. LINQ. entityset '1 [dlinq. Order] (entityset '1)
Query result is:
Class Name: customer
Customerid: Bruce (string ),
CompanyName: Microsoft (string ),
Contactname: Jack (string ),
Contacttitle: (string ),
Address: (string ),
City: Beijing (string ),
Region: (string ),
Postalcode: (string ),
Country: (string ),
Phone: (string ),
Fax: (string ),
Orders: system. Data. LINQ. entityset '1 [dlinq. Order] (entityset '1)
No result!
All testings are success!
If you have any better suggestions, please feel free to contact us! I will be very happy to hear it.