Created
February 25, 2011 17:09
-
-
Save ryanohs/844100 to your computer and use it in GitHub Desktop.
DDD update sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TradeController | |
{ | |
private readonly IRepository<Trade> _Trades; | |
public TradeController(IRepository<Trade> trades) | |
{ | |
_Trades = trades; | |
} | |
[ValidationExceptionFilter] | |
public JsonResult Update(UpdateTrade parameters) | |
{ | |
var trade = _Trades.Get(parameters.Id); | |
trade.Update(parameters); | |
return Json(new {success = true}); | |
} | |
} | |
public class Trade | |
{ | |
public Guid Id { get; private set; } | |
public decimal Quantity { get; private set; } | |
public decimal Price { get; private set; } | |
public void Update(UpdateTrade parameters) | |
{ | |
// Of course I'd expect a little more complex validation logic | |
if(parameters.Price <= 0) | |
{ | |
throw new ValidationException("Price must be greater than zero."); | |
} | |
Price = parameters.Price; | |
Quantity = parameters.Quantity; | |
} | |
} | |
// input model -- usually not exactly equivalent to the domain model | |
public class UpdateTrade | |
{ | |
public Guid Id { get; set; } | |
public decimal Quantity { get; set; } | |
public decimal Price { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment