/// <summary> /// Initializes a new instance of the <see cref="RepositoryFactory" /> class. /// </summary> /// <param name="optionsAction">A builder action used to create or modify options for this repository factory.</param> public RepositoryFactory([NotNull] Action <RepositoryOptionsBuilder> optionsAction) { Guard.NotNull(optionsAction, nameof(optionsAction)); var optionsBuilder = new RepositoryOptionsBuilder(); optionsAction(optionsBuilder); _options = optionsBuilder.Options; }
/// <summary> /// Initializes a new instance of the <see cref="RepositoryOptions" /> class. /// </summary> /// <param name="options">The repository options to clone.</param> public RepositoryOptions(IRepositoryOptions options) { Guard.NotNull(options, nameof(options)); _interceptors = options.Interceptors.ToDictionary(x => x.Key, x => x.Value); _cachingProvider = options.CachingProvider; _loggerProvider = options.LoggerProvider; _mapperProvider = options.MapperProvider; _contextFactory = options.ContextFactory; _conventions = options.Conventions; }
private void Initialize([NotNull] IRepositoryOptions options) { Guard.NotNull(options, nameof(options)); var contextFactory = Guard.EnsureNotNull(options.ContextFactory, "No context provider has been configured for this unit of work."); _context = contextFactory.Create(); _transactionManager = _context.BeginTransaction(); // Re-configures the options and adds a shared context for the repositories to use _options = new RepositoryOptions(options).With(new RepositoryContextFactory(_context)); }
private static void TestThrowsIfUpdateWhenEntityNoInStore(IRepositoryOptions options) { var repo = new Repository <Customer>(options); var entity = new Customer { Name = "Random Name" }; var ex = Assert.Throws <InvalidOperationException>(() => repo.Update(entity)); Assert.Equal("Attempted to update or delete an entity that does not exist in the store.", ex.Message); }
/// <summary> /// Initializes a repository with the specified options /// </summary> /// <param name="logger">A generic interface for logging where the category name is derrived from the specified TCategoryName type name.</param> /// <param name="provider">Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param> /// <param name="options">The runtime options for this repository</param> public SqlServerRepository(ILogger <SqlServerRepository> logger, IServiceProvider provider, IRepositoryOptions options) { try { Logger = logger; ServiceProvider = provider; _options = options; _connection = new SqlConnection(_options.ConnectionString); _connection.Open(); } catch (Exception error) { throw new ApiException(HttpStatusCode.InternalServerError, new ApiError("connection_error", $"Cannot open database connection to {_options.ConnectionString}."), error); } }
/// <summary> /// Initializes a new instance of the <see cref="InMemoryRepository{T}" /> class. /// </summary> /// <param name="logger">The logger.</param> /// <param name="mediator">The mediator.</param> /// <param name="context">The context containing entities.</param> /// <param name="options">The options.</param> public InMemoryRepository( ILogger <IRepository <TEntity> > logger, IMediator mediator, InMemoryContext <TEntity> context, IRepositoryOptions options = null) { EnsureArg.IsNotNull(logger, nameof(logger)); EnsureArg.IsNotNull(mediator, nameof(mediator)); EnsureArg.IsNotNull(context, nameof(context)); this.logger = logger; this.mediator = mediator; this.context = context ?? new InMemoryContext <TEntity>(); this.Options = options; }
public InMemoryRepository( ILogger <IRepository <TEntity> > logger, IMediator mediator, Func <TDestination, object> idSelector, InMemoryContext <TEntity> context, IRepositoryOptions options = null, IEnumerable <ISpecificationMapper <TEntity, TDestination> > specificationMappers = null) : base(logger, mediator, context, options) { EnsureArg.IsNotNull(idSelector, nameof(idSelector)); EnsureArg.IsNotNull(options?.Mapper, nameof(options.Mapper)); //base.entities = entities.NullToEmpty().Select(d => this.Options.Mapper.Map<TEntity>(d)); this.specificationMappers = specificationMappers; this.idSelector = idSelector; }
public TestRepositoryInterceptorWithDependencyInjectedServices( IRepositoryOptions options, IRepositoryFactory repoFactory, IRepository <Customer> repo, IUnitOfWorkFactory uowFactory, IUnitOfWork uow) { if (options == null || repoFactory == null || repo == null || uowFactory == null || uow == null) { throw new ArgumentNullException(); } }
public EntityFrameworkRepository(TDbContext dbContext, IRepositoryOptions repositoryOptions) { if (dbContext == null) { throw new ArgumentNullException(nameof(dbContext)); } if (repositoryOptions == null) { throw new ArgumentNullException(nameof(repositoryOptions)); } this.repositoryOptions = repositoryOptions; DbContext = dbContext; DbSet = dbContext.Set <TEntity>(); }
/// <summary> /// Constructor /// </summary> /// <param name="memoryCache"></param> /// <param name="loggerFactory"></param> /// <param name="options"></param> public MongoFactory(IMemoryCache memoryCache, ILoggerFactory loggerFactory, IRepositoryOptions options) : base(memoryCache, loggerFactory, options) { if (!(options is IMongoDatabaseOptions mongoOptions)) { throw new Exception("Options should be of type IMongoDatabaseOptions"); } _logger = loggerFactory.CreateLogger(GetType()); try { MongoClient client; try { var mongoClientSettings = new MongoClientSettings { Server = MongoServerAddress.Parse(mongoOptions.ConnectionString), MaxConnectionIdleTime = TimeSpan.FromMinutes(1) }; client = new MongoClient(mongoClientSettings); } catch { client = new MongoClient(mongoOptions.ConnectionString); } var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); if (string.IsNullOrWhiteSpace(environment)) { environment = "Development"; } _mongoDatabase = client.GetDatabase($"{mongoOptions.DatabaseName}-{environment}"); Repositories = new ConcurrentDictionary <Type, object>(); } catch (Exception exception) { _logger.LogCritical( $"Error while connecting to {mongoOptions.ConnectionString}.", exception); throw; } }
/// <summary> /// Initializes a new instance of the <see cref="Repository{TEntity, TKey1, TKey2, TKey3}"/> class. /// </summary> /// <param name="options">The repository options.</param> public Repository([NotNull] IRepositoryOptions options) : base(options) { }
/// <summary> /// Initializes a new instance of the <see cref="UnitOfWork" /> class. /// </summary> /// <param name="options">The repository options.</param> public UnitOfWork([NotNull] IRepositoryOptions options) { Initialize(Guard.NotNull(options, nameof(options))); }
/// <summary> /// Initializes a new instance of the <see cref="RepositoryBase{TEntity, TKey}"/> class. /// </summary> /// <param name="options">The repository options.</param> public TestCustomerRepository(IRepositoryOptions options) : base(options) { }
/// <summary> /// Initializes a new instance of the <see cref="RepositoryOptionsBuilder"/> class. /// </summary> /// <param name="options">The repository options.</param> public RepositoryOptionsBuilder([NotNull] IRepositoryOptions options) { _options = new RepositoryOptions(Guard.NotNull(options, nameof(options))); }
public ClipboardRepository(IRepositoryOptions options) : base(options) { }
private static void TestThrowsIfBeginTransaction(IRepositoryOptions options) { var ex = Assert.Throws <NotSupportedException>(() => new UnitOfWork(options)); Assert.Equal("This context provider does not support transactions.", ex.Message); }
/// <summary> /// </summary> /// <param name="memoryCache"></param> /// <param name="loggerFactory"></param> /// <param name="options"></param> protected RepositoryFactoryBase(IMemoryCache memoryCache, ILoggerFactory loggerFactory, IRepositoryOptions options) { MemoryCache = memoryCache; LoggerFactory = loggerFactory; var logger = loggerFactory.CreateLogger(GetType()); if (!options.TimeLoggingEnabled) { return; } logger.LogDebug("Time logging is enabled in appsettings: logging time with the LogTimeDecorator will be started."); LogTime = true; }
private static void TestThrowsIfAddingEntityOfSameTypeWithSamePrimaryKeyValue(IRepositoryOptions options) { var repo = new Repository <Customer>(options); var entity = new Customer { Name = "Random Name" }; repo.Add(entity); var ex = Assert.Throws <InvalidOperationException>(() => repo.Add(entity)); Assert.Equal($"The instance of entity type '{typeof(Customer)}' cannot be added to the store because another instance of this type with the same key is already being tracked.", ex.Message); }
/// <summary> /// Constructor /// </summary> /// <param name="memoryCache"></param> /// <param name="loggerFactory"></param> /// <param name="options"></param> /// <param name="actorId"></param> public NoActionFactory(IMemoryCache memoryCache, ILoggerFactory loggerFactory, IRepositoryOptions options) : base(memoryCache, loggerFactory, options) { Repositories = new ConcurrentDictionary <Type, object>(); _logger = loggerFactory.CreateLogger(GetType()); }
public AsyncEntityFrameworkRepository(TDbContext dbContext, IRepositoryOptions repositoryOptions) : base(dbContext, repositoryOptions) { }
/// <summary> /// Initializes a new instance of the <see cref="ServiceFactory" /> class. /// </summary> /// <param name="options">The service options.</param> public ServiceFactory([NotNull] IRepositoryOptions options) : this(new UnitOfWorkFactory(Guard.NotNull(options, nameof(options)))) { }
/// <summary> /// Initializes a new instance of the <see cref="UnitOfWorkFactory" /> class. /// </summary> /// <param name="options">The repository options.</param> public UnitOfWorkFactory([NotNull] IRepositoryOptions options) { _options = Guard.NotNull(options, nameof(options)); }
public IServantRepository Create(IRepositoryOptions repositoryOptions) { var database = databaseFactory.Connect(repositoryOptions.ConnectionString, repositoryOptions.DatabaseName); return(new ServantRepository(database.GetCollection <Servant>(repositoryOptions.CollectionName))); }