Beispiel #1
0
        private SubscriptionDescription UpdateSubscription(
            string name,
            Guid uniqueKey,
            SubscriptionGroupDescription subscriptionGroupDescription,
            SubscriptionDescription subscriptionDescription)
        {
            using (var db = new Db.DataContext())
                using (var scope = db.CreateScope(System.Transactions.TransactionScopeOption.Suppress))
                {
                    var rowDb = db.Subscription.Where(x => x.UniqueKey == uniqueKey).FirstOrDefault();
                    if (rowDb == null)
                    {
                        rowDb = new Db.Subscription()
                        {
                            NameSubscription = name,
                            UniqueKey        = uniqueKey,
                            IdGroup          = subscriptionGroupDescription.Id
                        };
                        db.Subscription.Add(rowDb);
                        db.SaveChanges();
                    }

                    rowDb.NameSubscription = name;
                    rowDb.IdGroup          = subscriptionGroupDescription.Id;
                    db.SaveChanges();

                    subscriptionDescription.Id                = rowDb.IdSubscription;
                    subscriptionDescription.Name              = name;
                    subscriptionDescription.IsConfirmed       = true;
                    subscriptionDescription.SubscriptionGroup = subscriptionGroupDescription;
                }
            return(subscriptionDescription);
        }
Beispiel #2
0
        private SubscriptionGroupDescription UpdateSubscriptionGroup(SubscriptionGroupDescription subscriptionGroupDescription, string name, Guid uniqueKey)
        {
            using (var db = new Db.DataContext())
                using (var scope = db.CreateScope(System.Transactions.TransactionScopeOption.Suppress))
                {
                    var subscriptionGroupDb = db.SubscriptionGroup.Where(x => x.UniqueKey == uniqueKey).FirstOrDefault();
                    if (subscriptionGroupDb == null)
                    {
                        subscriptionGroupDb = new Db.SubscriptionGroup()
                        {
                            NameGroup = name,
                            UniqueKey = uniqueKey
                        };
                        db.SubscriptionGroup.Add(subscriptionGroupDb);
                        db.SaveChanges();
                    }

                    subscriptionGroupDb.NameGroup = name;
                    db.SaveChanges();

                    subscriptionGroupDescription.Id          = subscriptionGroupDb.IdGroup;
                    subscriptionGroupDescription.Name        = name;
                    subscriptionGroupDescription.IsConfirmed = true;
                    subscriptionGroupDescription.UniqueKey   = uniqueKey;
                }
            return(subscriptionGroupDescription);
        }
Beispiel #3
0
        public SubscriptionDescription RegisterSubscription <TSubscription>(string name, SubscriptionGroupDescription group, TSubscription instance)
            where TSubscription : SubscriptionBase <TSubscription>
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Имя не может быть пустым.", nameof(name));
            }
            if (group == null)
            {
                throw new ArgumentException("Не указана группа подписок.", nameof(group));
            }
            if (!_subscriptionGroups.TryGetValue(group.UniqueKey, out var subscriptionGroupDescription))
            {
                throw new ArgumentException("Указанная группа подписок не найдена.", nameof(group));
            }
            var typeInfo = OnUtils.Types.TypeHelpers.ExtractGenericType(typeof(TSubscription), typeof(SubscriptionBase <,>));

            if (typeInfo == null)
            {
                throw new ArgumentException($"Тип '{instance.GetType()}' должен реализовывать '{typeof(SubscriptionBase<,>)}'.", nameof(instance));
            }

            try
            {
                instance.OnRegisterBase();
                var uniqueKey = typeof(TSubscription).FullName.GenerateGuid();
                var result    = _subscriptions.AddOrUpdate(typeof(TSubscription),
                                                           key => new Tuple <object, SubscriptionDescription>(instance, UpdateSubscription(name, uniqueKey, subscriptionGroupDescription, new SubscriptionDescription())),
                                                           (key, old) => new Tuple <object, SubscriptionDescription>(instance, UpdateSubscription(name, uniqueKey, subscriptionGroupDescription, old.Item2))
                                                           );
                instance.SubscriptionDescription = result.Item2;

                var types = typeInfo.GetGenericArguments();
                typeof(SubscriptionsManager).
                GetMethod(nameof(OnRegister), BindingFlags.Instance | BindingFlags.NonPublic).
                MakeGenericMethod(types[0], types[1]).Invoke(this, new object[] { instance });

                return(result.Item2);
            }
            catch (Exception ex)
            {
                this.RegisterEvent(Journaling.EventType.CriticalError, "Ошибка во время регистрации подписки", null, ex);
                throw new Exception("Неожиданная ошибка во время регистрации подписки.");
            }
        }