示例#1
0
        public void SameType()
        {
            var expected = _factory.GetSemaphore <Person>();
            var actual   = _factory.GetSemaphore <Person>();

            Assert.NotNull(expected);
            Assert.Equal(expected, actual);
        }
示例#2
0
        public void SameFactory()
        {
            var semaphore1 = _factory.GetSemaphore <Person>();
            var semaphore2 = _factory.GetSemaphore <Person>();

            // wait for a small task
            var released = SimulateFileOperation(
                semaphore1, 2000, semaphore2, 4000);

            // must be the same instance and receive a signal
            Assert.Equal(semaphore1, semaphore2);
            Assert.True(released);
        }
示例#3
0
        public async Task SaveAsync(T item)
        {
            var semaphore = _semaphoreFactory.GetSemaphore <T>();

            try
            {
                semaphore.WaitOne();
                await SaveToFileAsync(item);
            }
            finally
            {
                semaphore.Release();
            }
        }
        /// <summary>
        ///     Creates a new instance with the given options.
        /// </summary>
        /// <param name="options">The options for this repository.</param>
        /// <param name="semaphoreFactory">The semaphore factory.</param>
        public ConcurrentJsonRepository(JsonStoreOptions options, ISemaphoreFactory semaphoreFactory) : base(options)
        {
            var keyProperty = RepositoryKeyValidator.GetKeyProperty <T, TKey>();

            GetKeyValue = keyProperty.Compile();

            _semaphore = semaphoreFactory.GetSemaphore <T>();
        }
        /// <summary>
        ///     Creates a new instance with the given options and key.
        /// </summary>
        /// <param name="options">The options for this repository.</param>
        /// <param name="keyProperty">A <see cref="Func{TResult}" /> to get the object's key.</param>
        /// <param name="semaphoreFactory">The semaphore factory.</param>
        public ConcurrentJsonRepository(
            JsonStoreOptions options,
            Expression <Func <T, TKey> > keyProperty,
            ISemaphoreFactory semaphoreFactory
            ) : base(options)
        {
            GetKeyValue = keyProperty.Compile();

            _semaphore = semaphoreFactory.GetSemaphore <T>();
        }