Skip to content

Instantly share code, notes, and snippets.

@chrisortman
Last active December 30, 2015 06:49
Show Gist options
  • Save chrisortman/7792303 to your computer and use it in GitHub Desktop.
Save chrisortman/7792303 to your computer and use it in GitHub Desktop.
I've been doing this thing where I pass an 'output' object into methods. The things I like about this are * I can vary implementation of output objects like one for an MVC ViewModel and another for a WPF ViewModel * Keeps privates of business objects (for lack of a better term) private * When I have methods that need to return more than one thin…
public class Payment {
private int _accountID;
private int _userID;
private decimal _paymentAmount;
/* I realize this looks almost like it should be a FI, but
that would just distract from the point */
public void OnAccount(int id) {
_accountID = id;
}
public void FromUser(int userID) {
_userID = userID;
}
public void Amount(decimal amount) {
_paymentAmount = amount;
}
public void WithCard(CardData c) {
_cardData = c;
}
/* THIS */
public void Review(ConfirmationPage page,ISomeLookupService s) {
page.PaymentDate = DateTime.Now;
page.PaymentDescription = "Credit Card ****" + _cardData.LastFour;
page.PaymentAccount = s.GetAccountDescription(_accountID);
}
/* INSTEAD OF */
public ConfirmationPage Review(ISomeLookupService s) {
var page = new ConfirmationPage();
page.PaymentDate = DateTime.Now;
page.PaymentDescription = "Credit Card ****" + _cardData.LastFour;
page.PaymentAccount = s.GetAccountDescription(_accountID);
return page;
}
}
@drusellers
Copy link

its more 'injectiony' which could be nice. and since its view model-y probably should NOT be created by the Payment object.

does this function Review really belong here?

@chrisortman
Copy link
Author

Possibly not. It feels dual purpose to have the setup code From/With/On mixed with the Do it code, but the only other way i could really see was to expose all the data in my 'Payment' out to whatever would do the Review

When I've done this so far, my 'Payment' is very close to a command. A lot of the time it will have a single Execute() method on it, but in some cases the command has multiple steps / phases so I was putting each step as a method like Review
void Review
void Confirm
void Submit

You're right about injectiony though, it really is just another way to invert control. I'm about to the point where I think 'new' for anything other than a value object that you only use within your method scope is going to wind up biting you.

@drusellers
Copy link

And if its working for you, then rock it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment