Example #1
0
        public ServiceContext
            (DataModelDbContext serviceDataModel
            , ServiceCommandFactory serviceCommandFactory
            , IIngressQueue dataIngressTopic)
        {
            if (serviceDataModel == null)
            {
                throw new ArgumentNullException(nameof(serviceDataModel));
            }
            if (serviceCommandFactory == null)
            {
                throw new ArgumentNullException(nameof(serviceCommandFactory));
            }
            if (dataIngressTopic == null)
            {
                throw new ArgumentNullException(nameof(dataIngressTopic));
            }


            ServiceDataModel      = serviceDataModel;
            ServiceCommandFactory = serviceCommandFactory;
            DataIngressTopic      = dataIngressTopic;
        }
 public IngressGatewayTopicHandler()
 {
     serviceCommandFactory = new ServiceCommandFactory();
 }
        private static void processMessageInServiceLayer
            (DataModelDbContext dataModel
            , BrokeredMessage message
            , ServiceCommandFactory serviceCommandFactory)
        {
            if (dataModel == null)
            {
                throw new ArgumentNullException(nameof(dataModel));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (serviceCommandFactory == null)
            {
                throw new ArgumentNullException(nameof(serviceCommandFactory));
            }


            var messageId
                = Guid.Parse(message.MessageId);

            // If there is an exception between before the commit you do not
            // want to complete the message.  If there there is a problem
            // between the commit and the message being completed then we
            // have a message that will be processed twice so we take the
            // hit and just check to see if we have seen the message before
            // and if so just return and let the message be cleared of the
            // subscription.
            if (dataModel.HasSeenMessage(messageId))
            {
                return;
            }


            var transaction
                = dataModel
                  .Database
                  .BeginTransaction(IsolationLevel.Serializable);

            try {
                dataModel
                .AddMessageAsSeen(messageId);


                var serviceRequest
                    = new ServiceRequest(
                          dataModel
                          , Guid.Parse(message.CorrelationId)
                          , messageId
                          , TopicSourceName
                          , message.ContentType
                          , message.GetBody <string>()
                          , DateTime.Now
                          );


                // Because topics are heterogeneous we need to identify the
                // correct service command for the message. Different messages
                // have different handlers.
                //
                // It may be valid that we are not interested in auditing
                // certain messages in which case we need a default handler.
                serviceCommandFactory[message.ContentType]
                .Match(
                    just: serviceCommand => serviceCommand
                    , nothing: () => new UnhandledMessage()
                    )
                .Handle(serviceRequest);

                dataModel.SaveChanges();
                transaction.Commit();
            } finally {
                transaction.Dispose();
            }
        }