public SapphireChangeNotifier(
     DbContextAccesor dbContextAccessor,
     IServiceProvider serviceProvider,
     DbContextTypeContainer contextTypeContainer,
     SubscriptionManager subscriptionManager)
 {
     this.dbContextAccessor    = dbContextAccessor;
     this.serviceProvider      = serviceProvider.CreateScope().ServiceProvider;
     this.contextTypeContainer = contextTypeContainer;
     this.subscriptionManager  = subscriptionManager;
 }
 public SapphireChangeNotifier(
     ConnectionManager connectionManager,
     DbContextAccesor dbContextAccessor,
     IServiceProvider serviceProvider,
     ILogger <WebsocketConnection> logger,
     DbContextTypeContainer contextTypeContainer)
 {
     this.connectionManager    = connectionManager;
     this.dbContextAccessor    = dbContextAccessor;
     this.serviceProvider      = serviceProvider;
     this.logger               = logger;
     this.contextTypeContainer = contextTypeContainer;
 }
Exemple #3
0
        public SapphireDatabaseBuilder AddContext <TContextType>(
            Action <DbContextOptionsBuilder> dbContextOptions = null,
            string contextName = "Default")
            where TContextType : SapphireDbContext
        {
            DbContextTypeContainer contextTypes = (DbContextTypeContainer)serviceCollection
                                                  .FirstOrDefault(s => s.ServiceType == typeof(DbContextTypeContainer))?.ImplementationInstance;

            // ReSharper disable once PossibleNullReferenceException
            contextTypes.AddContext(contextName, typeof(TContextType));

            serviceCollection.AddDbContext <TContextType>(dbContextOptions, ServiceLifetime.Transient);

            return(this);
        }
Exemple #4
0
        public SyncManager(IServiceProvider serviceProvider, ILogger <SyncManager> logger, DbContextTypeContainer contextTypeContainer, SyncContext syncContext)
        {
            this.logger = logger;
            this.contextTypeContainer = contextTypeContainer;
            this.syncContext          = syncContext;
            sapphireSyncModule        = (ISapphireSyncModule)serviceProvider.GetService(typeof(ISapphireSyncModule));

            if (sapphireSyncModule != null)
            {
                sapphireSyncModule.SyncRequestRequestReceived += request =>
                {
                    if (request.OriginId == syncContext.SessionId)
                    {
                        return;
                    }

                    if (request.Propagate)
                    {
                        Publish(request);
                    }

                    if (request is SendChangesRequest sendChangesRequest)
                    {
                        Type dbType = contextTypeContainer.GetContext(sendChangesRequest.DbName);

                        if (dbType != null)
                        {
                            SapphireChangeNotifier changeNotifier =
                                (SapphireChangeNotifier)serviceProvider.GetService(typeof(SapphireChangeNotifier));
                            logger.LogInformation("Handling changes from other server");
                            logger.LogDebug(
                                "Handling {changeCount} changes of '{dbType}' from server with OriginId '{originId}'. Propagate: {propagate}",
                                sendChangesRequest.Changes.Count, dbType.Name,
                                sendChangesRequest.OriginId, sendChangesRequest.Propagate);

                            changeNotifier.HandleChanges(sendChangesRequest.Changes, dbType);
                        }
                    }
                    else if (request is SendMessageRequest sendMessageRequest)
                    {
                        SapphireMessageSender sender =
                            (SapphireMessageSender)serviceProvider.GetService(typeof(SapphireMessageSender));
                        logger.LogInformation("Handling message from other server");
                        logger.LogDebug(
                            "Handling message for filter '{filter}' from server with OriginId '{originId}'. Propagate: {propagate}",
                            sendMessageRequest.Filter, sendMessageRequest.OriginId, sendMessageRequest.Propagate);

                        sender.Send(sendMessageRequest.Message, sendMessageRequest.Filter,
                                    sendMessageRequest.FilterParameters, false);
                    }
                    else if (request is SendPublishRequest sendPublishRequest)
                    {
                        SapphireMessageSender sender =
                            (SapphireMessageSender)serviceProvider.GetService(typeof(SapphireMessageSender));
                        logger.LogInformation("Handling publish from other server");
                        logger.LogDebug(
                            "Handling publish to topic '{topic}' from server with OriginId '{originId}'. Retain: {retain}, Propagate: {propagate}",
                            sendPublishRequest.Topic, sendPublishRequest.OriginId, sendPublishRequest.Retain, sendPublishRequest.Propagate);

                        sender.Publish(sendPublishRequest.Topic, sendPublishRequest.Message, sendPublishRequest.Retain,
                                       false);
                    }
                };
            }
        }
