Exemplo n.º 1
0
        /// <inheritdoc />
        public IReadOnlyRepository <TEntity> GetReadOnlyRepository <TEntity>() where TEntity : class
        {
            Type entityType = typeof(TEntity);

            if (ReadOnlyRepositories.ContainsKey(entityType))
            {
                Logger?.LogDebug($"Get existing ReadOnlyRepository for entity {typeof(TEntity).Name}");
                return((IReadOnlyRepository <TEntity>)ReadOnlyRepositories[entityType]);
            }

            try
            {
                Logger?.LogDebug($"Get ReadOnlyRepository for entity {typeof(TEntity).Name} from services");
                IReadOnlyRepository <TEntity> customRepo = DbContext.GetService <IReadOnlyRepository <TEntity> >();
                ReadOnlyRepositories[entityType] = customRepo;
                return(customRepo);
            }
            catch
            {
                Logger?.LogDebug("Can't get ReadOnlyRepository from service provider");
            }
            Logger?.LogDebug($"Creating new ReadOnlyRepository for entity {typeof(TEntity).Name}");
            ReadOnlyRepositories[entityType] = new ReadOnlyRepository <TEntity>(DbContext);
            return((IReadOnlyRepository <TEntity>)ReadOnlyRepositories[entityType]);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 /// <param name="disposing">The disposing.</param>
 protected virtual void Dispose(bool disposing)
 {
     lock (SyncRoot)
     {
         if (!Disposed && disposing)
         {
             Logger?.LogTrace("Disposing {0}", GetType().GetFriendlyName());
             Repositories.Clear();
             ReadOnlyRepositories.Clear();
             DbContext.Dispose();
         }
         Disposed = true;
     }
 }