Skip to content

Commit 9dd4013

Browse files
authored
Add NServiceBus 10 version of the NHibernate snippets (#7750)
* Add NServiceBus 10 version of the NHibernate snippets * Remove numbers from the namespaces for version 11
1 parent 2834e3e commit 9dd4013

16 files changed

+462
-0
lines changed

Snippets/NHibernate/NHibernate.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NHibernate_9", "NHibernate_
1212
EndProject
1313
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NHibernate_10", "NHibernate_10\NHibernate_10.csproj", "{90854E02-DEAD-4F66-9C6A-C6C25B794448}"
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NHibernate_11", "NHibernate_11\NHibernate_11.csproj", "{E3B035A4-EE8B-44FB-B53B-D9E555E0BF3E}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -38,6 +40,10 @@ Global
3840
{90854E02-DEAD-4F66-9C6A-C6C25B794448}.Debug|Any CPU.Build.0 = Debug|Any CPU
3941
{90854E02-DEAD-4F66-9C6A-C6C25B794448}.Release|Any CPU.ActiveCfg = Release|Any CPU
4042
{90854E02-DEAD-4F66-9C6A-C6C25B794448}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{E3B035A4-EE8B-44FB-B53B-D9E555E0BF3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{E3B035A4-EE8B-44FB-B53B-D9E555E0BF3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{E3B035A4-EE8B-44FB-B53B-D9E555E0BF3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{E3B035A4-EE8B-44FB-B53B-D9E555E0BF3E}.Release|Any CPU.Build.0 = Release|Any CPU
4147
EndGlobalSection
4248
GlobalSection(SolutionProperties) = preSolution
4349
HideSolutionNode = FALSE
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using NServiceBus;
2+
using NServiceBus.SagaPersisters.NHibernate;
3+
4+
namespace NHibernate.Concurrency;
5+
6+
#region NHibernateConcurrencyLockMode
7+
[LockMode(LockModes.Read)]
8+
public class SagaDataWithLockMode :
9+
ContainSagaData
10+
#endregion
11+
12+
{
13+
[RowVersion]
14+
public int MyVersion { get; set; }
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using NServiceBus;
3+
using NServiceBus.SagaPersisters.NHibernate;
4+
5+
namespace NHibernate.Concurrency;
6+
7+
#region NHibernateConcurrencyRowVersion
8+
#pragma warning disable NSB0012 // Saga data classes should inherit ContainSagaData - [RowVersion] requires non-derived class
9+
10+
public class SagaDataWithRowVersion :
11+
IContainSagaData
12+
{
13+
[RowVersion]
14+
public virtual int MyVersion { get; set; }
15+
16+
public virtual string OriginalMessageId { get; set; }
17+
public virtual string Originator { get; set; }
18+
public virtual Guid Id { get; set; }
19+
}
20+
#pragma warning restore NSB0012 // Saga data classes should inherit ContainSagaData
21+
#endregion
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NServiceBus;
2+
using NServiceBus.Persistence;
3+
using NServiceBus.Persistence.NHibernate;
4+
5+
namespace NHibernate;
6+
7+
class DisableSchemaUpdates
8+
{
9+
void DisableSchemaUpdate(EndpointConfiguration endpointConfiguration)
10+
{
11+
#region DisableSchemaUpdate
12+
13+
var persistence = endpointConfiguration.UsePersistence<NHibernatePersistence>();
14+
persistence.DisableSchemaUpdate();
15+
16+
#endregion
17+
}
18+
19+
void DisableSubscriptionSchemaUpdate(EndpointConfiguration endpointConfiguration)
20+
{
21+
#region DisableSubscriptionSchemaUpdate
22+
23+
var persistence = endpointConfiguration.UsePersistence<NHibernatePersistence>();
24+
persistence.DisableSubscriptionStorageSchemaUpdate();
25+
26+
#endregion
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<RootNamespace>NHibernate</RootNamespace>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<PackageReference Include="NServiceBus.NHibernate" Version="11.0.0-alpha.2" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.Data;
3+
using NHibernate.Mapping.ByCode;
4+
using NHibernate.Mapping.ByCode.Conformist;
5+
using NServiceBus;
6+
using NServiceBus.NHibernate.Outbox;
7+
using NServiceBus.Outbox.NHibernate;
8+
9+
namespace NHibernate;
10+
11+
public class Outbox
12+
{
13+
public void TransactionIsolation(EndpointConfiguration endpointConfiguration)
14+
{
15+
#region OutboxTransactionIsolation
16+
17+
var outboxSettings = endpointConfiguration.EnableOutbox();
18+
outboxSettings.UseTransactionScope();
19+
outboxSettings.TransactionIsolationLevel(IsolationLevel.ReadCommitted);
20+
21+
#endregion
22+
}
23+
24+
public void CustomTableName(EndpointConfiguration endpointConfiguration)
25+
{
26+
#region OutboxNHibernateCustomTableNameConfig
27+
28+
var persistence = endpointConfiguration.UsePersistence<NHibernatePersistence>();
29+
persistence.CustomizeOutboxTableName(
30+
outboxTableName: "MyEndpointOutbox",
31+
outboxSchemaName: "MySchema");
32+
33+
#endregion
34+
}
35+
36+
public void PessimisticMode(EndpointConfiguration endpointConfiguration)
37+
{
38+
#region OutboxPessimisticMode
39+
40+
var outboxSettings = endpointConfiguration.EnableOutbox();
41+
outboxSettings.UsePessimisticConcurrencyControl();
42+
43+
#endregion
44+
}
45+
46+
public void TransactionScopeMode(EndpointConfiguration endpointConfiguration)
47+
{
48+
#region OutboxTransactionScopeMode
49+
50+
var outboxSettings = endpointConfiguration.EnableOutbox();
51+
outboxSettings.UseTransactionScope();
52+
53+
#endregion
54+
}
55+
56+
public void CustomMapping(EndpointConfiguration endpointConfiguration)
57+
{
58+
#region OutboxNHibernateCustomMappingConfig
59+
60+
var persistence = endpointConfiguration.UsePersistence<NHibernatePersistence>();
61+
persistence.UseOutboxRecord<MyOutboxRecord, MyOutboxRecordMapping>();
62+
63+
#endregion
64+
}
65+
66+
#region OutboxNHibernateCustomMapping
67+
68+
public class MyOutboxRecord :
69+
IOutboxRecord
70+
{
71+
public virtual string MessageId { get; set; }
72+
public virtual bool Dispatched { get; set; }
73+
public virtual DateTime? DispatchedAt { get; set; }
74+
public virtual string TransportOperations { get; set; }
75+
}
76+
77+
public class MyOutboxRecordMapping :
78+
ClassMapping<MyOutboxRecord>
79+
{
80+
public MyOutboxRecordMapping()
81+
{
82+
Table("MyOutboxTable");
83+
Id(
84+
idProperty: record => record.MessageId,
85+
idMapper: mapper => mapper.Generator(Generators.Assigned));
86+
Property(
87+
property: record => record.Dispatched,
88+
mapping: mapper =>
89+
{
90+
mapper.Column(c => c.NotNullable(true));
91+
mapper.Index("OutboxRecord_Dispatched_Idx");
92+
});
93+
Property(
94+
property: record => record.DispatchedAt,
95+
mapping: pm => pm.Index("OutboxRecord_DispatchedAt_Idx"));
96+
Property(
97+
property: record => record.TransportOperations,
98+
mapping: mapper => mapper.Type(NHibernateUtil.StringClob));
99+
}
100+
}
101+
102+
#endregion
103+
104+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<!--startcode OutboxNHibernateTimeToKeep-->
4+
<appSettings>
5+
<add key="NServiceBus/Outbox/NHibernate/TimeToKeepDeduplicationData"
6+
value="7.00:00:00" />
7+
<add key="NServiceBus/Outbox/NHibernate/FrequencyToRunDeduplicationDataCleanup"
8+
value="00:01:00" />
9+
</appSettings>
10+
<!--endcode -->
11+
</configuration>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace NHibernate.Session;
2+
3+
public class Order
4+
{
5+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using NServiceBus;
4+
5+
namespace NHibernate.Session;
6+
7+
#region NHibernateAccessingDataViaDI
8+
9+
public class OrderHandler :
10+
IHandleMessages<OrderMessage>
11+
{
12+
INHibernateStorageSession synchronizedStorageSession;
13+
14+
public OrderHandler(INHibernateStorageSession synchronizedStorageSession)
15+
{
16+
this.synchronizedStorageSession = synchronizedStorageSession;
17+
}
18+
19+
public Task Handle(OrderMessage message, IMessageHandlerContext context)
20+
{
21+
synchronizedStorageSession.Session.Save(new Order());
22+
return Task.CompletedTask;
23+
}
24+
}
25+
26+
#endregion
27+
28+
public class EndpointWithSessionRegistered
29+
{
30+
public void Configure(EndpointConfiguration config)
31+
{
32+
#region AccessingDataConfigureISessionDI
33+
34+
config.RegisterComponents(c =>
35+
{
36+
c.AddScoped<MyRepository, MyRepository>(svc =>
37+
{
38+
var session = svc.GetService<INHibernateStorageSession>();
39+
var repository = new MyRepository(session.Session);
40+
return repository;
41+
});
42+
});
43+
44+
#endregion
45+
}
46+
47+
public class MyRepository
48+
{
49+
public MyRepository(ISession session)
50+
{
51+
throw new System.NotImplementedException();
52+
}
53+
}
54+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading.Tasks;
2+
using NServiceBus;
3+
4+
namespace NHibernate.Session;
5+
6+
public class OrderHandlerViaContext :
7+
IHandleMessages<OrderMessage>
8+
{
9+
#region NHibernateAccessingDataViaContextHandler
10+
11+
public Task Handle(OrderMessage message, IMessageHandlerContext context)
12+
{
13+
var nhibernateSession = context.SynchronizedStorageSession.Session();
14+
nhibernateSession.Save(new Order());
15+
return Task.CompletedTask;
16+
}
17+
18+
#endregion
19+
}

0 commit comments

Comments
 (0)