Skip to content

Commit 9312cf0

Browse files
committed
Added example PostSharp.Samples.StoredProcedure.
1 parent a67be3f commit 9312cf0

8 files changed

Lines changed: 470 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using AutoMapper;
2+
using AutoMapper.Data;
3+
using PostSharp.Aspects;
4+
using PostSharp.Extensibility;
5+
using System;
6+
using System.Data;
7+
using System.Data.SqlClient;
8+
using System.Linq;
9+
using System.Reflection;
10+
using System.Runtime.InteropServices;
11+
using System.Text;
12+
13+
namespace PostSharp.Samples.StoredProcedure
14+
{
15+
16+
[StoredProcedure(AttributeInheritance = MulticastInheritance.Multicast)]
17+
internal abstract class BaseDbApi
18+
{
19+
20+
protected BaseDbApi( SqlConnection connection, SqlTransaction transaction = null )
21+
{
22+
this.Connection = connection;
23+
this.Transaction = transaction;
24+
25+
var mapperConfig = new MapperConfiguration(cfg =>
26+
{
27+
cfg.AddDataReaderMapping();
28+
cfg.CreateMap<IDataRecord, Speaker>();
29+
});
30+
31+
this.Mapper = mapperConfig.CreateMapper();
32+
33+
}
34+
35+
public SqlConnection Connection
36+
{
37+
get;
38+
}
39+
40+
public SqlTransaction Transaction
41+
{
42+
get;
43+
}
44+
45+
public IMapper Mapper
46+
{
47+
get;
48+
}
49+
50+
51+
}
52+
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CREATE TABLE Speakers
2+
(
3+
Id int NOT NULL PRIMARY KEY,
4+
Name varchar(255) NOT NULL,
5+
IsActive bit NOT NULL DEFAULT 1
6+
)
7+
GO
8+
9+
INSERT INTO Speakers ( Id, Name, IsActive ) VALUES ( 1, 'Adam Cogan', 1 )
10+
GO
11+
12+
INSERT INTO Speakers ( Id, Name, IsActive ) VALUES ( 2, 'Adam Dymitruk', 1 )
13+
GO
14+
15+
INSERT INTO Speakers ( Id, Name, IsActive ) VALUES ( 3, 'Alex Dunn', 1 )
16+
GO
17+
18+
INSERT INTO Speakers ( Id, Name, IsActive ) VALUES ( 4, 'Alexander Arvidsson', 0 )
19+
GO
20+
21+
22+
23+
CREATE PROCEDURE SetSpeakerStatus
24+
@Id int,
25+
@IsActive bit
26+
AS
27+
28+
UPDATE Speakers SET IsActive = @IsActive WHERE Id = @Id;
29+
30+
GO
31+
32+
CREATE PROCEDURE GetActiveSpeakers
33+
AS
34+
SELECT * FROM Speakers WHERE IsActive = 1;
35+
36+
GO
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
9+
<PostSharpProperties>DebuggerExtensionsMode=Disabled</PostSharpProperties>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="AutoMapper.Data">
14+
<Version>4.1.0</Version>
15+
</PackageReference>
16+
<PackageReference Include="PostSharp">
17+
<Version>6.6.11</Version>
18+
</PackageReference>
19+
<PackageReference Include="PostSharp.Community.ToString">
20+
<Version>1.2.0</Version>
21+
</PackageReference>
22+
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
23+
</ItemGroup>
24+
25+
</Project>
26+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Data.SqlClient;
3+
using System.Threading.Tasks;
4+
using System.Xml;
5+
6+
namespace PostSharp.Samples.StoredProcedure
7+
{
8+
internal class Program
9+
{
10+
public static async Task Main(string[] args)
11+
{
12+
using (SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=PostSharpSamples;Integrated Security=True"))
13+
{
14+
await connection.OpenAsync();
15+
16+
17+
SpeakerApi api = new SpeakerApi(connection);
18+
19+
api.SetSpeakerStatus(1, true);
20+
api.SetSpeakerStatus(2, true);
21+
22+
// Try the async API.
23+
foreach (var speaker in api.GetActiveSpeakers())
24+
{
25+
Console.WriteLine(speaker);
26+
}
27+
28+
await api.SetSpeakerStatusAsync(1, true);
29+
await api.SetSpeakerStatusAsync(2, false);
30+
31+
foreach (var speaker in api.GetActiveSpeakers())
32+
{
33+
Console.WriteLine(speaker);
34+
}
35+
36+
// TODO: GetActiveSpeakersAsync does not work because of a bug in PostSharp.
37+
38+
}
39+
}
40+
}
41+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+

2+
using PostSharp.Community.ToString;
3+
4+
namespace PostSharp.Samples.StoredProcedure
5+
{
6+
[ToString]
7+
internal class Speaker
8+
{
9+
public int Id
10+
{
11+
get; set;
12+
}
13+
14+
public string Name
15+
{
16+
get; set;
17+
}
18+
19+
public bool IsActive
20+
{
21+
get; set;
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using System.Data.SqlClient;
3+
using System.Runtime.CompilerServices;
4+
using System.Threading.Tasks;
5+
6+
namespace PostSharp.Samples.StoredProcedure
7+
{
8+
class SpeakerApi : BaseDbApi
9+
{
10+
public SpeakerApi(SqlConnection connection, SqlTransaction transaction = null) : base(connection, transaction)
11+
{
12+
}
13+
14+
[MethodImpl(MethodImplOptions.InternalCall)]
15+
public extern IEnumerable<Speaker> GetActiveSpeakers();
16+
17+
[MethodImpl(MethodImplOptions.InternalCall)]
18+
public extern void SetSpeakerStatus(int id, bool isActive);
19+
20+
[MethodImpl(MethodImplOptions.InternalCall)]
21+
public extern IAsyncEnumerable<Speaker> GetActiveSpeakersAsync();
22+
23+
[MethodImpl(MethodImplOptions.InternalCall)]
24+
public extern Task SetSpeakerStatusAsync(int id, bool isActive);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)