LINQ to SQL Ingenious (2): Hide, don't let me see.

Source: Internet
Author: User
Tags assert

Improved

This takes into account the appearance pattern (facade) in GoF23, which provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes the subsystem easier to use.

Consider establishing the appearance facade in the data access layer, which provides a simple class for complex data access methods, resulting in a much lower coupling. Increasing the appearance of facade can provide only a simple interface to the customer, reducing the dependence between the customer and the data access layer, easier to maintain and expand.

Data Access Layer

Let's use this method to improve the first piece of the program. A base class is first created to hold public methods, and then each data Access object (here only customer) inherits this base class to implement specifics.

1.DataAccessFacadeBase base class

Using a common base class for storing public methods, we create a new DataAccessFacadeBase.cs class, instantiate the data Access entity DataContext, configure DataContext connections and logs.

This instantiates the data Access entity DataContext Code the same as the code for the test base class, which is not posted here.

2. Data Access facade objects

We define a data access facade object for each data access object that encapsulates operations such as curd, without the load (), Save (), Delete () method in the specific data access class. The advantage of this is that the customer uses the direct call to the data access appearance facade in the external method, more based on the service, the calling code does not need to worry about the specific implementation details, but also can be easily tested.

Here, the new CustomerFacade.cs class is used to encapsulate the curd operation on the customer, which inherits from the Dataaccessfacadebase base class, in the following ways:

1. New temporary Customer object

CreateCustomer () {    Customer();}

2. Press CustomerID to get the Customer object

public   Customer  Getcustomerbyid (int  customerId) {return /span> (from  P in  datacontext.customer where  p.customerid = = CustomerId select  p). FirstOrDefault ();} 

3. Get a list of customer objects

IList<Customer> Getcustomerlist () {    (Datacontext.customer         p). tolist<Customer> ();}

4. Update Save Customer Object

Public voidUpdateCustomer (CustomerCustomer) {if(Customer = =NULL)    {throw NewArgumentNullException("Customer","Object is empty"); }Else{if(Customer.        CustomerId = = 0) {DataContext.Customer.InsertOnSubmit (Customer);    } datacontext.submitchanges (); }}

Here to demonstrate, just write out 4 methods, you can follow your own needs to add a few actions.

Unit Test Layer

You can test the results of our modifications above, and the new CustomerFacadeFixture.cs class in the unit test layer still inherits the test base class Unittestbase.

STEP1: Instantiating Customerfacade

In this test class, first instantiate the Customerfacade.

private   Customerfacade  M_facade; public  customerfacade  facade{get  {if  (M_facade = = null ) {m_facade = new  customerfacade  ();    } return  M_facade; }}

STEP2: Writing a Save Update method

Next, write a method to create and save the customer, because the database is empty before each test, and we need to enter some raw data before testing.

private   Customer  Createandsavenewcustomer (string  firstName, string  lastName) {customer  Newcustomer    = Facade.createcustomer ();    Newcustomer.firstname = FirstName;    Newcustomer.lastname = LastName;    Facade.updatecustomer (Newcustomer); return  newcustomer;} 

From this method, we directly use the Create () method and the update () method provided by facade to the client, and we do not know the specifics of the implementation at all.

STEP3: Test UpdateCustomer () method

Call the method above to test the initial data for creating a customer saved as Yjinglee, which is exactly the test of the Save data method.

[test ] public void  Updatecustomertest () {customer  Newcustomer = Createandsavenewcustomer ( "yjing" ,     "Lee" ); assert .    Arenotequal (0, Newcustomer.customerid); assert . AreEqual ( "yjing" , Newcustomer.firstname);} 

Let's see the results:

Analysis: First verify that the database exists, here, delete the original database to recreate a new database schema, insert a Yjinglee data into the database and query this data.

STEP4: Test Getcustomerbyid () method

Again to test the Getcustomerbyid () method, first insert a Yjinglee data in the database, see this sentence reloaded = Facade.getcustomerbyid (Tempcustomer.customerid) Call the Getcustomerbyid () method in the appearance facade to get the customer object by CustomerID, which shows the details of the implementation hidden from the outside, and finally asserts whether the data meets the expected results.

[Test]Public voidGetcustomerbyidtest () {CustomerTempcustomer = Createandsavenewcustomer ("Yjing","Lee");Assert. Arenotequal (0, Tempcustomer.customerid);CustomerReloaded = Facade.getcustomerbyid (Tempcustomer.customerid);Assert. Isnotnull (Reloaded);Assert. AreEqual (Tempcustomer.customerid, Reloaded. CUSTOMERID);Assert. Aresame (Tempcustomer, Reloaded);}

This test will be left to everyone to test! Test it out and tell me the result, please.

STEP5: Test Getcustomerlist () method

First initialize three data, then call the Getcustomerlist () method in the appearance facade to get the customer list, test consistency

[Test]Public voidGetlisttest () {List<Customer> tempcustomers =NewList<Customer> (); 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

This article we modify the first completely bare code, the use of an appearance of the facade class to provide a clearer interface to hide the specific implementation details, customer use only and facade object interface interaction, from this improvement also perfectly embodies the dependence of the reverse principle and Dimitri rule of thought.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

LINQ to SQL Ingenious (2): Hide, don't let me see.

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.