From d2afa7a317d05fe3523809399634f7e47f86898e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 17 Jun 2024 15:11:26 +0100 Subject: [PATCH] Add legacy root converter for non proxied converters (#3518) --- .../ArcGISConverterModule.cs | 2 +- .../RhinoConverterModule.cs | 2 +- .../LegacyRootToSpeckleConverter.cs | 37 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 DUI3-DX/Sdk/Speckle.Converters.Common/LegacyRootToSpeckleConverter.cs diff --git a/DUI3-DX/Converters/ArcGIS/Speckle.Converters.ArcGIS3.DependencyInjection/ArcGISConverterModule.cs b/DUI3-DX/Converters/ArcGIS/Speckle.Converters.ArcGIS3.DependencyInjection/ArcGISConverterModule.cs index 30a739310b..88d26d8166 100644 --- a/DUI3-DX/Converters/ArcGIS/Speckle.Converters.ArcGIS3.DependencyInjection/ArcGISConverterModule.cs +++ b/DUI3-DX/Converters/ArcGIS/Speckle.Converters.ArcGIS3.DependencyInjection/ArcGISConverterModule.cs @@ -11,7 +11,7 @@ public class ArcGISConverterModule : ISpeckleModule public void Load(SpeckleContainerBuilder builder) { //don't need a host specific RootToSpeckleConverter - builder.AddConverterCommon(); + builder.AddConverterCommon(); // most things should be InstancePerLifetimeScope so we get one per operation builder.AddScoped(); builder.AddScoped(); diff --git a/DUI3-DX/Converters/Rhino/Speckle.Converters.Rhino7.DependencyInjection/RhinoConverterModule.cs b/DUI3-DX/Converters/Rhino/Speckle.Converters.Rhino7.DependencyInjection/RhinoConverterModule.cs index 9117848c90..088b93cc10 100644 --- a/DUI3-DX/Converters/Rhino/Speckle.Converters.Rhino7.DependencyInjection/RhinoConverterModule.cs +++ b/DUI3-DX/Converters/Rhino/Speckle.Converters.Rhino7.DependencyInjection/RhinoConverterModule.cs @@ -9,7 +9,7 @@ public class RhinoConverterModule : ISpeckleModule { public void Load(SpeckleContainerBuilder builder) { - builder.AddConverterCommon(); + builder.AddConverterCommon(); // single stack per conversion builder.AddScoped, RhinoConversionContextStack>(); } diff --git a/DUI3-DX/Sdk/Speckle.Converters.Common/LegacyRootToSpeckleConverter.cs b/DUI3-DX/Sdk/Speckle.Converters.Common/LegacyRootToSpeckleConverter.cs new file mode 100644 index 0000000000..34cd5576a2 --- /dev/null +++ b/DUI3-DX/Sdk/Speckle.Converters.Common/LegacyRootToSpeckleConverter.cs @@ -0,0 +1,37 @@ +using Speckle.Autofac.DependencyInjection; +using Speckle.Converters.Common.Objects; +using Speckle.Core.Models; + +namespace Speckle.Converters.Common; + +public class LegacyRootToSpeckleConverter : IRootToSpeckleConverter +{ + private readonly IFactory _toSpeckle; + + public LegacyRootToSpeckleConverter(IFactory toSpeckle) + { + _toSpeckle = toSpeckle; + } + + public Base Convert(object target) + { + Type type = target.GetType(); + try + { + var objectConverter = _toSpeckle.ResolveInstance(type.Name); //poc: would be nice to have supertypes resolve + + if (objectConverter == null) + { + throw new NotSupportedException($"No conversion found for {type.Name}"); + } + var convertedObject = objectConverter.Convert(target); + + return convertedObject; + } + catch (SpeckleConversionException e) + { + Console.WriteLine(e); + throw; // Just rethrowing for now, Logs may be needed here. + } + } +}