Skip to content

Commit 9301cfc

Browse files
authored
Merge pull request #618 from xBimTeam/develop
Minor fixes for release - easier testing of Xbim Services, fix for 4x3 BuildingStorey traversal (#616)
2 parents ecb8e33 + d5b7eb6 commit 9301cfc

File tree

6 files changed

+116
-36
lines changed

6 files changed

+116
-36
lines changed

Tests/DependencyInjectionTests.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ public void Can_Supply_External_ServiceProvider()
233233
[Fact]
234234
public void Services_Are_Registered_Once_Only()
235235
{
236-
var services = new ServiceCollection();
237236
int count = 0;
238237

239238
SuT.ConfigureServices(s =>
@@ -270,6 +269,33 @@ public void Minimial_Fallback_Services_Are_Always_Available()
270269
ifcStore.Instances.Should().NotBeEmpty();
271270
}
272271

272+
[Fact]
273+
public void Services_Can_Be_Disposed()
274+
{
275+
var logFactory = new LoggerFactory();
276+
SuT.ConfigureServices(s =>
277+
{
278+
s.AddXbimToolkit(c => c.AddLoggerFactory(logFactory));
279+
});
280+
281+
SuT.Dispose();
282+
283+
SuT = XbimServices.CreateInstanceInternal();
284+
285+
SuT.ConfigureServices(s =>
286+
{
287+
var services = s.Count;
288+
services.Should().Be(0);
289+
s.AddXbimToolkit();
290+
});
291+
292+
var newLogFactory = SuT.ServiceProvider.GetService<ILoggerFactory>();
293+
294+
newLogFactory.Should().NotBe(logFactory);
295+
296+
}
297+
298+
273299

274300
private class DummyModelProvider : IModelProvider
275301
{

Tests/Ifc4x3ExtensionsTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using Xbim.Common;
55
using Xbim.Ifc;
6+
using Xbim.Ifc4.Interfaces;
67
using Xbim.IO.Memory;
78
using Xunit;
89
using XbimCommonSchema = Xbim.Ifc4;
@@ -383,6 +384,23 @@ public void CanChangeUnits()
383384

384385
}
385386

387+
[Fact]
388+
public void CanGetBuildingStoreys()
389+
{
390+
using var model = MemoryModel.OpenRead(@"TestFiles\IFC4x3_ADD2\construction-scheduling-task.ifc");
391+
var building = model.Instances.FirstOrDefault<IIfcBuilding>();
392+
building.BuildingStoreys.Should().HaveCountGreaterThan(0);
393+
394+
}
395+
396+
[Fact]
397+
public void CanGetBuildingSubStoreys()
398+
{
399+
using var model = MemoryModel.OpenRead(@"TestFiles\IFC4x3_ADD2\construction-scheduling-task.ifc");
400+
var storey = model.Instances.FirstOrDefault<IIfcBuildingStorey>();
401+
storey.BuildingStoreys.Should().HaveCountGreaterThanOrEqualTo(0);
402+
}
403+
386404
#region Dispose
387405
private bool disposedValue;
388406

Xbim.Common/Configuration/XbimServices.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Xbim.Common.Configuration
1515
/// <remarks>the service delays building of the internal service provider when not configured to support
1616
/// late configutation of the services.
1717
/// In this case the service provision is delagated to <see cref="InternalServiceProvider"/></remarks>
18-
public class XbimServices
18+
public class XbimServices : IDisposable
1919
{
2020

2121
private XbimServices()
@@ -40,6 +40,7 @@ public static XbimServices CreateInstanceInternal()
4040

4141
static Lazy<XbimServices> lazySingleton = new Lazy<XbimServices>(() => new XbimServices());
4242
Lazy<IServiceProvider> serviceProviderBuilder;
43+
private bool disposedValue;
4344

4445
/// <summary>
4546
/// The shared instance of all Xbim Services
@@ -140,13 +141,8 @@ internal void Rebuild()
140141
{
141142
servicesCollection.Clear();
142143
isBuilt = false;
143-
144-
if (serviceProviderBuilder != null &&
145-
serviceProviderBuilder.IsValueCreated &&
146-
serviceProviderBuilder.Value is ServiceProvider sp)
147-
{
148-
sp.Dispose();
149-
}
144+
145+
DisposeServiceProvider();
150146
//rebuild the Lazy so IServiceProvider is rebuilt next time
151147
serviceProviderBuilder = new Lazy<IServiceProvider>(() =>
152148
{
@@ -157,6 +153,39 @@ internal void Rebuild()
157153
});
158154
}
159155

160-
156+
private void DisposeServiceProvider()
157+
{
158+
if (serviceProviderBuilder != null &&
159+
serviceProviderBuilder.IsValueCreated &&
160+
serviceProviderBuilder.Value is ServiceProvider sp)
161+
{
162+
sp.Dispose();
163+
}
164+
}
165+
166+
protected virtual void Dispose(bool disposing)
167+
{
168+
if (!disposedValue)
169+
{
170+
if (disposing)
171+
{
172+
Rebuild();
173+
}
174+
175+
disposedValue = true;
176+
}
177+
}
178+
179+
/// <summary>
180+
/// Disposes of the XbimServices and resources held by any <see cref="IServiceProvider"/> in use.
181+
/// Note: Not for general usage. This is typically used to reset the singleton DI between tests
182+
/// or to ensure orderly release of resources at application close.
183+
/// </summary>
184+
public void Dispose()
185+
{
186+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
187+
Dispose(disposing: true);
188+
GC.SuppressFinalize(this);
189+
}
161190
}
162191
}

Xbim.Ifc2x3/ProductExtension/IfcBuildingStoreyPartial.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ public IEnumerable<Xbim.Ifc4.Interfaces.IIfcSpace> Spaces
5454
}
5555
}
5656