Exemple #5
0
        public SyncManager(IServiceProvider serviceProvider, ILogger <SyncManager> logger, DbContextTypeContainer contextTypeContainer, SyncContext syncContext)
        {
            this.logger = logger;
            this.contextTypeContainer = contextTypeContainer;
            this.syncContext          = syncContext;
            sapphireSyncModule        = (ISapphireSyncModule)serviceProvider.GetService(typeof(ISapphireSyncModule));

            if (sapphireSyncModule != null)
            {
                sapphireSyncModule.SyncRequestRequestReceived += request =>
                {
                    if (request.OriginId == syncContext.SessionId)
                    {
                        return;
                    }

                    if (request.Propagate)
                    {
                        Publish(request);
                    }

                    if (request is SendChangesRequest sendChangesRequest)
                    {
                        Type dbType = contextTypeContainer.GetContext(sendChangesRequest.DbName);

                        if (dbType != null)
                        {
                            SapphireChangeNotifier changeNotifier =
                                (SapphireChangeNotifier)serviceProvider.GetService(typeof(SapphireChangeNotifier));
                            logger.LogInformation("Handling changes from other server");
                            logger.LogDebug(
                                "Handling {ChangeCount} changes of {DbType} from server with OriginId {OriginId}. Propagate: {Propagate}",
                                sendChangesRequest.Changes.Count, dbType.Name,
                                sendChangesRequest.OriginId, sendChangesRequest.Propagate);

                            sendChangesRequest.Changes.ForEach(change =>
                            {
                                KeyValuePair <Type, string> property = dbType.GetDbSetType(change.CollectionName);

                                JObject rawValue = change.Value as JObject;
                                change.Value     = rawValue?.ToObject(property.Key);

                                if (change.State == ChangeResponse.ChangeState.Modified && change.OriginalValue != null)
                                {
                                    JObject rawOriginalValue = change.OriginalValue as JObject;
                                    change.OriginalValue     = rawOriginalValue?.ToObject(property.Key);
                                }
                            });

                            changeNotifier.HandleChanges(sendChangesRequest.Changes, dbType);
                        }
                    }
                    else if (request is SendMessageRequest sendMessageRequest)
                    {
                        SapphireMessageSender sender =
                            (SapphireMessageSender)serviceProvider.GetService(typeof(SapphireMessageSender));
                        logger.LogInformation("Handling message from other server");
                        logger.LogDebug(
                            "Handling message for filter {Filter} from server with OriginId {OriginId}. Propagate: {Propagate}",
                            sendMessageRequest.Filter, sendMessageRequest.OriginId, sendMessageRequest.Propagate);

                        sender.Send(sendMessageRequest.Message, sendMessageRequest.Filter,
                                    sendMessageRequest.FilterParameters, false);
                    }
                    else if (request is SendPublishRequest sendPublishRequest)
                    {
                        SapphireMessageSender sender =
                            (SapphireMessageSender)serviceProvider.GetService(typeof(SapphireMessageSender));
                        logger.LogInformation("Handling publish from other server");
                        logger.LogDebug(
                            "Handling publish to topic {Topic} from server with OriginId {OriginId}. Retain: {Retain}, Propagate: {Propagate}",
                            sendPublishRequest.Topic, sendPublishRequest.OriginId, sendPublishRequest.Retain, sendPublishRequest.Propagate);

                        sender.Publish(sendPublishRequest.Topic, sendPublishRequest.Message, sendPublishRequest.Retain,
                                       false);
                    }
                };
            }
        }