Skip to content

Commit 536f377

Browse files
committed
StoredProcedure: added README
1 parent 2899b95 commit 536f377

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
This example implements something like P-Invoke, but it calls database stored procedures instead of native functions.
3+
4+
Methods have to be defined as `extern` like this:
5+
6+
```cs
7+
[MethodImpl(MethodImplOptions.InternalCall)]
8+
public extern IEnumerable<Speaker> GetActiveSpeakers();
9+
```
10+
11+
In this design, methods must be instance (non-static) and declared in a class derived from `BaseDpApi`. This base class
12+
defined the `Connection` and `Transaction` properties used by the aspect.
13+
14+
The `StoredProcedureAttribute` class is the implementation of the aspect itself. It is applied to the `BaseDbApi` class
15+
with inheritance and multicast enabled, so any `extern` method in a derived class will be turned into a stored
16+
procedure call.
17+
18+
The `MethodImpl` thing is ugly but unfortunately required.
19+
20+
To test this sample, you must create a SQL database, load `CreateDb.sql`, and change the connection string in `Program.Main`.
21+
22+
This code is given for inspiration. A production-ready implementation should add error handling, test mappings,
23+
implement out parameters, and so on.
24+

Framework/PostSharp.Samples.StoredProcedure/StoredProcedureAttribute.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public override void OnInvoke(MethodInterceptionArgs args)
9595
args.ReturnValue = this.mapDataReaderMethod.MakeGenericMethod(returnType.GetGenericArguments()[0]).Invoke(null, new object[] { reader, instance.Mapper });
9696
}
9797

98+
// TODO: Map out parameters back to method arguments.
99+
98100
}
99101

100102
public override async Task OnInvokeAsync(MethodInterceptionArgs args)
@@ -127,6 +129,8 @@ public override async Task OnInvokeAsync(MethodInterceptionArgs args)
127129

128130
}
129131

132+
// TODO: Map out parameters back to method arguments.
133+
130134
}
131135

132136
private static IEnumerable<T> MapDataReader<T>(SqlDataReader reader, IMapper mapper)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ or you can download them on [GitHub](https://www.github.com/postsharp/PostSharp.
2424
| [PostSharp.Samples.AutoDataContract](Framework/PostSharp.Samples.AutoDataContract/) | Automatically adds `[DataContract]` and `[DataMember]` attributes to derived classes and all properties |
2525
| [PostSharp.Samples.Authorization](Framework/PostSharp.Samples.Authorization/) | Requires permissions before getting or setting fields or executing methods. |
2626
| [PostSharp.Samples.NormalizeString](Framework/PostSharp.Samples.NormalizeString/) | Trims and lowercases strings before they are assigned to a field or property.
27+
| [PostSharp.Samples.StoredProcedure](Framework/PostSharp.Samples.StoredProcedure/) | Implements something like P-Invoke, but for database stored procedures. |
2728
| **Diagnostics**
2829
| [PostSharp.Samples.Logging.Customization](Diagnostics/PostSharp.Samples.Logging.Customization/) | Shows how to customize PostSharp Logging. |
2930
| [PostSharp.Samples.Logging.Console](Diagnostics/PostSharp.Samples.Logging.Console/) | Demonstrates how to configure PostSharp Logging so that it directs its output to the *system console*. |

0 commit comments

Comments
 (0)