public void RunBeforeAnyTests() { using (var mySqlConnection = new MySqlConnection("Server=localhost;Port=3306;uid=root;password=password!")) { mySqlConnection.Execute(string.Format("CREATE DATABASE IF NOT EXISTS `{0}`", DatabaseName)); } Container = new Castle.Windsor.WindsorContainer(); DapperConfiguration .Use() .UseClassMapper(typeof(AutoClassMapper <>)) .UseContainer <ContainerForWindsor>(cfg => cfg.UseExisting(Container)) .UseSqlDialect(new MySqlDialect()) .WithDefaultConnectionStringNamed("__DefaultMySql") .FromAssembly("Dapper.Extensions.Linq.Test.Entities") .FromAssembly("Dapper.Extensions.Linq.Test.Maps") .Build(); var connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["__DefaultMySql"].ConnectionString); var files = new List <string> { ReadScriptFile("CreateAnimalTable"), ReadScriptFile("CreateFooTable"), ReadScriptFile("CreateMultikeyTable"), ReadScriptFile("CreatePersonTable"), ReadScriptFile("CreateCarTable"), ReadScriptFile("CreatePhoneTable") }; foreach (var setupFile in files) { connection.Execute(setupFile); } }
public static void AddData(this IServiceCollection services, IConfiguration configuration) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } IDapperConfiguration getConfiguration(ILoggerFactory loggerFactory) => DapperConfiguration .Use(GetAllConnectionStrings(configuration), loggerFactory) .UseClassMapper(typeof(AutoClassMapper <>)) .UseSqlDialect(new MySqlDialect()) .WithDefaultConnectionStringNamed("DefaultConnectionString") .FromAssemblies(GetEntityAssemblies()) .Build(); services.AddSingleton(x => getConfiguration(x.GetRequiredService <ILoggerFactory>())); services.AddSingleton <IConnectionStringProvider, StaticConnectionStringProvider>(); services.AddSingleton <IDapperSessionFactory, DapperSessionFactory>(); services.AddScoped <IDapperSessionContext, DapperSessionContext>(); services.AddScoped <ISqlGenerator, SqlGeneratorImpl>(); services.AddScoped <IDapperImplementor, DapperImplementor>(); services.AddScoped(typeof(IRepository <>), typeof(DapperRepository <>)); }
protected void AndGivenAUnitOfWork() { // instance be application scope var config = DapperConfiguration .Use() .UseSqlDialect(new SqlServerDialect()); var classMapper = new CustomEntityMap <MyEntity>(); classMapper.SetIdentityGenerated(m => m.Id); var classMappers = new List <IClassMapper> { classMapper }; classMappers.Each(m => config.Register(m)); // only one supported for the time being var tranCommitPolicy = new CommitPolicyNoOp(); this._unitOfWork = new UnitOfWorkLinq <MyEntity, int>( session => new RepositoryLinq <MyEntity, int>(session), new SessionFactory(_conString, config), tranCommitPolicy); }
protected override void RegisterTypes(IContainerRegistry containerRegistry) { // Application commands containerRegistry.Register <IApplicationCommands, ApplicationCommandsProxy>(); // Flyout service containerRegistry.RegisterInstance <IFlyoutService>(Container.Resolve <FlyoutService>()); // Localizer service // Localizer-Service LoggerFactory var = new LoggerFactory(); containerRegistry.RegisterInstance(typeof(ILocalizerService), new LocalizerService("de-DE")); var envBuilder = new ConfigurationBuilder().AddJsonFile("appsettings.json"); var envConfig = envBuilder.Build(); var dapperConfig = DapperConfiguration .Use(GetAllConnectionStrings(envConfig), var) .UseClassMapper(typeof(AutoClassMapper <>)) .UseSqlDialect(new MySqlDialect()) .WithDefaultConnectionStringNamed("DefaultConnectionString") .FromAssemblies(GetEntityAssemblies()) .Build(); containerRegistry.RegisterInstance(typeof(IDapperConfiguration), dapperConfig); containerRegistry.RegisterSingleton <IConnectionStringProvider, StaticConnectionStringProvider>(); containerRegistry.RegisterSingleton <IDapperSessionFactory, DapperSessionFactory>(); containerRegistry.Register <IDapperSessionContext, DapperSessionContext>(); containerRegistry.Register <ISqlGenerator, SqlGeneratorImpl>(); containerRegistry.Register <IDapperImplementor, DapperImplementor>(); containerRegistry.Register(typeof(IRepository <>), typeof(DapperRepository <>)); }
protected override void Load(ContainerBuilder builder) { base.Load(builder); //register dapper configuration builder.Register(m => { var classMappers = m.Resolve <IEnumerable <IClassMapper> >(); // instance be application scope var config = DapperConfiguration .Use() .UseSqlDialect(new SqlServerDialect()); classMappers.Each(n => config.Register(n)); return(config.Build()); }) .As <IDapperConfiguration>() .SingleInstance() .AsImplementedInterfaces() .PreserveExistingDefaults(); //regiser session factory builder.Register(m => { var componentContext = m.Resolve <IComponentContext>(); return(new SessionFactory(ConnectionString, componentContext.Resolve <IDapperConfiguration>())); }) .AsSelf() .AsImplementedInterfaces(); // guid key'ed entities builder.RegisterGeneric(typeof(RepositoryLinq <>)) .AsSelf() .As(typeof(IRepositoryLinq <>)) .As(typeof(IRepository <>)) .As(typeof(ILinqReader <>)) .Named("Default", typeof(IRepositoryLinq <>)) .Named("Default", typeof(IRepository <>)) .Named("Default", typeof(ILinqReader <>)); // generic key builder.RegisterGeneric(typeof(RepositoryLinq <,>)) .AsSelf() .As(typeof(IRepositoryLinq <,>)) .As(typeof(ILinqReader <,>)) .As(typeof(IRepository <,>)) .Named("Default", typeof(IRepositoryLinq <,>)) .Named("Default", typeof(IRepository <,>)) .Named("Default", typeof(ILinqReader <,>)); // use transactions builder.Register(m => new CommitPolicy()) .AsSelf() .AsImplementedInterfaces() .SingleInstance(); }
public void CanOpenAndCloseSession() { var subject = new SessionFactory(ConnectionString, DapperConfiguration.Use()); using ( var session = subject.Create()) { var tran = session.BeginTran(); tran.Commit(); } }
public void RunBeforeAnyTests() { Container = new Castle.Windsor.WindsorContainer(); DapperConfiguration .Use() .UseClassMapper(typeof(AutoClassMapper <>)) .UseContainer <ContainerForWindsor>(cfg => cfg.UseExisting(Container)) .UseSqlDialect(new SQLiteDialect()) .FromAssembly("Dapper.Extensions.Linq.Test.Entities") .Build(); }
private static void Init() { DapperConfiguration .Use() .UseClassMapper(typeof(AutoClassMapper <>)) .UseContainer <ContainerForWindsor>(cfg => cfg.UseExisting(_container)) .UseSqlDialect(new SqlServerDialect()) .WithDefaultConnectionStringNamed("__DefaultSqlServer") .FromAssembly("ConsoleApp.Domains") .FromAssembly("ConsoleApp.Maps") .Build(); }
public void RunBeforeAnyTests() { _container = new Castle.Windsor.WindsorContainer(); DapperConfiguration .Use() .UseClassMapper(typeof(AutoClassMapper <>)) .UseContainer <Dapper.Extensions.Linq.CastleWindsor.ContainerForWindsor>(c => c.UseExisting(_container)) .UseSqlDialect(new SqlServerDialect()) .WithDefaultConnectionStringNamed("__DefaultSqlServer") .Build(); }
public void RunBeforeAnyTests() { string connectionString = ConfigurationManager.ConnectionStrings["__DefaultSqlCe"].ConnectionString; string[] connectionParts = connectionString.Split(';'); string file = connectionParts .ToDictionary(k => k.Split('=')[0], v => v.Split('=')[1]) .Where(d => d.Key.Equals("Data Source", StringComparison.OrdinalIgnoreCase)) .Select(k => k.Value).Single(); if (File.Exists(file)) { File.Delete(file); } using (SqlCeEngine ce = new SqlCeEngine(connectionString)) { ce.CreateDatabase(); } Container = new Castle.Windsor.WindsorContainer(); DapperConfiguration .Use() .UseClassMapper(typeof(AutoClassMapper <>)) .UseContainer <ContainerForWindsor>(cfg => cfg.UseExisting(Container)) .UseSqlDialect(new SqlCeDialect()) .WithDefaultConnectionStringNamed("__DefaultSqlCe") .FromAssembly("Dapper.Extensions.Linq.Test.Entities") .FromAssembly("Dapper.Extensions.Linq.Test.Maps") .Build(); _connection = new SqlCeConnection(connectionString); var files = new List <string> { ReadScriptFile("CreateAnimalTable"), ReadScriptFile("CreateFooTable"), ReadScriptFile("CreateMultikeyTable"), ReadScriptFile("CreatePersonTable"), ReadScriptFile("CreateCarTable"), ReadScriptFile("CreatePhoneTable") }; foreach (var setupFile in files) { _connection.Execute(setupFile); } }
static UnitOfWorkManagesTransactionsStoryBase() { // can only be configured once per entity _config = DapperConfiguration .Use() .UseSqlDialect(new SqlServerDialect()); var classMapper = new CustomEntityMap <TestEntity>(); classMapper.SetIdentityGenerated(m => m.Id); var classMappers = new List <IClassMapper> { classMapper }; classMappers.Each(n => _config.Register(n)); }
protected void AndGivenADapperConfiguration() { // instance be application scope _config = DapperConfiguration .Use() .UseSqlDialect(new SqlServerDialect()); var classMapper = new CustomEntityMap <MyEntity>(); classMapper.SetIdentityGenerated(m => m.Id); var classMappers = new List <IClassMapper> { classMapper }; classMappers.Each(n => _config.Register(n)); }
public static Castle.Windsor.WindsorContainer Configuration() { using (var sqlConnection = new SqlConnection("Data Source=.;Integrated security=True;")) { const string sqlCreateDatabase = @" IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = '{0}') BEGIN CREATE DATABASE {0}; END "; sqlConnection.Execute(string.Format(sqlCreateDatabase, DatabaseName)); } var container = new Castle.Windsor.WindsorContainer(); DapperConfiguration .Use() .UseClassMapper(typeof(AutoClassMapper <>)) .UseContainer <ContainerForWindsor>(cfg => cfg.UseExisting(container)) .UseSqlDialect(new SqlServerDialect()) .WithDefaultConnectionStringNamed("__DefaultSqlServer") .FromAssembly("Dapper.Extensions.Linq.Test.Entities") .FromAssembly("Dapper.Extensions.Linq.Test.Maps") .Build(); var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["__DefaultSqlServer"].ConnectionString); var files = new List <string> { ReadScriptFile("CreateAnimalTable"), ReadScriptFile("CreateFooTable"), ReadScriptFile("CreateMultikeyTable"), ReadScriptFile("CreatePersonTable"), ReadScriptFile("CreateCarTable"), ReadScriptFile("CreatePhoneTable") }; foreach (var setupFile in files) { connection.Execute(setupFile); } return(container); }