当前位置: 首页>>代码示例>>C#>>正文


C# Func.IfNotNull方法代码示例

本文整理汇总了C#中Func.IfNotNull方法的典型用法代码示例。如果您正苦于以下问题:C# Func.IfNotNull方法的具体用法?C# Func.IfNotNull怎么用?C# Func.IfNotNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Func的用法示例。


在下文中一共展示了Func.IfNotNull方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Write

        public static long Write(int howMany, 
            Func<int, IEvent> createEvent = null, 
            Func<EventStoreDbContext> createEventStore = null)
        {
            createEvent = createEvent ?? (i => new Order.ItemAdded
            {
                SequenceNumber = i,
                AggregateId = Recipes.Any.Guid(),
                Price = 1.99m,
                ProductName = Recipes.Any.Paragraph(3),
                Quantity = 1
            });

            Console.WriteLine("writing {0} events", howMany);

            long lastEventId = 0;

            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            using (var eventStore = createEventStore.IfNotNull()
                                                    .Then(c => c())
                                                    .Else(() => new EventStoreDbContext()))
            {
                Enumerable.Range(1, howMany).ForEach(i =>
                {
                    var e = createEvent(i);

                    e.IfTypeIs<Event>()
                     .ThenDo(ev =>
                     {
                         if (ev.AggregateId == Guid.Empty)
                         {
                             ev.AggregateId = Guid.NewGuid();
                         }

                         if (ev.SequenceNumber == 0)
                         {
                             ev.SequenceNumber = i;
                         }
                     });

                    var storableEvent = e.ToStorableEvent();

                    eventStore.Events.Add(storableEvent);
                    eventStore.SaveChanges();

                    lastEventId = storableEvent.Id;
                    Console.WriteLine("wrote event " + lastEventId);
                });
            }

            return lastEventId;
        }
开发者ID:halakaraki,项目名称:Its.Cqrs,代码行数:52,代码来源:Events.cs

示例2: Calculate

        public static IEnumerable<EventHandlerProgress> Calculate(
            Func<DbContext> createDbContext,
            Func<EventStoreDbContext> createEventStoreDbContext = null)
        {
            if (createDbContext == null)
            {
                throw new ArgumentNullException("createDbContext");
            }

            int count;

            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            using (var db = createEventStoreDbContext.IfNotNull()
                                                     .Then(create => create())
                                                     .Else(() => new EventStoreDbContext()))
            {
                count = db.Events.Count();
            }

            if (count == 0)
            {
                return Enumerable.Empty<EventHandlerProgress>();
            }

            var now = Clock.Now();
            var progress = new List<EventHandlerProgress>();

            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            using (var db = createDbContext())
            {
                var readModelInfos = db.Set<ReadModelInfo>().ToArray();
                
                readModelInfos
                    .ForEach(i =>
                    {
                        var eventsProcessed = i.InitialCatchupEndTime.HasValue
                            ? i.BatchTotalEvents - i.BatchRemainingEvents
                            : i.InitialCatchupEvents - i.BatchRemainingEvents;

                        long? timeTakenForProcessedEvents = null;
                        if (i.BatchStartTime.HasValue && i.InitialCatchupStartTime.HasValue)
                        {
                            timeTakenForProcessedEvents = i.InitialCatchupEndTime.HasValue
                                ? (now - i.BatchStartTime).Value.Ticks
                                : (now - i.InitialCatchupStartTime).Value.Ticks;
                        }

                        if (eventsProcessed == 0)
                        {
                            return;
                        }

                        var eventHandlerProgress = new EventHandlerProgress
                        {
                            Name = i.Name,
                            InitialCatchupEvents = i.InitialCatchupEvents,
                            TimeTakenForInitialCatchup = i.InitialCatchupStartTime.HasValue
                                ? (i.InitialCatchupEndTime.HasValue ? i.InitialCatchupEndTime : now) - i.InitialCatchupStartTime
                                : null,
                            TimeRemainingForCatchup = eventsProcessed != 0 && timeTakenForProcessedEvents.HasValue
                                ? (TimeSpan?) TimeSpan.FromTicks((long) (timeTakenForProcessedEvents*(i.BatchRemainingEvents/(decimal) eventsProcessed)))
                                : null,
                            EventsRemaining = i.BatchRemainingEvents,
                            PercentageCompleted = (1 - ((decimal) i.BatchRemainingEvents/count))*100,
                            LatencyInMilliseconds = i.LatencyInMilliseconds,
                            LastUpdated = i.LastUpdated,
                            CurrentAsOfEventId = i.CurrentAsOfEventId,
                            FailedOnEventId = i.FailedOnEventId,
                            Error = i.Error
                        };

                        progress.Add(eventHandlerProgress);
                    });
            }

            return progress;
        }
开发者ID:gitter-badger,项目名称:Its.Cqrs,代码行数:77,代码来源:EventHandlerProgressCalculator.cs


注:本文中的Func.IfNotNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。