Here is a generic base class I wrote that derives from CommandBase: I use it to execute arbitrary SQL statements (including fetching of scalar values). You have to derive a class from it, though, and then you just override DataPortal_Execute.
(You can leave out the database key stuff -- we have up to 3 different databases we might be connected to, so all of our infrastructure has to be aware of which one to execute any command or fetch operation against)
The general idea of CommandBase is to just remote the CommandBase-derived object itself over to the server, do some work on the server side, and then bring the object back with the result in a class member. If you're already on the server, then it uses the local data portal. You don't really have to be aware of it.
For convenience, you usually provide a static factory invoker, so in your case the actual invocation might look like:
decimal discount = DiscountCommand.GetCurrentDiscount(...)
This factory method would take whatever parameters are required and assign them to members of the DiscountCommand. The DataPortal_Execute method, which runs on the server, uses these member values to retrieve your discount, which is then put in a separate member as a result. When the DiscountCommand object comes back from the data portal, you just return the result value.
[Serializable()]
public class SQLCommand<T, V> : CommandBase where V : SQLCommand<T, V>
{
protected string _sql;
protected T _result;
protected string _databaseKey;
protected SQLCommand()
: this(String.Empty, "Base")
{
}
protected SQLCommand(string sql, string databaseKey)
{
_sql = sql;
_databaseKey = databaseKey;
}
public static T Execute(string sqlText)
{
return Execute(sqlText, "Base");
}
public static T Execute(string sqlText, string databaseKey)
{
// Must retrieve via data portal
V result;
#if DEBUG
Type t = typeof(V);
string typeName = typeof(V).Name;
#endif
V command = Activator.CreateInstance(typeof(V),true) as V;
command._databaseKey = databaseKey;
command._sql = sqlText;
result = DataPortal.Execute<V>
(command);
return result._result;
}
protected override void DataPortal_Execute()
{
throw new System.NotImplementedException("Method DataPortal_Execute() must be implemented in derived class");
}
}