예제 #1
0
        /// <summary>
        /// Gets an exclusive object on the specified value, must be called in 'using' statement
        /// </summary>
        /// <example>
        /// Using(Sync.GetHandle(value))
        /// {
        ///     ....
        /// }
        /// </example>
        /// <param name="id">The value on which to acquire the new object.</param>
        /// <param name="generator">Method to create new object of T</param>
        /// <returns>Handle for lock object for K value</returns>
        public RepoHandle GetObject(TID id, Func <TObject> generator)
        {
            TObject    obj;
            RepoHandle handle;

            lock (this.repoObjects)
            {
                if (!this.repoObjects.TryGetValue(id, out obj))
                {
                    obj = generator();
                    this.repoObjects.Add(id, obj);
                    this.repoHandles.Add(obj, new List <RepoHandle>());
                }

                handle = new RepoHandle(id, obj, this);
                this.repoHandles[obj].Add(handle);
            }

            return(handle);
        }
예제 #2
0
        private void Release(RepoHandle handle)
        {
            IDisposable disposeObject = null;

            lock (this.repoObjects)
            {
                var handles = this.repoHandles[handle.Object];
                handles.Remove(handle);

                if (handles.Count == 0)
                {
                    this.repoObjects.Remove(handle.ID);
                    this.repoHandles.Remove(handle.Object);

                    disposeObject = handle.Object as IDisposable;
                }
            }

            if (disposeObject != null)
            {
                disposeObject.Dispose();
            }
        }