Improved
Knowing the "Stink" of the program, how do we improve it? Think about hiding the entity through an interface. We use interface implementation, create partial class for customer, create Icustomer interface, customer implements Icustomer interface, write Customerfacade with Icustomer interface, Update unit Tests against the new Icustomer interface instead of unit testing the customer object in the previous section so that the customer does not know that the data access layer is specifically implemented. This is the work we do in this section.
Data Access Layer
To further improve our program, look at the class diagram design of the data access layer:
1. New Icustomer interface
Icustomer { getset;} getset; } getset;}}
2. New partial class customer implementation Icustomer interface
Icustomer {}
I'm here. This partial class is implemented as NULL, in fact, the customer class (data Access object) in the data Access object DataContext implements this interface.
3. Modify the data access appearance base class
Add a method to implement iqueryable<t> conversion to ilist<tinterface> interface
protectedIList<TInterface> Converttolist<tinterface, Tconcrete> (IEnumerable<TConcrete> convertthis)wheretconcrete:tinterface{if(Convertthis = =NULL) {return newList<TInterface> (); }Else{IList<TInterface> returnvalue =NewList<TInterface> ();foreach(Tconcrete IteminchConvertthis) {Returnvalue.add (item); }returnreturnvalue; }}4. Modify the data access appearance Customerfacade
This class encapsulates the specific CRUD implementation of the Customer object, and we need to modify it to implement the interface.
STEP1: New Temporary Customer object
CreateCustomer () { Customer();}
STEP2: Press CustomerID To get the Customer object
public icustomer Getcustomerbyid (int customerId) {return (from P in datacontext.customer where p.customerid = = CustomerId select p). FirstOrDefault ();}
Step3: Get a list of customer objects
public IList <icustomer > Getcustomerlist () {iqueryable <customer > results = from P in datacontext.customer sele CT p; return Converttolist<icustomer , Customer > (results);}
STEP4: Update save Customer object
public void UpdateCustomer ( icustomer customer) {customer Tempcustomer = Customer as customer ; if (Tempcustomer.customerid = = 0) {DataContext.Customer.InsertOnSubmit (Tempcustomer); } datacontext.submitchanges ();}
The data access layer has been modified.
Unit Test Layer
As long as the corresponding data access object is swapped for the data Access object interface, you can invert the dependency to completely remove the dependency on LINQ to SQL.
STEP1: Modify create and Save the customer method:
private icustomer createandsavenewcustomer (string firstName, string lastName) { icustomer newcustomer = Facade.createcustomer (); Newcustomer.firstname = FirstName; Newcustomer.lastname = LastName; Facade.updatecustomer (Newcustomer); return newcustomer;}
Step2: Modify Test UpdateCustomer () method:
[test ] public void Updatecustomertest () { icustomer Newcustomer = Createandsavenewcustomer (" yjing ", Lee" ); assert . Arenotequal (0, Newcustomer.customerid); assert . AreEqual ( "yjing" , Newcustomer.firstname);}
STEP3: Modify the Getcustomerbyidtest () method:
[Test]Public voidGetcustomerbyidtest () {IcustomerTempcustomer = Createandsavenewcustomer ("Yjing","Lee");Assert. Arenotequal (0, Tempcustomer.customerid);IcustomerReloaded = Facade.getcustomerbyid (Tempcustomer.customerid);Assert. Isnotnull (Reloaded);Assert. AreEqual (Tempcustomer.customerid, Reloaded. CUSTOMERID);Assert. Aresame (Tempcustomer, Reloaded);}
This article publishes the results of this test method:
STEP4: Modify the Get customer list method
[Test]Public voidGetlisttest () {List<Icustomer> tempcustomers =NewList<Icustomer> (); Tempcustomers.add (Createandsavenewcustomer ("Yjing","Lee")); Tempcustomers.add (Createandsavenewcustomer ("Li","Yongjing")); Tempcustomers.add (Createandsavenewcustomer ("Cnblogs","com"));varReloaded = Facade.getcustomerlist ();Assert. Isnotnull (Reloaded);Assert. AreEqual (Tempcustomers.count, Reloaded. Count);}Conclusion
Through this modification, the "stink" of our program is basically gone, and the dependency is shifted from the original LINQ to SQL dependency to the interface, and the customer uses the interface directly. Let's start with the next chapter and see what's Good in LINQ to SQL!
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
LINQ to SQL Ingenious (3): Sniff out "stink" disappears