Example #1
0
            public bool Register(string connectionId, Action <string[]> onChanged)
            {
                ConcurrentDictionary <string, IDisposable> dict;

                if (!Listeners.TryGetValue(typeof(TDomainObject), out dict))
                {
                    dict = new ConcurrentDictionary <string, IDisposable>();
                    if (!Listeners.TryAdd(typeof(TDomainObject), dict))
                    {
                        return(false);
                    }
                }
                var name     = typeof(TDomainObject).FullName;
                var listener = ChangeNotification.Track <TDomainObject>().Subscribe(kv => onChanged(kv.Key));

                if (!dict.TryAdd(connectionId, listener))
                {
                    listener.Dispose();
                }
                else
                {
                    ConcurrentBag <Type> bag;
                    if (!Connections.TryGetValue(connectionId, out bag))
                    {
                        bag = new ConcurrentBag <Type>();
                        Connections.TryAdd(connectionId, bag);
                    }
                    bag.Add(typeof(TDomainObject));
                }
                return(true);
            }
Example #2
0
            public bool Register(string connectionId, Type type, string specificationJson, Action <string> onMatched)
            {
                Func <TDomainObject, bool> isMatched;

                try
                {
                    ISpecification <TDomainObject> specification = (ISpecification <TDomainObject>)Newtonsoft.Json.JsonConvert.DeserializeObject(specificationJson, type);
                    isMatched = specification.IsSatisfied.Compile();
                }
                catch { return(false); }
                ConcurrentDictionary <string, IDisposable> dict;

                if (!Listeners.TryGetValue(typeof(TDomainObject), out dict))
                {
                    dict = new ConcurrentDictionary <string, IDisposable>();
                    if (!Listeners.TryAdd(typeof(TDomainObject), dict))
                    {
                        return(false);
                    }
                }
                var name     = typeof(TDomainObject).FullName;
                var listener = ChangeNotification.Track <TDomainObject>().Subscribe(kv =>
                {
                    foreach (var v in kv.Value.Value)
                    {
                        if (isMatched(v))
                        {
                            onMatched(v.URI);
                        }
                    }
                });

                if (!dict.TryAdd(connectionId, listener))
                {
                    listener.Dispose();
                }
                else
                {
                    ConcurrentBag <Type> bag;
                    if (!Connections.TryGetValue(connectionId, out bag))
                    {
                        bag = new ConcurrentBag <Type>();
                        Connections.TryAdd(connectionId, bag);
                    }
                    bag.Add(typeof(TDomainObject));
                }
                return(true);
            }