Skip to content

Commit 2f694e8

Browse files
committed
Async regen
1 parent 0def815 commit 2f694e8

File tree

3 files changed

+214
-19
lines changed

3 files changed

+214
-19
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using System.Linq;
13+
using NHibernate.Cfg;
14+
using NHibernate.Cfg.MappingSchema;
15+
using NHibernate.Mapping.ByCode;
16+
using NUnit.Framework;
17+
using NHibernate.Linq;
18+
19+
namespace NHibernate.Test.NHSpecificTest.IlogsProxyTest
20+
{
21+
using System.Threading.Tasks;
22+
[TestFixture]
23+
public class FixtureAsync : TestCaseMappingByCode
24+
{
25+
private Guid _entityWithClassProxy1Id;
26+
private Guid _entityWithClassProxy2Id;
27+
private Guid _entityWithInterfaceProxy1Id;
28+
private Guid _entityWithInterfaceProxy2Id;
29+
private Guid _entityWithClassLookupId;
30+
private Guid _entityWithInterfaceLookupId;
31+
32+
protected override HbmMapping GetMappings()
33+
{
34+
var mapper = new ModelMapper();
35+
mapper.Class<EntityWithClassProxyDefinition>(rc =>
36+
{
37+
rc.Proxy(typeof(EntityWithClassProxyDefinition));
38+
39+
rc.Id(x => x.Id);
40+
rc.Property(x => x.Name);
41+
});
42+
43+
mapper.Class<EntityWithInterfaceProxyDefinition>(rc =>
44+
{
45+
rc.Proxy(typeof(IEntityProxy));
46+
47+
rc.Id(x => x.Id);
48+
rc.Property(x => x.Name);
49+
});
50+
51+
mapper.Class<EntityWithClassLookup>(rc =>
52+
{
53+
rc.Id(x => x.Id);
54+
rc.Property(x => x.Name);
55+
rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityWithClassProxyDefinition)));
56+
});
57+
58+
mapper.Class<EntityWithInterfaceLookup>(rc =>
59+
{
60+
rc.Id(x => x.Id);
61+
rc.Property(x => x.Name);
62+
rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityWithInterfaceProxyDefinition)));
63+
});
64+
65+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
66+
}
67+
68+
69+
protected override void OnSetUp()
70+
{
71+
using(var session = OpenSession())
72+
{
73+
var entityCP1 = new EntityWithClassProxyDefinition
74+
{
75+
Id = Guid.NewGuid(),
76+
Name = "Name 1"
77+
};
78+
_entityWithClassProxy1Id = entityCP1.Id;
79+
80+
var entityCP2 = new EntityWithClassProxyDefinition
81+
{
82+
Id = Guid.NewGuid(),
83+
Name = "Name 2"
84+
};
85+
_entityWithClassProxy2Id = entityCP2.Id;
86+
87+
var entityIP1 = new EntityWithInterfaceProxyDefinition
88+
{
89+
Id = Guid.NewGuid(),
90+
Name = "Name 1"
91+
};
92+
_entityWithInterfaceProxy1Id = entityIP1.Id;
93+
94+
var entityIP2 = new EntityWithInterfaceProxyDefinition
95+
{
96+
Id = Guid.NewGuid(),
97+
Name = "Name 2"
98+
};
99+
_entityWithInterfaceProxy2Id = entityIP2.Id;
100+
101+
session.Save(entityCP1);
102+
session.Save(entityCP2);
103+
session.Save(entityIP1);
104+
session.Save(entityIP2);
105+
106+
var entityCL = new EntityWithClassLookup
107+
{
108+
Id = Guid.NewGuid(),
109+
Name = "Name 1",
110+
EntityLookup = (EntityWithClassProxyDefinition)session.Load(typeof(EntityWithClassProxyDefinition), entityCP1.Id)
111+
};
112+
_entityWithClassLookupId = entityCL.Id;
113+
114+
var entityIL = new EntityWithInterfaceLookup
115+
{
116+
Id = Guid.NewGuid(),
117+
Name = "Name 1",
118+
EntityLookup = (IEntityProxy)session.Load(typeof(EntityWithInterfaceProxyDefinition), entityIP1.Id)
119+
};
120+
_entityWithInterfaceLookupId = entityIL.Id;
121+
122+
session.Save(entityCL);
123+
session.Save(entityIL);
124+
125+
session.Flush();
126+
}
127+
}
128+
129+
130+
protected override void OnTearDown()
131+
{
132+
using (var session = OpenSession())
133+
{
134+
using (var transaction = session.BeginTransaction())
135+
{
136+
session.Delete("from System.Object");
137+
138+
session.Flush();
139+
transaction.Commit();
140+
}
141+
}
142+
}
143+
144+
145+
[Test]
146+
public async Task UpdateEntityWithClassLookupAsync()
147+
{
148+
using (var session = OpenSession())
149+
{
150+
using(var transaction = session.BeginTransaction())
151+
{
152+
var entityToUpdate = await (session.Query<EntityWithClassLookup>()
153+
.FirstAsync(e => e.Id == _entityWithClassLookupId));
154+
155+
entityToUpdate.EntityLookup = (EntityWithClassProxyDefinition)await (session.LoadAsync(typeof(EntityWithClassProxyDefinition), _entityWithClassProxy2Id));
156+
157+
await (session.UpdateAsync(entityToUpdate));
158+
await (session.FlushAsync());
159+
await (transaction.RollbackAsync());
160+
}
161+
}
162+
}
163+
164+
165+
[Test]
166+
public async Task UpdateEntityWithInterfaceLookupAsync()
167+
{
168+
using(var session = OpenSession())
169+
{
170+
using(var transaction = session.BeginTransaction())
171+
{
172+
var entityToUpdate = await (session.Query<EntityWithInterfaceLookup>()
173+
.FirstAsync(e => e.Id == _entityWithInterfaceLookupId));
174+
175+
entityToUpdate.EntityLookup = (IEntityProxy)await (session.LoadAsync(typeof(EntityWithInterfaceProxyDefinition), _entityWithInterfaceProxy2Id));
176+
entityToUpdate.EntityLookup.Id = Guid.Empty;
177+
178+
await (session.UpdateAsync(entityToUpdate));
179+
await (session.FlushAsync());
180+
await (transaction.RollbackAsync());
181+
}
182+
}
183+
}
184+
}
185+
}

