Channels ▼
RSS

Database

Using LINQ-to-SQL XML Mapping Files


3. Create a Custom DataContext Class

If you'll be querying the database directly using LINQ or Lambda expressions you can use the built-in System.Data.Linq.DataContext class directly to run your queries and skip this step. However, if you also need to query a stored procedure defined in a mapping file then you'll want to create a custom class that derives from the DataContext class. This is necessary since you'll need to access the protected ExecuteMethodCall() method found in DataContext which can be used to call stored procedures. Here's the signature for the ExecuteMethodCall() method:

//
// Summary:
//     Executes the stored database procedure or scalar function
//     associated with the specified CLR method.
//
// Parameters:
//   instance:
//  The instance of the method invocation (the current object).
//
//  methodInfo:
//   Identifies CLR method that corresponds to database method.
//
//   parameters:
//     The array of parameters to be passed to the command.
//
// Returns:
//     Result (the return value and output parameters) of 
//       executing the specified method.
protected internal IExecuteResult ExecuteMethodCall(object instance, MethodInfo methodInfo, params object[] parameters);

Creating a custom DataContext class is a bit of extra work, but I normally create a custom class that inherits from DataContext or my LINQ to SQL designer class if I'm using the Visual Studio 2008 designer anyway so that I can control things like logging and connection strings in a more centralized place. Here's a simple example of a custom class named CustomDataContext that derives from DataContext and exposes a GetCustomerByLastName() method that is used to call the ap_GetCustomerByLastName stored procedure shown previously. Notice that the method calls the DataContext class's ExecuteMethodCall() method and passes in the name of the method to call (contained within the System.Reflection.MethodInfo object) as well as the parameter data to pass to the stored procedure.

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;

namespace ConsoleApplication1
{
    public class CustomDataContext : DataContext
    {

        public CustomDataContext(string connStr, XmlMappingSource xmlMap) : base(connStr, xmlMap) { }

        public IEnumerable<DAL.Customer> GetCustomerByLastName(string lastNameLetter)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), lastNameLetter);
            return result.ReturnValue as IEnumerable<DAL.Customer>;
        }
    }
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video