Пример #1
0
        public AzureEventSourcedRepository(IEventStore eventStore, IEventStoreBusPublisher publisher, ITextSerializer serializer, IMetadataProvider metadataProvider, IMemoryCache cache)
        {
            this.eventStore       = eventStore;
            this.publisher        = publisher;
            this.serializer       = serializer;
            this.metadataProvider = metadataProvider;
            this.cache            = cache;

            // TODO: could be replaced with a compiled lambda to make it more performant
            var constructor = typeof(T).GetTypeInfo().GetConstructor(new[] { typeof(Guid), typeof(IEnumerable <IVersionedEvent>) });

            if (constructor == null)
            {
                throw new InvalidCastException(
                          "Type T must have a constructor with the following signature: .ctor(Guid, IEnumerable<IVersionedEvent>)");
            }
            this.entityFactory = (id, events) => (T)constructor.Invoke(new object[] { id, events });

            if (typeof(IMementoOriginator).IsAssignableFrom(typeof(T)) && this.cache != null)
            {
                // TODO: could be replaced with a compiled lambda to make it more performant
                var mementoConstructor = typeof(T).GetConstructor(new[] { typeof(Guid), typeof(IMemento), typeof(IEnumerable <IVersionedEvent>) });
                if (mementoConstructor == null)
                {
                    throw new InvalidCastException(
                              "Type T must have a constructor with the following signature: .ctor(Guid, IMemento, IEnumerable<IVersionedEvent>)");
                }
                this.originatorEntityFactory  = (id, memento, events) => (T)mementoConstructor.Invoke(new object[] { id, memento, events });
                this.cacheMementoIfApplicable = (T originator) =>
                {
                    string key     = GetPartitionKey(originator.Id);
                    var    memento = ((IMementoOriginator)originator).SaveToMemento();
                    this.cache.Set(
                        key,
                        new Tuple <IMemento, DateTime?>(memento, DateTime.UtcNow),
                        DateTimeOffset.UtcNow.AddMinutes(30));
                };
                this.getMementoFromCache = id => (Tuple <IMemento, DateTime?>) this.cache.Get(GetPartitionKey(id));
                this.markCacheAsStale    = id =>
                {
                    var key  = GetPartitionKey(id);
                    var item = (Tuple <IMemento, DateTime?>) this.cache.Get(key);
                    if (item != null && item.Item2.HasValue)
                    {
                        item = new Tuple <IMemento, DateTime?>(item.Item1, null);
                        this.cache.Set(
                            key,
                            item,
                            DateTimeOffset.UtcNow.AddMinutes(30));
                    }
                };
            }
            else
            {
                // if no cache object or is not a cache originator, then no-op
                this.cacheMementoIfApplicable = o => { };
                this.getMementoFromCache      = id => { return(null); };
                this.markCacheAsStale         = id => { };
            }
        }
        public AzureEventSourcedRepository(IEventStore <T> eventStore, IEventStoreBusPublisher <T> publisher)
        {
            this._eventStore          = eventStore;
            this._publisher           = publisher;
            _versionedEventSerializer = new VersionedEventSerializer();

            var constructor = typeof(T).GetConstructor(new[] { typeof(Guid), typeof(IEnumerable <IVersionedEvent>) });

            if (constructor == null)
            {
                throw new InvalidCastException(
                          "Type T must have a constructor with the following signature: .ctor(Guid, IEnumerable<IVersionedEvent>)");
            }
            _entityFactory = (id, events) => (T)constructor.Invoke(new object[] { id, events });
        }
Пример #3
0
 public PublisherProcessorAdapter(IEventStoreBusPublisher publisher)
 {
     this.publisher = publisher;
     this.tokenSource = new CancellationTokenSource();
 }
 public PublisherProcessorAdapter(IEventStoreBusPublisher publisher, CancellationToken token)
 {
     this.publisher = publisher;
     this.token = token;
 }
 public PublisherProcessorAdapter(IEventStoreBusPublisher publisher, CancellationToken token)
 {
     this.publisher = publisher;
     this.token     = token;
 }
Пример #6
0
 public EventPublishProcessor(IEventStoreBusPublisher <T> publisher)
 {
     _publisher   = publisher;
     _tokenSource = new CancellationTokenSource();
 }
Пример #7
0
 public PublisherProcessorAdapter(IEventStoreBusPublisher publisher)
 {
     this.publisher   = publisher;
     this.tokenSource = new CancellationTokenSource();
 }