Skip to content

Instantly share code, notes, and snippets.

@ryanohs
Created February 25, 2011 17:09
Show Gist options
  • Save ryanohs/844100 to your computer and use it in GitHub Desktop.
Save ryanohs/844100 to your computer and use it in GitHub Desktop.
DDD update sample
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