Benefit of using CommandBase??

Benefit of using CommandBase??

Old forum URL: forums.lhotka.net/forums/t/1867.aspx


rhinoishere posted on Wednesday, November 29, 2006

Notes on the code blocks seen below
- Both code blocks do the same thing. They have the same interface and they return the same data. 
- Code block #2 is considerably less code and easier to read.
- Other than the obvious differences, the only notable difference is the "Serializable" attribute.

Why would there be so much effort to use a command object? Is it just so that the code will run on the app server rather than the client?

Code block #1
public static string GetDataOnSomething(int id)
{
   GetDataOnSomethingCommand result;
   result = DataPortal.Execute<GetDataOnSomethingCommand>
   (
new GetDataOnSomethingCommand(id));
   return result.ReturnedData;
}

[Serializable()]
private class GetDataOnSomethingCommand : CommandBase
{
   
private int _id;
   private string _returnedData = string.Empty;
   
   public
string ReturnedData
   {
      get { return _returnedData; }
   }

   public GetDataOnSomethingCommand(int id)
   {
      _id = id;
   }

   protected override void DataPortal_Execute()
   {
      using (SqlConnection cn = new SqlConnection(Database.YourConnection))
      {
         cn.Open();
         using (SqlCommand cm = cn.CreateCommand())
         {
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "storedProc";
            cm.Parameters.AddWithValue("@Id", _id);
            string returnedData = cm.ExecuteScalar().ToString();
            _returnedData = returnedData;
         }
      }

   }
}

Code block #2
public static string GetDataOnSomething(int id)
{
   using (SqlConnection cn = new SqlConnection(Database.YourConnection))
   {
      cn.Open();
      using (SqlCommand cm = cn.CreateCommand())
      {
         cm.CommandType = CommandType.StoredProcedure;
         cm.CommandText = "storedProc";
         cm.Parameters.AddWithValue("@Id", id);
         string returnedData = cm.ExecuteScalar().ToString();
      }
   }
   return returnedData;
}

 

thanks, Ryan

xAvailx replied on Wednesday, November 29, 2006

With the second code block you are bypassing the data portal. If you are not sure why the data portal is used, then post back and me/someone will explain.

HTH

ajj3085 replied on Wednesday, November 29, 2006

Well, you're bypassing the dataportal, which means you instantely lose the ability to use remoting to run data access code on an application server.

I think typically you'd also expose the command object itself to the UI.  Nor would you use a command object to simply return a single value from an Id.  Those kinds of mappings are usually done with a subclass to NameValueListBase.

HTH
Andy

rhinoishere replied on Wednesday, November 29, 2006

Thanks for the replies.

I think typically you'd also expose the command object itself to the UI. 

This statement confused me a bit.  Can you explain?

thanks, Ryan

ajj3085 replied on Wednesday, November 29, 2006

Well the command subclass would be publicly visible and have a static factory method, much like other business objects, since the CommandBase itself is considered a business object (it implements IBusinessObject).

So in your code you'd remove the static method which returns a string, make the command class public, and add a factory method for the command object.  The UI would then set whatever it needs to prior to executing the command then execute it.  After execution, it could read out any returned data, if any.

Copyright (c) Marimer LLC