Skip to content

Commit

Permalink
Change PolymorphicJsonConverter to use AbstractFactory instead switch…
Browse files Browse the repository at this point in the history
…-case
  • Loading branch information
tatarincev committed Feb 16, 2017
1 parent cd52f87 commit 4166e03
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomerOrder>.TryCreateInstance();
}
else if (typeof(LineItem).IsAssignableFrom(objectType))
{
retVal = AbstractTypeFactory<LineItem>.TryCreateInstance();
}
else if (typeof(Shipment).IsAssignableFrom(objectType))
{
retVal = AbstractTypeFactory<Shipment>.TryCreateInstance();
}
else if (typeof(PaymentIn).IsAssignableFrom(objectType))
{
retVal = AbstractTypeFactory<PaymentIn>.TryCreateInstance();
}
else if (typeof(CustomerOrderSearchCriteria).IsAssignableFrom(objectType))
{
retVal = AbstractTypeFactory<CustomerOrderSearchCriteria>.TryCreateInstance();
}
else if(objectType == typeof(PaymentMethod))

if(objectType == typeof(PaymentMethod))
{
var paymentGatewayCode = obj["code"].Value<string>();
retVal = _paymentMethodsService.GetAllPaymentMethods().FirstOrDefault(x => x.Code.EqualsInvariant(paymentGatewayCode));
Expand All @@ -70,18 +50,11 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
var shippingGatewayCode = obj["code"].Value<string>();
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<string>();
retVal = AbstractTypeFactory<IOperation>.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;
Expand Down
7 changes: 1 addition & 6 deletions VirtoCommerce.OrderModule.Web/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ public override void Initialize()

_container.RegisterType<ICustomerOrderService, CustomerOrderServiceImpl>();
_container.RegisterType<ICustomerOrderSearchService, CustomerOrderServiceImpl>();
_container.RegisterType<ICustomerOrderBuilder, CustomerOrderBuilderImpl>();

//Thats need for PolymorphicOperationJsonConverter for API deserialization
AbstractTypeFactory<IOperation>.RegisterType<CustomerOrder>();
AbstractTypeFactory<IOperation>.RegisterType<Shipment>();
AbstractTypeFactory<IOperation>.RegisterType<PaymentIn>();
_container.RegisterType<ICustomerOrderBuilder, CustomerOrderBuilderImpl>();
}

public override void PostInitialize()
Expand Down

0 comments on commit 4166e03

Please sign in to comment.