InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Wrapping Stored Procedures in .NET Languages

Posted by Jonathan Allen on Jun 03, 2009

Sections
Architecture & Design,
Development,
Operations & Infrastructure
Topics
.NET ,
Language Design ,
Data Access
Tags
C# 4 ,
C# ,
F# ,
VB 10 ,
.NET Framework 4.0 ,
Visual Basic.NET

Creating wrapper functions for pre-existing stored procedures is surprisingly difficult in .NET. Stored procedures have certain calling conventions that aren’t generally used in the .NET Framework. These include:

  • Pass by Reference Parameters
  • Output Parameters
  • Optional Parameters with default values
  • All parameters are nullable

Until recently, full mappings were not possible. C# doesn’t currently support optional parameters, making placeholders for missing parameters necessary. VB could handle optional parameters, but not if those parameters were also nullable. For example, you cannot write:

Function FindCustomer(Optional ByVal firstName As String = Nothing, _ 
Optional ByVal lastName As String = Nothing, _
Optional ByVal zipCode As Integer? = Nothing) As DataTable

 This becomes a real concern when call search-style stored procedures that then to have numerous optional parameters. With C# 4 and VB 10, the last of these holes have been addressed. C# now supports optional values and both languages can use optional types in conjunction with nullable structures.

Feature

C#

Mapping

VB

Mapping

F#

Mapping

Pass by Reference

1

ref type [param]

7

ByRef [param] As [type]

4

[param] : [type] byref

Output

1

out type [param]

7

ByRef [param] As [type]

4

[param] : [type] byref

Optional Strings

4

string param = [default]

7

Optional [param] As String = [default]

N/A

4

Normal functions do not support optional parameters.

Method functions use: ?[param] : [type]

Optional (other)

4

[type] param = [default]

7

Optional param As [type] = [default]

Nullable Strings

1

string param

7

param as String

4

[param] : string

[param] : string option

Nullable (other)

2

[type]? param

8

param As Nullable(of

[type])

4

[param] : Nullable<[type]>

[param] : [type] option

9

param as [type]?

Optional Nullable (other)

4

[type]? param = [default]

10

Optional param As [type]? = [default]

4

?[param] : Nullable<[type]>

?[param] : [type] option

It should be noted that while F# does support returning multiple values, the support is very limited. There is no way to update more than one arbitrary mutable value without using the byref syntax.

Real .NET to Stored Procedure Implementation by Daniel Bullington Posted
  1. Back to top

    Real .NET to Stored Procedure Implementation

    by Daniel Bullington

    I have successfully created a framework for mapping interface methods to database stored prcoedure calls, as part of my blog code suite.

    The framework is well engineered ans fully unit (in the true sense) tested.

    The code is at: www.codeplex.com/SoftwareIsHardwork
    Blog: blog.softwareishardwork.com

    For example:

    [DatabaseContract]
    public interface IMockDatabaseContract : IDisposable
    {
    [CommandContract(CommandType.StoredProcedure, "usp_DoSomething", CommandBehavior.Default, -1, false)]
    void DoSomething([ParameterContract(DbType.Int32, 0, "@ItemId")] int itemId);
    }

    would map to

    exec usp_DoSomething @ItemId

    but the .NET code ONLY sees the contract; a factory dishes out transparent prxoies which intercept these methods and do the real database work.

    Take a look...

    dpb

    D. P. Bullington

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.