From 4166e036b9fa22378032f4f843801158950db04b Mon Sep 17 00:00:00 2001 From: Tatarincev Date: Thu, 16 Feb 2017 15:17:05 +0200 Subject: [PATCH] Change PolymorphicJsonConverter to use AbstractFactory instead switch-case --- .../PolymorphicOperationJsonConverter.cs | 39 +++---------------- VirtoCommerce.OrderModule.Web/Module.cs | 7 +--- 2 files changed, 7 insertions(+), 39 deletions(-) diff --git a/VirtoCommerce.OrderModule.Web/JsonConverters/PolymorphicOperationJsonConverter.cs b/VirtoCommerce.OrderModule.Web/JsonConverters/PolymorphicOperationJsonConverter.cs index 4e30a93e..55bd6765 100644 --- a/VirtoCommerce.OrderModule.Web/JsonConverters/PolymorphicOperationJsonConverter.cs +++ b/VirtoCommerce.OrderModule.Web/JsonConverters/PolymorphicOperationJsonConverter.cs @@ -39,28 +39,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist { object retVal = null; var obj = JObject.Load(reader); - - if (typeof(CustomerOrder).IsAssignableFrom(objectType)) - { - retVal = AbstractTypeFactory.TryCreateInstance(); - } - else if (typeof(LineItem).IsAssignableFrom(objectType)) - { - retVal = AbstractTypeFactory.TryCreateInstance(); - } - else if (typeof(Shipment).IsAssignableFrom(objectType)) - { - retVal = AbstractTypeFactory.TryCreateInstance(); - } - else if (typeof(PaymentIn).IsAssignableFrom(objectType)) - { - retVal = AbstractTypeFactory.TryCreateInstance(); - } - else if (typeof(CustomerOrderSearchCriteria).IsAssignableFrom(objectType)) - { - retVal = AbstractTypeFactory.TryCreateInstance(); - } - else if(objectType == typeof(PaymentMethod)) + + if(objectType == typeof(PaymentMethod)) { var paymentGatewayCode = obj["code"].Value(); retVal = _paymentMethodsService.GetAllPaymentMethods().FirstOrDefault(x => x.Code.EqualsInvariant(paymentGatewayCode)); @@ -70,18 +50,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist var shippingGatewayCode = obj["code"].Value(); retVal = _shippingMethodsService.GetAllShippingMethods().FirstOrDefault(x => x.Code.EqualsInvariant(shippingGatewayCode)); } - else if (typeof(IOperation).IsAssignableFrom(objectType)) + else { - var pt = obj["operationType"]; - if (pt != null) - { - var operationType = pt.Value(); - retVal = AbstractTypeFactory.TryCreateInstance(operationType); - if (retVal == null) - { - throw new NotSupportedException("Unknown operation type: " + operationType); - } - } + var tryCreateInstance = typeof(AbstractTypeFactory<>).MakeGenericType(objectType).GetMethods().FirstOrDefault(x => x.Name.EqualsInvariant("TryCreateInstance") && x.GetParameters().Count() == 0); + retVal = tryCreateInstance.Invoke(null, null); + } serializer.Populate(obj.CreateReader(), retVal); return retVal; diff --git a/VirtoCommerce.OrderModule.Web/Module.cs b/VirtoCommerce.OrderModule.Web/Module.cs index 06178a97..4f0a0e44 100644 --- a/VirtoCommerce.OrderModule.Web/Module.cs +++ b/VirtoCommerce.OrderModule.Web/Module.cs @@ -75,12 +75,7 @@ public override void Initialize() _container.RegisterType(); _container.RegisterType(); - _container.RegisterType(); - - //Thats need for PolymorphicOperationJsonConverter for API deserialization - AbstractTypeFactory.RegisterType(); - AbstractTypeFactory.RegisterType(); - AbstractTypeFactory.RegisterType(); + _container.RegisterType(); } public override void PostInitialize()