Sometimes is needed a custom functionality that goes far that the CRUD functionality generated by NetTiers. To acomplish this task we can use a custom stored procedure to get the data and expose it as a method of the componentlayer (services layer).
Follow the next steps to expose a custom stored procedure:
1) Configure Codesmith properties
CustomProceduresStartsWith (example: {1}{0}_)
ProcedurePrefix (example: cust_sp_ )
2) Add a Stored Procedure
Add a SP with a name that follows the convention defined in CustomProceduresStartsWith where {1} is the placeholder for
ProcedurePrefix and {0} is the placeholder for the TableName, for example cust_sp_Contact_GetBySalesOrderCountBetween (using AdventureWorks db)
CREATE PROCEDURE [dbo].[cust_sp_Contact_GetBySalesOrderCountBetween]
@min int,
@max int
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM Person.Contact C
WHERE
(SELECT COUNT(*) FROM Sales.SalesOrderHeader SOH
WHERE SOH.ContactId = C.ContactId) BETWEEN @min and @max
END
3) Run the template4) Use the methodNow in the componentlayer for the entity we have our custom stored procedure exposed as a method
ContactService contactService = new ContactService();
gvContacts.DataSource = contactService.GetBySalesOrderCountBetween(25, 30);
gvContacts.DataBind();
Note: the return type of the custom stored procedure method can be a TList or the defined in the CustomNonMatchingReturnType property (DataSet/IDataReader).
To determine what type will be return a method compares every column order and type returned from the Sp with every column order and type of the Entity table.
Debugging a bit the template, I have reviewed the method that compares the stored procedure column type with the table column type. This method returns false if some column type returned has no equivalent (File: \TemplateLib\CommonSqlCode.cs Method: IsMatching)
if (!SqlTypesAreEquivalent(command.CommandResults[0].Columns
.NativeType, table.Columns
.NativeType))
return false;
If you have UDT (User-Defined Data Types) fields in your table this method will be return false and CustomNonMatchingReturnType will be setted as Method Return type.
Usually is preferible that have an TList as method return than a Dataset or a IDataReader, because in this way you have all the NetTiers functionality to manipulate the data (deepload the TList returned for example). To force return a TList when have
UDT fields you can edit the file \TemplateLib\CommonSqlCode.cs and change in the method IsMatching the above line for this:
if (!SqlTypesAreEquivalent(command.CommandResults[0].Columns
.DataType.ToString(), table.Columns
.DataType.ToString()))
Read the complete post at http://feeds.feedburner.com/~r/MartinOlivares/~3/173987892/nettiers-expose-custom-stored.html
Posted
Oct 23 2007, 04:08 PM
by
CodeSmith on NewsGator Online