Type TryGetType(string typeName) { try { return(_domainTypeNameMapper.GetType(typeName)); } catch { return(null); } }
/// <summary> /// Gets the aggregate root of the specified type with the specified ID by hydrating it with events from the event store. The /// root will have events replayed until the specified <seealso cref="maxGlobalSequenceNumber"/> ceiling. If the root has /// no events (i.e. it doesn't exist yet), a newly initialized instance is returned. /// </summary> public AggregateRoot Get <TAggregateRoot>(string aggregateRootId, IUnitOfWork unitOfWork, long maxGlobalSequenceNumber = long.MaxValue, bool createIfNotExists = false) { var domainEventsForThisAggregate = _eventStore.Load(aggregateRootId); var eventsToApply = domainEventsForThisAggregate .Where(e => e.GetGlobalSequenceNumber() <= maxGlobalSequenceNumber) .Select(e => _domainEventSerializer.Deserialize(e)); AggregateRoot aggregateRoot = null; foreach (var e in eventsToApply) { if (aggregateRoot == null) { if (!e.Meta.ContainsKey(DomainEvent.MetadataKeys.Owner)) { throw new InvalidOperationException(string.Format("Attempted to load aggregate root with ID {0} but the first event {1} did not contain metadata with the aggregate root type name!", aggregateRootId, e)); } var aggregateRootTypeName = e.Meta[DomainEvent.MetadataKeys.Owner]; var aggregateRootType = _domainTypeNameMapper.GetType(aggregateRootTypeName); aggregateRoot = CreateNewAggregateRootInstance(aggregateRootType, aggregateRootId, unitOfWork); } aggregateRoot.ApplyEvent(e, ReplayState.ReplayApply); } if (aggregateRoot == null) { if (!createIfNotExists) { throw new ArgumentException(string.Format("Attempted to load aggregate root with ID {0} as {1}, but it didn't exist!", aggregateRootId, typeof(TAggregateRoot))); } aggregateRoot = CreateNewAggregateRootInstance(typeof(TAggregateRoot), aggregateRootId, unitOfWork); aggregateRoot.InvokeCreated(); } return(aggregateRoot); }