57-
5857
public IEnumerable<Xbim.Ifc4.Interfaces.IIfcBuildingStorey> BuildingStoreys
5958
{
6059
get
6160
{
62-
var storeys = IsDecomposedBy.SelectMany(s => s.RelatedObjects).OfType<IfcBuildingStorey>().ToList();
61+
var storeys = IsDecomposedBy.SelectMany(s => s.RelatedObjects).OfType<IfcBuildingStorey>().ToList();
6362
storeys.Sort(CompareStoreysByElevation);
6463
return storeys;
6564
}
6665
}
66+
6767
}
6868
}

Xbim.Ifc4x3/Interfaces/IFC4/IfcBuilding.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,19 @@ public IEnumerable<IIfcSpace> Spaces
106106
}
107107
}
108108

109-
110-
111-
/// <summary>
112-
/// Returns the building storeys for this floor
113-
/// </summary>
114-
/// <returns></returns>
115-
public IEnumerable<IIfcBuildingStorey> BuildingStoreys
116-
{
117-
get
118-
{
119-
return IsDecomposedBy.SelectMany(s => s.RelatedObjects).OfType<IIfcBuildingStorey>()
120-
.OrderBy(s => s.Elevation.HasValue ? s.Elevation.Value : 0f);
121-
}
122-
}
123-
//##
124-
}
109+
/// <summary>
110+
/// Returns the building storeys for this floor
111+
/// </summary>
112+
/// <returns></returns>
113+
public IEnumerable<IIfcBuildingStorey> BuildingStoreys
114+
{
115+
get
116+
{
117+
var storeys = IsDecomposedBy.SelectMany(s => s.RelatedObjects).OfType<IfcBuildingStorey>().ToList();
118+
storeys.Sort(IfcBuildingStorey.CompareStoreysByElevation);
119+
return storeys;
120+
}
121+
}
122+
//##
123+
}
125124
}

Xbim.Ifc4x3/Interfaces/IFC4/IfcBuildingStorey.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ public IEnumerable<IIfcSpace> Spaces
8383
/// <returns></returns>
8484
public IEnumerable<IIfcBuildingStorey> BuildingStoreys
8585
{
86-
get
87-
{
88-
return IsDecomposedBy.SelectMany(s => s.RelatedObjects).OfType<IIfcBuildingStorey>()
89-
.OrderBy(s => s.Elevation.HasValue ? s.Elevation.Value : 0f);
90-
}
91-
}
86+
get
87+
{
88+
var storeys = IsDecomposedBy.SelectMany(s => s.RelatedObjects).OfType<IfcBuildingStorey>().ToList();
89+
storeys.Sort(CompareStoreysByElevation);
90+
return storeys;
91+
}
92+
}
9293
/// <summary>
9394
/// Returns the Gross Floor Area, if the element base quantity GrossFloorArea is defined
9495
/// </summary>
@@ -140,6 +141,13 @@ public TQType GetQuantity<TQType>(string qName) where TQType : IIfcPhysicalQuant
140141
var qSets = IsDefinedBy.SelectMany(r => r.RelatingPropertyDefinition.PropertySetDefinitions).OfType<IIfcElementQuantity>();
141142
return qSets.SelectMany(qset => qset.Quantities).OfType<TQType>().FirstOrDefault(q => q.Name == qName);
142143
}
143-
//##
144-
}
144+
145+
internal static int CompareStoreysByElevation(IfcBuildingStorey x, IfcBuildingStorey y)
146+
{
147+
double a = x.Elevation ?? 0;
148+
double b = y.Elevation ?? 0;
149+
return a.CompareTo(b);
150+
}
151+
//##
152+
}
145153
}

0 commit comments

Comments
 (0)