public async Task <IAggregateRoot> GetAsync(object aggregateRootId, Type aggregateRootType) { if (aggregateRootId == null) { throw new ArgumentNullException("aggregateRootId"); } if (aggregateRootType == null) { throw new ArgumentNullException("aggregateRootType"); } if (_aggregateRootInfoDict.TryGetValue(aggregateRootId.ToString(), out AggregateCacheInfo aggregateRootInfo)) { var aggregateRoot = aggregateRootInfo.AggregateRoot; if (aggregateRoot.GetType() != aggregateRootType) { throw new Exception(string.Format("Incorrect aggregate root type, aggregateRootId:{0}, type:{1}, expecting type:{2}", aggregateRootId, aggregateRoot.GetType(), aggregateRootType)); } if (aggregateRoot.GetChanges().Count() > 0) { var lastestAggregateRoot = await _aggregateStorage.GetAsync(aggregateRootType, aggregateRootId.ToString()); if (lastestAggregateRoot != null) { ResetAggregateRootCache(lastestAggregateRoot); } return(lastestAggregateRoot); } return(aggregateRoot); } return(null); }
public async Task <T> GetAsync <T>(object id, bool firstFromCache = true) where T : class, IAggregateRoot { if (id == null) { throw new ArgumentNullException("id"); } var aggregateRootId = id.ToString(); if (_trackingAggregateRootDict.TryGetValue(aggregateRootId, out IAggregateRoot aggregateRoot)) { return(aggregateRoot as T); } if (firstFromCache) { aggregateRoot = await _repository.GetAsync <T>(id); } else { aggregateRoot = await _aggregateRootStorage.GetAsync(typeof(T), aggregateRootId); } if (aggregateRoot != null) { _trackingAggregateRootDict.TryAdd(aggregateRoot.UniqueId, aggregateRoot); return(aggregateRoot as T); } return(null); }
public async Task <IAggregateRoot> GetAsync(Type aggregateRootType, object aggregateRootId) { if (aggregateRootType == null) { throw new ArgumentNullException("aggregateRootType"); } if (aggregateRootId == null) { throw new ArgumentNullException("aggregateRootId"); } return(await _memoryCache.GetAsync(aggregateRootId, aggregateRootType) ?? await _aggregateRootStorage.GetAsync(aggregateRootType, aggregateRootId.ToString())); }