public void AddOrUpdatePosition(Trade trade) { if (trade == null) { throw new ArgumentNullException(nameof(trade)); } // verify the account exists if (myUnitOfWork.AccountRepository.Find(trade.AccountCode) == null) { throw new ArgumentException("bad account code"); } // try to get existing position (note: I'm using .Result here because I'm fine with this method being synchronous) var position = positionQueryRepo.GetPositionAsync(trade.AccountCode, trade.SecurityId).Result; // if a position doesn't already exist, create it; otherwise, update it if (position == null) { CreatePosition(trade); } else { UpdatePosition(position, trade); } // save changes to data store in a single transaction myUnitOfWork.Commit("username"); }
public void OpenPosition(string accountCode, Trade trade) { // verify the account exists if (myUnitOfWork.AccountRepository.Find(accountCode) == null) { throw new ArgumentException("bad account code"); } // add trade myUnitOfWork.TradeRepository.Add(trade); // create and add position var position = new Position() { AccountCode = accountCode, PositionDate = trade.TradeDate, MarketValue = trade.MarketValue }; myUnitOfWork.PositionRepository.Add(position); // save changes to data store in a single transaction myUnitOfWork.Commit(); }