CQRSMicroservices.Application.Deserializer.CreateEvent C# (CSharp) Метод

CreateEvent() публичный Метод

You could implement this with reflection or some other deserialize algorithm. For this sample, we explicitly implemented this.
public CreateEvent ( Newtonsoft.Json.Linq.JObject eventJson ) : Event
eventJson Newtonsoft.Json.Linq.JObject
Результат CQRSMicroservices.Framework.Event
    public Event CreateEvent(JObject eventJson)
    {
      var eventName = eventJson.Properties().First().Name;
      var eventBody = eventJson.Properties().First().Value.Value<JObject>();

      switch(eventName)
      {
        case "CQRSMicroservices.Articles.ArticleCreatedEvent":

          return new ArticleCreatedEvent
          {
            ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
            Description = eventBody["Description"].Value<string>(),
            Price = decimal.Parse(eventBody["Price"].Value<string>(), CultureInfo.InvariantCulture)
          };

        case "CQRSMicroservices.Articles.ArticleAvailableEvent":

          return new ArticleAvailableEvent
          {
            ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
          };

        case "CQRSMicroservices.Articles.ArticleUnavailableEvent":
          return new ArticleUnavailableEvent
          {
            ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
          };

        case "CQRSMicroservices.Articles.ArticleSoldEvent":
          return new ArticleSoldEvent
          {
            ArticleId = Guid.Parse(eventBody["ArticleId"].Value<string>()),
            CustomerId = Guid.Parse(eventBody["CustomerId"].Value<string>()),
            Price = decimal.Parse(eventBody["Price"].Value<string>(), CultureInfo.InvariantCulture)
          };

        case "CQRSMicroservices.Customers.CustomerCreatedEvent":
          return new CustomerCreatedEvent
          {
            CustomerId = Guid.Parse(eventBody["CustomerId"].Value<string>()),
            Name = eventBody["Name"].Value<string>()
          };

        default:
          throw new EventNotFoundException(eventName);
      }
    }

Usage Example

    /// <summary>
    /// This is the main entry point for your service's partition replica. 
    /// RunAsync executes when the primary replica for this partition has write status.
    /// </summary>
    /// <param name="cancellationToken">Canceled when Service Fabric terminates this partition's replica.</param>
    protected override async Task RunAsync(CancellationToken cancellationToken)
    {
      var queue = await StateManager.GetOrAddAsync<IReliableQueue<string>>("eventBusQueue");

      // We use the ServiceFabricEventBus as our way of dispatching in this service.
      // The class has all the registered builders and can locate them through the ServiceClient.

      var eventBus = new ServiceFabricEventBus();
      Handlers.QueryModelBuilders.ToList().ForEach(eventBus.RegisterBuilder);
      var deserializer = new Deserializer();

      var count = (int)await queue.GetCountAsync();
      if(count > 0)
      {
        _semaphore.Release(count);
      }

      while(true)
      {
        cancellationToken.ThrowIfCancellationRequested();

        using(ITransaction tx = StateManager.CreateTransaction())
        {
          ConditionalResult<string> dequeueReply = await queue.TryDequeueAsync(tx);
          if(dequeueReply.HasValue)
          {
            string message = dequeueReply.Value;
            await eventBus.Dispatch(deserializer.CreateEvent(JObject.Parse(message)));
            await tx.CommitAsync();
          }
        }

        await _semaphore.WaitAsync(cancellationToken);
      }
    }