Skip to content

Commit 2899b95

Browse files
committed
Improvements to PostSharp.Samples.StoredProcedure.
1 parent 9312cf0 commit 2899b95

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

Diff for: Framework/PostSharp.Samples.StoredProcedure/CreateDb.sql

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
DROP TABLE IF EXISTS Speakers
2+
GO
3+
14
CREATE TABLE Speakers
25
(
36
Id int NOT NULL PRIMARY KEY,
@@ -19,6 +22,8 @@ INSERT INTO Speakers ( Id, Name, IsActive ) VALUES ( 4, 'Alexander Arvidsson', 0
1922
GO
2023

2124

25+
DROP PROCEDURE IF EXISTS SetSpeakerStatus
26+
GO
2227

2328
CREATE PROCEDURE SetSpeakerStatus
2429
@Id int,
@@ -29,8 +34,22 @@ AS
2934

3035
GO
3136

37+
DROP PROCEDURE IF EXISTS GetActiveSpeakers
38+
GO
39+
40+
3241
CREATE PROCEDURE GetActiveSpeakers
3342
AS
3443
SELECT * FROM Speakers WHERE IsActive = 1;
3544

45+
GO
46+
47+
DROP PROCEDURE IF EXISTS GetSpeakers
48+
GO
49+
50+
51+
CREATE PROCEDURE GetSpeakers
52+
AS
53+
SELECT * FROM Speakers;
54+
3655
GO

Diff for: Framework/PostSharp.Samples.StoredProcedure/Program.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ public static async Task Main(string[] args)
1616

1717
SpeakerApi api = new SpeakerApi(connection);
1818

19-
api.SetSpeakerStatus(1, true);
20-
api.SetSpeakerStatus(2, true);
2119

22-
// Try the async API.
23-
foreach (var speaker in api.GetActiveSpeakers())
20+
Console.WriteLine("All speakers:");
21+
foreach (var speaker in api.GetSpeakers())
2422
{
2523
Console.WriteLine(speaker);
2624
}
2725

28-
await api.SetSpeakerStatusAsync(1, true);
29-
await api.SetSpeakerStatusAsync(2, false);
26+
Console.WriteLine("Disable speaker 1...");
27+
await api.SetSpeakerStatusAsync(1, false);
3028

29+
Console.WriteLine("Active speakers:");
3130
foreach (var speaker in api.GetActiveSpeakers())
3231
{
3332
Console.WriteLine(speaker);

Diff for: Framework/PostSharp.Samples.StoredProcedure/SpeakerApi.cs

+10
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@ public SpeakerApi(SqlConnection connection, SqlTransaction transaction = null) :
1111
{
1212
}
1313

14+
15+
// Sync methods.
1416
[MethodImpl(MethodImplOptions.InternalCall)]
1517
public extern IEnumerable<Speaker> GetActiveSpeakers();
1618

19+
[MethodImpl(MethodImplOptions.InternalCall)]
20+
public extern IEnumerable<Speaker> GetSpeakers();
21+
1722
[MethodImpl(MethodImplOptions.InternalCall)]
1823
public extern void SetSpeakerStatus(int id, bool isActive);
1924

25+
26+
// Async variants of the same methods.
2027
[MethodImpl(MethodImplOptions.InternalCall)]
2128
public extern IAsyncEnumerable<Speaker> GetActiveSpeakersAsync();
2229

30+
[MethodImpl(MethodImplOptions.InternalCall)]
31+
public extern IAsyncEnumerable<Speaker> GetSpeakersAsync();
32+
2333
[MethodImpl(MethodImplOptions.InternalCall)]
2434
public extern Task SetSpeakerStatusAsync(int id, bool isActive);
2535
}

Diff for: Framework/PostSharp.Samples.StoredProcedure/StoredProcedureAttribute.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace PostSharp.Samples.StoredProcedure
1313
{
1414
[PSerializable]
15+
[MulticastAttributeUsage( MulticastTargets.Method, TargetMemberAttributes =MulticastAttributes.Instance)]
1516
internal class StoredProcedureAttribute : MethodInterceptionAspect
1617
{
1718
MethodInfo mapDataReaderMethod;
@@ -27,12 +28,19 @@ public override bool CompileTimeValidate(MethodBase method)
2728
{
2829
if (method.MethodImplementationFlags != MethodImplAttributes.InternalCall)
2930
{
30-
// We transform only extern methods.
31+
// We silently ignore any non-extern method.
3132
return false;
3233
}
3334

3435
var methodInfo = (MethodInfo) method;
3536

37+
// Validate the base type.
38+
if (!typeof(BaseDbApi).IsAssignableFrom(methodInfo.DeclaringType))
39+
{
40+
Message.Write(method, SeverityType.Error, "SP003", "Cannot apply the [StoredProcedure] aspect to {0} because the method is not declared in a type derived from BaseDbApi.", method);
41+
return false;
42+
}
43+
3644

3745
// Validate the parameter types.
3846
var success = true;

0 commit comments

Comments
 (0)