Home  /  Questions  /  Question

56   94.9
Sep 01, 2012

What is the purpose of the Take Method in the Patterns In action ADO.NET data access layer?

Hello, I've just recently purchased the design patterns framework.  So far it's looking really good.  I'm only just starting to look around it.  

I'm reading the Getting Started 4.0 PDF at the moment. I got to "Step 3: Detail Level" where I started looking at the Data Objects project.  so far most of it makes sense but I noticed a method named Take() that I cannot find any use. The code attached is from the SqlServerProductDao.cs file.

The method is simple but I was just curious if anyone can tell me why it is there?  The same method is used in all of the ADO.NET DAO classes and the same name is used in DynamicLinq.cs (but that appears to be calling http://msdn.microsoft.com/en-us/library/bb503062.aspx which is unrelated to the Take method I'm asking about).

I tried debugging with a breakpoint on this method and it never gets called and as I said before I cannot find it used in the source code.  Maybe it was an idea that never got completed?

70   96.1
Sep 02, 2012
Hi Steve,

The purpose of the Take() method is to convert the business object into an array with contains the SQL parameter names and values. This is used in combination with Insert and Update queries in the Data Objects project. 

An example on where this is used can be found in the SqlServerCustomerDao class. On line 90 the Db.Insert method is called with on of the parameters the return value of the Take() method. 

The implementation of the Take() method is shown on line 157. Using the Take() method this way reduces repeated code and makes the function easier to read.

Hope this helps.

Regards, Maurits 
 1 comment
Thanks, it was a silly mistake, I'd somehow missed looking at the other DAO classes. It just happened I had looked at one that didn't have any inserts. I'm guessing they left it in for consistency. --- Steve Martin  Sep 02, 2012

50   50
Sep 17, 2012

I too had this same question but was able to figure out what was going on...sort of...

I am actually in the process of implementing this functionality in an application I am working on.

There is a problem though...

If you do not plan to use all of the variables in the SQL statement (such as the below delete command) you will get an error.

Am I missing something?  Didn't anyone test this out?
Below only CustomerID and Version are needed in the SQL.

However, when you use the "Take" functionality you are applying CompanyName, City, and Country to the SQL text event though they are not needed.  This creates an error...

You are actually loading up parameters you are not going to use:

In Oracle, if you attempt to execute this you get:

"ORA-01036: illegal variable name/number"

This means that you are attempting to apply parameters that are not actually in the SQL text.

Do factory folks,

Do you have a solution for this?



private object[] Take(Customer customer)
             return new object[]  
                 "@CustomerId", customer.CustomerId,
                 "@CompanyName", customer.Company,
                 "@City", customer.City,
                 "@Country", customer.Country,
     "@Version", customer.Version.AsByteArray()

public void DeleteCustomer(Customer customer)
            string sql =
            @"DELETE FROM [Customer]
               WHERE CustomerId = @CustomerId 
                AND Version = @Version";

            Db.Update(sql, Take(customer));

 1 comment
I got rid of the Take helper method and just applied the parameters I needed per method. Example: object[] parms = { "@memberId", smid }; return DbAdapterHRMS.ReadFromStoredProcedure(proc, Make, parms); Thanks Dennis --- Dennis Tucker  Sep 24, 2012


Write Your Answer