src/NHibernate.Test/NHSpecificTest/GH2043/Fixture.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@ public class Fixture : TestCaseMappingByCode
1616
private Guid _entityWithInterfaceProxy2Id;
1717
private Guid _entityWithClassLookupId;
1818
private Guid _entityWithInterfaceLookupId;
19-
20-
protected override void Configure(Configuration configuration)
21-
{
22-
base.Configure(configuration);
23-
24-
Cfg.Environment.UseReflectionOptimizer = false;
25-
}
26-
27-
19+
2820
protected override HbmMapping GetMappings()
2921
{
3022
var mapper = new ModelMapper();

src/NHibernate/Async/Engine/ForeignKeys.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,38 @@ public static async Task<bool> IsNotTransientSlowAsync(string entityName, object
171171
/// <remarks>
172172
/// Don't hit the database to make the determination, instead return null;
173173
/// </remarks>
174-
public static async Task<bool?> IsTransientFastAsync(string entityName, object entity, ISessionImplementor session, CancellationToken cancellationToken)
174+
public static Task<bool?> IsTransientFastAsync(string entityName, object entity, ISessionImplementor session, CancellationToken cancellationToken)
175175
{
176-
cancellationToken.ThrowIfCancellationRequested();
177-
if (Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, entity))
176+
if (cancellationToken.IsCancellationRequested)
178177
{
179-
// an unfetched association can only point to
180-
// an entity that already exists in the db
181-
return false;
178+
return Task.FromCanceled<bool?>(cancellationToken);
182179
}
180+
try
181+
{
182+
if (Equals(Intercept.LazyPropertyInitializer.UnfetchedProperty, entity))
183+
{
184+
// an unfetched association can only point to
185+
// an entity that already exists in the db
186+
return Task.FromResult<bool?>(false);
187+
}
188+
189+
// let the interceptor inspect the instance to decide
190+
191+
if (session.Interceptor.IsTransient(entity) == true)
192+
return Task.FromResult<bool?>(true);
183193

184-
// let the interceptor inspect the instance to decide
185-
// let the persister inspect the instance to decide
186-
return session.Interceptor.IsTransient(entity) ??
187-
await (session.GetEntityPersister(entityName, entity).IsTransientAsync(entity, session, cancellationToken)).ConfigureAwait(false);
194+
if (entity is INHibernateProxy proxy && proxy.HibernateLazyInitializer.IsUninitialized)
195+
{
196+
return Task.FromResult<bool?>(false);
197+
}
198+
199+
// let the persister inspect the instance to decide
200+
return session.GetEntityPersister(entityName, entity).IsTransientAsync(entity, session, cancellationToken);
201+
}
202+
catch (System.Exception ex)
203+
{
204+
return Task.FromException<bool?>(ex);
205+
}
188206
}
189207

190208
/// <summary>

0 commit comments

Comments
 (0)