HansMusterWhatElse
HansMusterWhatElse

Reputation: 671

ConcurrentDictonary to IQueryable<T> - Thread Safety

I assume the same logic from this question also applies to the following code snippet? Which would mean that the enumerator will never be a snapshot in time and thus an additional synchronization primitive would be needed. This of course doesn't apply if I interact with the dictionary directly.

public class InMemoryUserStore<TUser> :
        IQueryableUserStore<TUser>
        where TUser : User
    {
        private readonly ConcurrentDictionary<string, TUser> _users = new ConcurrentDictionary<string, TUser>();

        #region IQueryableUserStore<TUser>

        /// <summary>
        /// Return the dictionary as a <see cref="IQueryable{TUser}"/>.
        /// </summary>
        public IQueryable<TUser> Users => _users.Values.AsQueryable();

        #region CRUD

        /// <summary>
        /// Find a user by id.
        /// </summary>
        public Task<TUser> FindByIdAsync(string userId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var user = Users.FirstOrDefault(u => u.Id == userId);
            if (user != null)
            {
                return Task.FromResult(user);
            }

            return Task.FromResult<TUser>(null);
        }
}

Upvotes: 0

Views: 308

Answers (0)

Related Questions