Permalink
Browse files

Introduce Parameter Object

From tuple isomorphisms, we know that a method with multiple arguments
is isomorphic to a tuple with the same values. This is also Fowler's
Introduce Parameter Object refactoring.
  • Loading branch information...
ploeh committed Jan 12, 2018
1 parent a015b6f commit 64fa2638ffbdb81a077ac1dc3fbce697b3cba35b
@@ -82,6 +82,7 @@
<Compile Include="PaymentExample\PaymentJsonModel.cs" />
<Compile Include="PaymentExample\PaymentService.cs" />
<Compile Include="PaymentExample\PaymentType.cs" />
<Compile Include="PaymentExample\PaymentTypeParameters.cs" />
<Compile Include="PaymentExample\PaymentTypeTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BooleanTests.cs" />
@@ -15,12 +15,9 @@ public Child(ChildPaymentService childPaymentService)
this.childPaymentService = childPaymentService;
}
public T Match<T>(
Func<PaymentService, T> individual,
Func<PaymentService, T> parent,
Func<ChildPaymentService, T> child)
public T Match<T>(PaymentTypeParameters<T> parameters)
{
return child(childPaymentService);
return parameters.Child(childPaymentService);
}
}
}
@@ -12,8 +12,8 @@ public class ChildPaymentService
string originalTransactionKey,
PaymentService paymentService)
{
this.OriginalTransactionKey = originalTransactionKey;
this.PaymentService = paymentService;
OriginalTransactionKey = originalTransactionKey;
PaymentService = paymentService;
}
public string OriginalTransactionKey { get; }
@@ -8,9 +8,6 @@ namespace Ploeh.Samples.ChurchEncoding.PaymentExample
{
public interface IPaymentType
{
T Match<T>(
Func<PaymentService, T> individual,
Func<PaymentService, T> parent,
Func<ChildPaymentService, T> child);
T Match<T>(PaymentTypeParameters<T> parameters);
}
}
@@ -15,12 +15,9 @@ public Individual(PaymentService paymentService)
this.paymentService = paymentService;
}
public T Match<T>(
Func<PaymentService, T> individual,
Func<PaymentService, T> parent,
Func<ChildPaymentService, T> child)
public T Match<T>(PaymentTypeParameters<T> parameters)
{
return individual(paymentService);
return parameters.Individual(paymentService);
}
}
}
@@ -15,12 +15,9 @@ public Parent(PaymentService paymentService)
this.paymentService = paymentService;
}
public T Match<T>(
Func<PaymentService, T> individual,
Func<PaymentService, T> parent,
Func<ChildPaymentService, T> child)
public T Match<T>(PaymentTypeParameters<T> parameters)
{
return parent(paymentService);
return parameters.Parent(paymentService);
}
}
}
@@ -10,8 +10,8 @@ public class PaymentService
{
public PaymentService(string name, string action)
{
this.Name = name;
this.Action = action;
Name = name;
Action = action;
}
public string Name { get; }
@@ -11,31 +11,32 @@ public static class PaymentType
public static PaymentJsonModel ToJson(this IPaymentType payment)
{
return payment.Match(
individual : ps =>
new PaymentJsonModel
{
Name = ps.Name,
Action = ps.Action,
StartRecurrent = new ChurchFalse(),
TransactionKey = new Nothing<string>()
},
parent : ps =>
new PaymentJsonModel
{
Name = ps.Name,
Action = ps.Action,
StartRecurrent = new ChurchTrue(),
TransactionKey = new Nothing<string>()
},
child : cps =>
new PaymentJsonModel
{
Name = cps.PaymentService.Name,
Action = cps.PaymentService.Action,
StartRecurrent = new ChurchFalse(),
TransactionKey =
new Just<string>(cps.OriginalTransactionKey)
});
new PaymentTypeParameters<PaymentJsonModel>(
individual : ps =>
new PaymentJsonModel
{
Name = ps.Name,
Action = ps.Action,
StartRecurrent = new ChurchFalse(),
TransactionKey = new Nothing<string>()
},
parent : ps =>
new PaymentJsonModel
{
Name = ps.Name,
Action = ps.Action,
StartRecurrent = new ChurchTrue(),
TransactionKey = new Nothing<string>()
},
child : cps =>
new PaymentJsonModel
{
Name = cps.PaymentService.Name,
Action = cps.PaymentService.Action,
StartRecurrent = new ChurchFalse(),
TransactionKey =
new Just<string>(cps.OriginalTransactionKey)
}));
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ploeh.Samples.ChurchEncoding.PaymentExample
{
public class PaymentTypeParameters<T>
{
public PaymentTypeParameters(
Func<PaymentService, T> individual,
Func<PaymentService, T> parent,
Func<ChildPaymentService, T> child)
{
Individual = individual;
Parent = parent;
Child = child;
}
public Func<PaymentService, T> Individual { get; }
public Func<PaymentService, T> Parent { get; }
public Func<ChildPaymentService, T> Child { get; }
}
}

0 comments on commit 64fa263

Please sign in to comment.