public AggregateRoot Get <TAggregateRoot>(string aggregateRootId, IUnitOfWork unitOfWork, long maxGlobalSequenceNumber = long.MaxValue, bool createIfNotExists = false) { if (!EnabledFor <TAggregateRoot>()) { return(_aggregateRootRepository.Get <TAggregateRoot>(aggregateRootId, unitOfWork, maxGlobalSequenceNumber, createIfNotExists)); } var snapshot = _snapshotStore.LoadSnapshot <TAggregateRoot>(aggregateRootId, maxGlobalSequenceNumber); if (snapshot == null) { var aggregateRootInstance = _aggregateRootRepository.Get <TAggregateRoot>(aggregateRootId, unitOfWork, maxGlobalSequenceNumber, createIfNotExists); var checkedOutSequenceNumber = new AggregateRootInfo(aggregateRootInstance).SequenceNumber; if (maxGlobalSequenceNumber != long.MaxValue) { _snapshotStore.SaveSnapshot <TAggregateRoot>(aggregateRootId, aggregateRootInstance, maxGlobalSequenceNumber); } OnCommitted <TAggregateRoot>(aggregateRootId, unitOfWork, aggregateRootInstance, checkedOutSequenceNumber); return(aggregateRootInstance); } var preparedInstance = PrepareFromSnapshot <TAggregateRoot>(snapshot, maxGlobalSequenceNumber, unitOfWork, aggregateRootId); var sequenceNumberOfPreparedInstance = new AggregateRootInfo(preparedInstance).SequenceNumber; OnCommitted <TAggregateRoot>(aggregateRootId, unitOfWork, preparedInstance, sequenceNumberOfPreparedInstance); return(preparedInstance); }
public async Task Handle(ChangeDescription message) { var uploadData = await repository.Get(message.Id); uploadData.ChangeDescription(message.Description); await repository.Save(uploadData); }
public AggregateRoot Get <TAggregateRoot>(string aggregateRootId, IUnitOfWork unitOfWork, long maxGlobalSequenceNumber = Int64.MaxValue, bool createIfNotExists = false) { if (maxGlobalSequenceNumber < long.MaxValue) { var aggregateRoot = _aggregateRootRepository.Get <TAggregateRoot>(aggregateRootId, unitOfWork, maxGlobalSequenceNumber, createIfNotExists); return(aggregateRoot); } AggregateRootInfo info; if (_cache.TryRemove(aggregateRootId, out info)) { Console.WriteLine("HIT! {0}", aggregateRootId); unitOfWork.Committed += () => { Console.WriteLine("put {0}", aggregateRootId); _cache.TryAdd(aggregateRootId, info); }; return(info.Instance); } var instance = _aggregateRootRepository.Get <TAggregateRoot>(aggregateRootId, unitOfWork, maxGlobalSequenceNumber, createIfNotExists); unitOfWork.Committed += () => { Console.WriteLine("put {0}", aggregateRootId); _cache.TryAdd(aggregateRootId, new AggregateRootInfo(instance)); }; return(instance); }
public async Task Handle(MembersAssign message) { var group = await repository.Get(message.Id); UnnasignMembers(message, group); AssignMembers(message, group); await repository.Save(group); }
private void ConsumeInternal(Guid aggregateId, Func <RoomTypes, IEnumerable <object> > action) { using (new Mutex(true, aggregateId.ToString())) { var rooms = _aggregateRootRepository.Get <RoomTypes>(aggregateId); var events = action(rooms).ToList(); _aggregateRootRepository.Save(rooms, events); _messageBus.Publish(events); } }
public async Task HandleAsync(AccountTransferedEvent @event) { var client = await _aggregateRootRepository.Get <Account>(@event.Id); client.Deposit(@event.Amount, @event.Description); await _aggregateRootRepository.Save(client); }
public async Task <T> LoadAggregate <T>(Guid id, CancellationToken ct) where T : class, IAggregateRoot { for (var loadAttempts = 0; ; loadAttempts++) { using (var asyncLock = await _aggregateTrackerMutex.LockAsync(_sessionProfile.LoadAttemptTimeout, ct)) { var lockAcquired = asyncLock != null; if (lockAcquired && !_trackedAggregates.ContainsKey(id)) { var aggregate = await _repository.Get <T>(id); _trackedAggregates.Add(aggregate.Id, new AggregateDescriptor { Aggregate = aggregate, Version = aggregate.Version }); return(aggregate); } } ct.ThrowIfCancellationRequested(); if (loadAttempts > _sessionProfile.MaxLoadAttempts) { break; } await Task.Delay(Backoff); } throw new MaxRetryLimitExceededException(_sessionProfile.MaxLoadAttempts, id, typeof(T)); }
public void Handle(MyCommand command) { var g = Guid.Parse("28ca41b6-68d8-4464-b8f8-e270cc928371"); var es = _repository.Get(g); es.DoStuff(command.Something); }
public async Task Handle(CreateCategory message) { try { var item = await repository.Get(message.Id.ToString()); if (item != null) { throw new Exception($"Category with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var category = CategorySource.Create(message.Id, message.ParentId, message.Name, message.Color, message.Visible); await repository.Save(category); }
public static TAggregateRoot Get <TAggregateRoot>( this IAggregateRootRepository repo, string aggregateRootId) where TAggregateRoot : AggregateRoot, new() { var aggregateRoot = repo.Get <TAggregateRoot>(aggregateRootId, new InMemoryUnitOfWork(repo, new DefaultDomainTypeNameMapper()), createIfNotExists: true); return((TAggregateRoot)aggregateRoot); }
public async Task HandleAsync(ProductSubscribedEvent @event) { var client = await _aggregateRootRepository.Get <AggregatesModel.ClientAggregate.Client>(@event.ClientId); #region product var product = await _aggregateRootRepository.Get <AggregatesModel.ProductAggregate.Product>(@event.ProductId); product.SubscribeClient(@event.ClientId, $"{client.FirstName} {client.LastName}", client.Email); await _aggregateRootRepository.Save(product); #endregion #region account var aggregate = new Account(IdentityGenerator.NewSequentialGuid(IdentityGeneratorType.SequentialAsString), @event.Number, @event.ClientId, @event.ProductId); aggregate.Activate(); await _aggregateRootRepository.Save(aggregate); #endregion }
public async Task Handle(CreateJobCommand message) { try { var item = await repository.Get(message.Id); if (item != null) { throw new Exception($"Operational task with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var operationalTask = JobDomain.Create( message.Id, message.CategoryId, message.Title, message.JobTypeId, message.CreatorId, message.CreatedByRole, message.AddressList, message.RelationGroupList, message.ParentId); await repository.Save(operationalTask); }
public void Handle(RegisterEmployee command) { var employee = _repository.Get(Guid.NewGuid()); employee.Register( command.SocialSecurityNumber, command.FirstName, command.LastName, command.EmployedFrom ); }
AggregateRoot LoadAggregateRoot <TAggregateRoot>(string aggregateRootId, long globalSequenceNumber) where TAggregateRoot : class { var aggregateRootInfo = _aggregateRootRepository .Get <TAggregateRoot>(aggregateRootId, this, maxGlobalSequenceNumber: globalSequenceNumber); var frozen = new FrozenAggregateRootService(aggregateRootInfo, _realUnitOfWork); aggregateRootInfo.UnitOfWork = frozen; return(aggregateRootInfo); }
public void Handle(RegisterEmployee command) { //var id = Guid.Parse("6077b565-36a1-4d99-b848-b65159282fa6"); var id = Guid.NewGuid(); var employee = _repository.Get(id); employee.Register( command.SocialSecurityNumber, command.FirstName, command.LastName, command.EmployedFrom ); }
public AggregateRoot Get <TAggregateRoot>(string aggregateRootId, long globalSequenceNumberCutoff, bool createIfNotExists) { var aggregateRootInfoFromCache = GetAggregateRootFromCache(aggregateRootId, globalSequenceNumberCutoff); if (aggregateRootInfoFromCache != null) { return(aggregateRootInfoFromCache); } var aggregateRoot = _aggregateRootRepository.Get <TAggregateRoot>(aggregateRootId, this, globalSequenceNumberCutoff, createIfNotExists: createIfNotExists); AddToCache(aggregateRoot, globalSequenceNumberCutoff); return(aggregateRoot); }
public AggregateRoot Get <TAggregateRoot>(string aggregateRootId, IUnitOfWork unitOfWork, long maxGlobalSequenceNumber = long.MaxValue, bool createIfNotExists = false) { var stopwatch = Stopwatch.StartNew(); Type actualAggregateRootType = null; try { var aggregateRoot = _innnerAggregateRootRepository .Get <TAggregateRoot>(aggregateRootId, unitOfWork, maxGlobalSequenceNumber, createIfNotExists); actualAggregateRootType = aggregateRoot.GetType(); return(aggregateRoot); } finally { _operationProfiler.RecordAggregateRootGet(stopwatch.Elapsed, aggregateRootId, actualAggregateRootType ?? typeof(TAggregateRoot)); } }
public async Task Handle(CreateOperationalTaskCommand message) { try { var item = await repository.Get(message.Id.ToString()); if (item != null) { throw new Exception($"Operational task with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var operationalTask = OperationalTask.Create(message.Id, message.Year, message.Week, message.CategoryId, message.DepartmentId, message.Title); operationalTask.SaveDaysPerWeek(message.DaysPerWeek); operationalTask.ChangeAssignedEmployees(message.GroupId, message.AssignedEmployees); await repository.Save(operationalTask); }
public async Task Handle(CreateDayAssignCommand message) { try { var item = await repository.Get(message.Id); if (item != null) { throw new Exception($"DayAssign with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var time = GetTime(message.Date); var date = message.Date ?? GetDate(message.WeekDay, message.WeekNumber); var year = message.Date?.Year ?? DateTime.UtcNow.Year; var dayAssign = DayAssignDomain.Create( message.Id, message.JobId, message.JobAssignId, message.DepartmentId, message.GroupId, message.UserIdList, date, time, message.EstimatedMinutes, message.WeekNumber, message.Address, message.TeamLeadId, message.WeekDay, message.DayPerWeekId, message.IsAssignedToAllUsers, message.Comment, message.ResidentName, message.ResidentPhone, message.Type, message.ExpiredDayAssignId, message.ExpiredWeekNumber, year, message.IsUrgent); await repository.Save(dayAssign, message.CreatorId); }
public void Handle(Transfer transfer) { var transferring = _repository.Get(Guid.NewGuid()); transferring.Transfer(transfer.From, transfer.To, transfer.Amount); }
protected async Task <TAggregateRoot> Get(Guid aggregateId) { return(await _aggregateRootRepository.Get <TAggregateRoot>(aggregateId)); }
AggregateRoot GetFromInnerRepository <TAggregateRoot>(string aggregateRootId, IUnitOfWork unitOfWork, long maxGlobalSequenceNumber, bool createIfNotExists) { var aggregateRoot = _innerAggregateRootRepository.Get <TAggregateRoot>(aggregateRootId, unitOfWork, maxGlobalSequenceNumber, createIfNotExists); return(aggregateRoot); }
public void Handle(ReplayAll command) { var eventStore = _repository.Get(EventStore.SystemEventStoreId); eventStore.ReplayAll(); }
public AggregateRoot Get <TAggregateRoot>(string aggregateRootId, long globalSequenceNumberCutoff, bool createIfNotExists) { return(_aggregateRootRepository.Get <TAggregateRoot>(aggregateRootId, this, globalSequenceNumberCutoff)); }
public void Handle(MyCommand command) { var ar = _repository.Get(Guid.NewGuid()); ar.DoStuff(); }
public async Task Handle(CreateJobAssignCommand message) { try { var item = await repository.Get(message.Id.ToString()); if (item != null) { throw new Exception($"JobAssign with id: {message.Id} already exist"); } } catch (AggregateNotFoundException) { // That is fine that id not used } var jobAssign = JobAssignDomain.Create(message.Id, new List <string> { message.JobId }, message.CreatedByRole, message.TillYear); await repository.Save(jobAssign); }