Jump To

MappedMethodAttribute

back to top...
The MappedMethodAttribute is used to tag data access layer methods (methods in your class that implements one of the sub-classes of DataAccessLayer) so that the SqlSiphon framework may find them in the call stack and use them to construct Stored Procedure calls. The MappedMethodAttribute also supports a few optional parameters that can be set in its constructor when tagging the method.

Name Type Default Value Description
Timeout int -1 Sets the DbCommand::CommandTimeout attribute when constructing the ADO.NET command, representing the number of seconds to wait before cancelling a call to a stored procedure. A value of -1 prevents any value from being set, leaving the default timeout as defined by the system or connection string.
Schema string NULL the database schema in which the stored procedure resides. Used to prefix the stored procedure name in the format <Schema>.<ProcedureName>. A NULL value prevents any schema from being prefixed to the Procedure Name, leaving the database to look for the procedure in the default schema as defined by the server, the user login settings, or the connection string.
CommandType System.Data.CommandType StoredProcedure Specifies the type of command to be used. Use this to specify Text queries inline with the mapping method.
Query string NULL If the CommandType is set to Text, used to specify the Parameterized SQL Statement that will be executed on the database.
ProcedureName string NULL An alias for the Query property. When used with the CommandType of StoredProcedure, can be used to specify the exact procedure name, instead of defaulting to the Mapped Method name. Useful for allowing multiple methods to call the same procedure.

Examples

back to top...
There are a few specific use cases in which one would require non-default values for the MappedMethodAttribute's properties. Keep in mind, there is no use case where all properties must be set explicitly.

Setting a Timeout of 30 Seconds

back to top...
For long-running procedures, it may be necessary to set the Timeout parameter to keep your application responsive.
[MappedMethod(Timeout=30)]
public void sp_Something(string MyParam)
{
    this.Execute(MyParam);
}

Aliasing a Procedure Name

back to top...
Stored procedures support multiple signatures that may be incompatible with the C# method definition system. Occasionally, one may wish to call the same stored procedure form multiple methods in the same data access layer (calling from different data access layer types is not a problem). In these cases, the ProcedureName parameter must be set.
[MappedMethod(ProcedureName="sp_MyProc")]
public string GetDefaultColumn()
{
    return this.Get<string>("Column1");
}

[MappedMethod(ProcedureName="sp_MyProc")]
public string GetSecondColumn()
{
    return this.Get<string>("Column2");
}
[MappedMethod(ProcedureName="sp_MyProc")]
public string GetColumn3()
{
    return this.Get<string>("Column3");
}

Executing a Parameterized Text Query

back to top...
WARNING: text queries are not recommended as they violate the best practice of keeping queries stored in the database as stored procedures. Large text queries may incur network transmission overhead, as well as may not benefit from being compiled on the database server.

For executing raw text queries, without a stored procedure, the CommandType and Query properties of the MappedMethod attribute must be set.
[MappedMethod(CommandType = CommandType.Text, Query=
@"SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @TableName")]
public List<string> GetColumnNames(string TableName)
{
    this.GetList<string>("COLUMN_NAME", TableName);
}

Executing a Procedure From a Specific Schema

back to top...
For stored procedures that do not reside in the dbo schema (or whatever schema is defined as the default for the user logged into the database), the Schema property on the MappedMethod attribute must be set.
[MappedMethod(Schema = "inventory")]
public List<Item> sp_Items_Get()
{
    return this.GetList<Item>();
}

Last edited Oct 8, 2012 at 3:16 PM by capn_midnight, version 10

Comments

No comments yet.