public override void OnConfiguring(RepositoryOptionsBuilder builder) { builder .BuildConnection(new SqlConnection("server=risingup.life98.cn,55940;database=Lige;user=sa;password=!RisingupTech/././.;max pool size=300")) .BuildProvider(new MsSqlProvider()) .BuildAutoSyncStructure(true); }
public void ConfigureFromAppSetting() { var optionsBuilder = new RepositoryOptionsBuilder() .UseConfiguration(TestConfigurationHelper.GetConfiguration()); TestConfiguration(optionsBuilder); }
public void ThrowsIfContextProviderNotConfiguered() { var options = new RepositoryOptionsBuilder().Options; var ex = Assert.Throws <InvalidOperationException>(() => new Repository <Customer>(options)); Assert.Equal("No context provider has been configured. For more information on DotNetToolkit.Repository options configuration, visit the https://github.com/johelvisguzman/DotNetToolkit.Repository/wiki/Repository-Options-Configuration.", ex.Message); }
public void ConfigureFromAppConfig() { var optionsBuilder = new RepositoryOptionsBuilder() .UseConfiguration(); TestConfiguration(optionsBuilder); }
public void ConfigureFromRepositoryConfig() { var optionsBuilder = new RepositoryOptionsBuilder() .UseConfiguration("repository.config"); TestConfiguration(optionsBuilder); }
public void ConfigureInternalContextFactory() { var optionsBuilder = new RepositoryOptionsBuilder() .UseInMemoryDatabase(); Assert.NotNull(optionsBuilder.Options.ContextFactory); }
public void CantModifyUserOnInterceptionsWhenDisabled() { const string user = "******"; var entity = new CustomerWithTimeStamp(); var options = new RepositoryOptionsBuilder() .UseInMemoryDatabase() .UseInterceptor(new TestRepositoryTimeStampInterceptor(user)) .Options; var repo = new Repository <CustomerWithTimeStamp>(options) { InterceptorsEnabled = false }; Assert.Null(entity.CreateTime); Assert.Null(entity.ModTime); Assert.Null(entity.CreateUser); Assert.Null(entity.ModUser); repo.Add(entity); Assert.Null(entity.CreateTime); Assert.Null(entity.ModTime); Assert.Null(entity.CreateUser); Assert.Null(entity.ModUser); }
public async Task CantModifyUserOnInterceptionsWhenDisabledByTypeAsync() { const string user = "******"; var entity = new CustomerWithTimeStamp(); var options = new RepositoryOptionsBuilder() .UseInMemoryDatabase() .UseInterceptor(new TestRepositoryTimeStampInterceptor(user)) .Options; var repo = new Repository <CustomerWithTimeStamp>(options) { InterceptorsEnabled = true }; repo.InterceptorTypesDisabled.Add(typeof(TestRepositoryTimeStampInterceptor), true); Assert.Null(entity.CreateTime); Assert.Null(entity.ModTime); Assert.Null(entity.CreateUser); Assert.Null(entity.ModUser); await repo.AddAsync(entity); Assert.Null(entity.CreateTime); Assert.Null(entity.ModTime); Assert.Null(entity.CreateUser); Assert.Null(entity.ModUser); }
private static void TestConfiguration(RepositoryOptionsBuilder optionsBuilder) { // is configured Assert.True(optionsBuilder.IsConfigured); // context factory Assert.NotNull(optionsBuilder.Options.ContextFactory); var context = optionsBuilder.Options.ContextFactory.Create() as IInMemoryRepositoryContext; Assert.NotNull(context); Assert.Equal("__InMemoryDatabaseName__", context.DatabaseName); // logging provider Assert.NotNull(optionsBuilder.Options.LoggerProvider); // caching provider Assert.NotNull(optionsBuilder.Options.CachingProvider); Assert.Equal(((TestCacheProvider)optionsBuilder.Options.CachingProvider).Expiry, TimeSpan.FromSeconds(30)); // interceptor Assert.Single(optionsBuilder.Options.Interceptors); Assert.True(optionsBuilder.Options.Interceptors.ContainsKey(typeof(TestRepositoryInterceptor))); var interceptor = optionsBuilder.Options.Interceptors[typeof(TestRepositoryInterceptor)].Value as TestRepositoryInterceptor; Assert.Equal("random param", interceptor.P1); Assert.True(interceptor.P2); }
/// <summary> /// 配置连接 /// </summary> /// <param name="builder"></param> public override void OnConfiguring(RepositoryOptionsBuilder builder) { builder.BuildConnection(x => new MySqlConnection(@"server=localhost;port=3306;user id=root;password=A5101264a;database=gc_fps_receivable;")) .BuildConnection(x => new MySqlConnection(@"server=192.168.87.141;port=63307;user id=fps_dbuser_dev;password=kxx44cuvlmjluqncju; persistsecurityinfo=True;database=gc_fps_receivable_dev_2021_0;SslMode=none"), "fps_2021") .BuildAutoSyncStructure(false) .BuildProvider(new MySqlProvider()); }
public void CanBeginNullTransactionWhenWarningIgnored() { var options = new RepositoryOptionsBuilder() .UseInMemoryDatabase(ignoreTransactionWarning: true) .Options; var uow = new UnitOfWork(options); }
/// <summary> /// Configures the context to use NHibernate. /// </summary> /// <param name="source">The repository options builder.</param> /// <returns>The same builder instance.</returns> /// <remarks> /// Configure NHibernate using the <c><hibernate-configuration></c> section /// from the application config file, if found, or the file <c>hibernate.cfg.xml</c> if the /// <c><hibernate-configuration></c> section not include the session-factory configuration. /// </remarks> public static RepositoryOptionsBuilder UseNHibernate([NotNull] this RepositoryOptionsBuilder source) { Guard.NotNull(source, nameof(source)); source.UseInternalContextFactory(new NHibernateRepositoryContextFactory()); return(source); }
/// <summary> /// Configures the context to use entity framework using an IOC container to resolve the <typeparamref name="TDbContext"/>. /// </summary> /// <param name="source">The repository options builder.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseEntityFramework <TDbContext>([NotNull] this RepositoryOptionsBuilder source) where TDbContext : DbContext { Guard.NotNull(source, nameof(source)); source.UseInternalContextFactory(new EfRepositoryContextFactory <TDbContext>()); return(source); }
/// <summary> /// Configures the context to connect to a in-memory database. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="ignoreTransactionWarning">If a transaction operation is requested, ignore any warnings since the in-memory provider does not support transactions.</param> /// <param name="ignoreSqlQueryWarning">If a SQL query is executed, ignore any warnings since the in-memory provider does not support SQL query execution.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseInMemoryDatabase([NotNull] this RepositoryOptionsBuilder source, bool ignoreTransactionWarning = false, bool ignoreSqlQueryWarning = false) { Guard.NotNull(source, nameof(source)); source.UseInternalContextFactory(new InMemoryRepositoryContextFactory(ignoreTransactionWarning, ignoreSqlQueryWarning)); return(source); }
/// <summary> /// Configures the caching provider to use microsoft's in-memory cache. /// </summary> /// <param name="source">The repository options builder.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseInMemoryCache([NotNull] this RepositoryOptionsBuilder source) { Guard.NotNull(source, nameof(source)); source.UseCachingProvider(new InMemoryCacheProvider()); return(source); }
/// <summary> /// Configures the context to use entity framework with a connection string. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="nameOrConnectionString">Either the database name or a connection string.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseEntityFramework <TDbContext>([NotNull] this RepositoryOptionsBuilder source, [NotNull] string nameOrConnectionString) where TDbContext : DbContext { Guard.NotNull(source, nameof(source)); Guard.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString)); source.UseInternalContextFactory(new EfRepositoryContextFactory <TDbContext>(nameOrConnectionString)); return(source); }
/// <summary> /// Configures the context to use the azure storage blob service with a connection string. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="nameOrConnectionString">Either the database name or a connection string.</param> /// <param name="createIfNotExists">Creates the container if it does not exist.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseAzureStorageBlob([NotNull] this RepositoryOptionsBuilder source, [NotNull] string nameOrConnectionString, bool createIfNotExists = false) { Guard.NotNull(source, nameof(source)); Guard.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString)); source.UseInternalContextFactory(new AzureStorageBlobRepositoryContextFactory(nameOrConnectionString, createIfNotExists)); return(source); }
/// <summary> /// Configures the context to use ado.net with a connection string. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="nameOrConnectionString">Either the database name or a connection string.</param> /// <param name="ensureDatabaseCreated"> /// Ensures that the database for the context exists. If it exists, no action is taken. /// If it does not exist then the database and all its schema are created. /// If the database exists, then no effort is made to ensure it is compatible with the model for this context. /// </param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseAdoNet([NotNull] this RepositoryOptionsBuilder source, [NotNull] string nameOrConnectionString, bool ensureDatabaseCreated = false) { Guard.NotNull(source, nameof(source)); Guard.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString)); source.UseInternalContextFactory(new AdoNetRepositoryContextFactory(nameOrConnectionString, ensureDatabaseCreated)); return(source); }
public void ConfigureInterceptor() { var optionsBuilder = new RepositoryOptionsBuilder() .UseInterceptor(new TestRepositoryInterceptor("Random Param", false)); Assert.Single(optionsBuilder.Options.Interceptors); Assert.True(optionsBuilder.Options.Interceptors.ContainsKey(typeof(TestRepositoryInterceptor))); }
/// <summary> /// Configures the context to connect to a named XML formatted database. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="path">The database directory to create.</param> /// <param name="ignoreTransactionWarning">If a transaction operation is requested, ignore any warnings since the context provider does not support transactions.</param> /// <param name="ignoreSqlQueryWarning">If a SQL query is executed, ignore any warnings since the in-memory provider does not support SQL query execution.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseXmlDatabase([NotNull] this RepositoryOptionsBuilder source, [NotNull] string path, bool ignoreTransactionWarning = false, bool ignoreSqlQueryWarning = false) { Guard.NotNull(source, nameof(source)); Guard.NotEmpty(path, nameof(path)); source.UseInternalContextFactory(new XmlRepositoryContextFactory(path, ignoreTransactionWarning, ignoreSqlQueryWarning)); return(source); }
/// <summary> /// Configures the context to use ado.net with an existing connection. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="existingConnection">The existing connection.</param> /// <param name="ensureDatabaseCreated"> /// Ensures that the database for the context exists. If it exists, no action is taken. /// If it does not exist then the database and all its schema are created. /// If the database exists, then no effort is made to ensure it is compatible with the model for this context. /// </param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseAdoNet([NotNull] this RepositoryOptionsBuilder source, [NotNull] DbConnection existingConnection, bool ensureDatabaseCreated = false) { Guard.NotNull(source, nameof(source)); Guard.NotNull(existingConnection, nameof(existingConnection)); source.UseInternalContextFactory(new AdoNetRepositoryContextFactory(existingConnection, ensureDatabaseCreated)); return(source); }
/// <summary> /// Initializes a new instance of the <see cref="UnitOfWorkFactory" /> class. /// </summary> /// <param name="optionsAction">A builder action used to create or modify options for this unit of work factory.</param> public UnitOfWorkFactory([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="UnitOfWork" /> class. /// </summary> /// <param name="optionsAction">A builder action used to create or modify options for this unit of work.</param> public UnitOfWork([NotNull] Action <RepositoryOptionsBuilder> optionsAction) { Guard.NotNull(optionsAction, nameof(optionsAction)); var optionsBuilder = new RepositoryOptionsBuilder(); optionsAction(optionsBuilder); Initialize(optionsBuilder.Options); }
/// <summary> /// Configures the context to use the azure storage blob service with a connection string. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="connectionString">The connection string.</param> /// <param name="createIfNotExists">Creates the container if it does not exist.</param> /// <param name="serializerSettings">The serializer options to use when serializing to JSON.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseAzureStorageBlob([NotNull] this RepositoryOptionsBuilder source, [NotNull] string connectionString, bool createIfNotExists = false, JsonSerializerSettings serializerSettings = null) { Guard.NotNull(source, nameof(source)); Guard.NotEmpty(connectionString, nameof(connectionString)); source.UseInternalContextFactory( new AzureStorageBlobRepositoryContextFactory(connectionString, createIfNotExists, serializerSettings)); return(source); }
public void ConfigureMultipleInterceptorsOfDifferentType() { var optionsBuilder = new RepositoryOptionsBuilder() .UseInterceptor(new TestRepositoryInterceptor("Random Param", false)) .UseInterceptor(new TestRepositoryInterceptor2()); Assert.Equal(2, optionsBuilder.Options.Interceptors.Count()); Assert.True(optionsBuilder.Options.Interceptors.ContainsKey(typeof(TestRepositoryInterceptor))); Assert.True(optionsBuilder.Options.Interceptors.ContainsKey(typeof(TestRepositoryInterceptor2))); }
public void CanExecuteQueryWhenWarningIgnored() { var options = new RepositoryOptionsBuilder() .UseInMemoryDatabase(ignoreTransactionWarning: false, ignoreSqlQueryWarning: true) .Options; var repo = new Repository <Customer>(options); repo.ExecuteSqlCommand("SELECT * FROM Customers"); repo.ExecuteSqlQuery("SELECT * FROM Customers"); }
private static void TestConfiguration(RepositoryOptionsBuilder optionsBuilder) { Assert.NotNull(optionsBuilder.Options.ContextFactory); Assert.NotNull(optionsBuilder.Options.LoggerProvider); Assert.NotNull(optionsBuilder.Options.CachingProvider); Assert.NotNull(optionsBuilder.Options.CachingProvider.Expiry); Assert.NotNull(optionsBuilder.Options.MapperProvider); Assert.Equal(1, optionsBuilder.Options.Interceptors.Count()); Assert.True(optionsBuilder.Options.Interceptors.ContainsKey(typeof(TestRepositoryInterceptor))); }
/// <summary> /// Configures the context to use entity framework core with a context options builder for configuring the context. /// </summary> /// <param name="source">The repository options builder.</param> /// <param name="optionsAction">The context options builder action.</param> /// <returns>The same builder instance.</returns> public static RepositoryOptionsBuilder UseEntityFrameworkCore <TDbContext>([NotNull] this RepositoryOptionsBuilder source, [NotNull] Action <DbContextOptionsBuilder> optionsAction) where TDbContext : DbContext { Guard.NotNull(source, nameof(source)); Guard.NotNull(optionsAction, nameof(optionsAction)); var optionsBuilder = new DbContextOptionsBuilder <TDbContext>(); optionsAction(optionsBuilder); return(UseEntityFrameworkCore <TDbContext>(source, optionsBuilder.Options)); }
public void ConfigureFromJson() { var optionsBuilder = new RepositoryOptionsBuilder(); Assert.False(optionsBuilder.IsConfigured); optionsBuilder.UseConfiguration(TestConfigurationHelper.GetConfiguration()); Assert.True(optionsBuilder.IsConfigured); TestConfiguration(optionsBuilder); }
public void ConfigureLoggingProvider() { var optionsBuilder = new RepositoryOptionsBuilder(); Assert.False(optionsBuilder.IsConfigured); optionsBuilder.UseLoggerProvider(new ConsoleLoggerProvider(LogLevel.Debug)); Assert.True(optionsBuilder.IsConfigured); Assert.NotNull(optionsBuilder.Options.LoggerProvider); }