This example implements something like P-Invoke, but it calls database stored procedures instead of native functions.
Methods have to be defined as
extern like this:
[MethodImpl(MethodImplOptions.InternalCall)] public extern IEnumerable<Speaker> GetActiveSpeakers();
In this design, methods must be instance (non-static) and declared in a class derived from
BaseDpApi. This base class
Transaction properties used by the aspect.
StoredProcedureAttribute class is the implementation of the aspect itself. It is applied to the
with inheritance and multicast enabled, so any
extern method in a derived class will be turned into a stored
MethodImpl thing is ugly but unfortunately required if we want to use
extern methods. Other designs are possible,
such as providing a dummy implementation for the method.
To test this sample, you must create a SQL database, load
CreateDb.sql, and change the connection string in
This code is given for inspiration. A production-ready implementation should add error handling, test mapping of parameter types, implement out parameters, and so on.