Neat Partially Partial C# 2.0 Keyword

Partial is great!

The new Partial keyword in C# 2.0 is a excellent addition to the language.

What does it do?

It allows you to create an extension to an existing class (as long as the original class is also marked Partial) without having to mess around with the original code.  You can also use partial to add your own code to structs and interfaces.

I found it as a great tool to incorporate my own CRUD (Create, Update, Delete) hook mechanism into any typed data source TableAdapter classes that VS.NET Data Source Wizard can create for you.

For example, I've used the VS.NET Data Source Wizard to create a typed dataset that contains OrderLineTableAdapter (partial) class. With Partial keyword I can create my own OrderLineTableAdapter.cs file and add the following code:

using System;
using
System.Data;
using
System.Data.OracleClient;

namespace WindowsApplication1
{
   
public partial class ProductCategoryLineTableAdapter
   
{
       
public void HookInsertCommand()
       
{
           
OracleCommand command = new OracleCommand(
               
"usp_InsertProductCategoryLine",
               
this.Connection);

            
command.CommandType = CommandType.StoredProcedure;
            
command.Parameters.AddRange(
               
new OracleParameter[] {
                   
new OracleParameter("CATEGORYID", OracleType.Number, 0, "CategoryID"),
                   
new OracleParameter("ITEMID", OracleType.Number, 0, "ItemID")
               
});

           
command.Parameters["CATEGORYID"].SourceVersion = DataRowVersion.Current;
           
command.Parameters["ITEMID"].SourceVersion = DataRowVersion.Current;

           
this.Adapter.InsertCommand = command;
       
}

       
public void HookUpdateCommand()
       
{
           
OracleCommand command = new OracleCommand(
               
"usp_UpdateProductCategoryLine",
               
this.Connection);

           
command.CommandType = CommandType.StoredProcedure;
           
command.Parameters.AddRange(
               
new OracleParameter[] {
                   
new OracleParameter("CATEGORYLINEID", OracleType.Number, 0, "CategoryLineID"),
                   
new OracleParameter("ITEMID", OracleType.Number, 0, "ItemID")
               
});

           
command.Parameters["CATEGORYLINEID"].SourceVersion = DataRowVersion.Original;
           
command.Parameters["ITEMID"].SourceVersion = DataRowVersion.Current;

           
this.Adapter.UpdateCommand = command;
       
}

       
public void HookDeleteCommand()
       
{
           
OracleCommand command = new OracleCommand(
               
"usp_DeleteProductCategoryLine",
               
this.Connection);

           
command.CommandType = CommandType.StoredProcedure;

           
command.Parameters.Add(
               
new OracleParameter("CATEGORYLINEID", OracleType.Number, 0, "CategoryLineID"));
           
command.Parameters["CATEGORYLINEID"].SourceVersion = DataRowVersion.Original;

           
this.Adapter.DeleteCommand = command;
       
}

        public void HookCRUDCommands()
        {
            this.HookInsertCommand();
            this.HookUpdateCommand();
            this.HookDeleteCommand();
        }

   
}
}

and in my WindowsApplication (or Data component) constructor / etc. method I can do the following:

using System;
using
System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1
()
        {
            InitializeComponents();
            productCategoryLineTableAdapter.HookCRUDCommands();
        }

        void updateButton_Click(object sender, EventArgs e)
        {
           
try
           
{
                productCategoryLineDataGridView.EndEdit(DataGridViewDataErrorContext.Commit);
                dataConnector.EndEdit();
                productCategoryLineTableAdapter.Update(myDataSet.ProductCategoryLine);
            }
           
catch
           
{
               
throw;
            }
        }

    }
}

in my main code and a call to productCategoryLineTableAdapter.Update(myDataSet.ProductCategoryLine) will now work perfectly.

 

Share this post: | | | |
Published Tuesday, December 21, 2004 2:48 AM by Jimmy Chandra
Filed under: , ,

Comments

# re: Neat Partially Partial C# 2.0 Keyword

kjmgET

Sunday, May 18, 2008 9:06 PM by gZNQLYCK

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: