Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/OhioBox.Storage.MySql.Tests/Bootstrap/Bootstrapper.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
using System.Configuration;
using System.Reflection;
using OhioBox.Storage.MySql.Bootstrap;
using OhioBox.Storage.MySql.Moranbernate;

namespace OhioBox.Storage.MySql.Tests.Bootstrap
{
public static class Bootstrapper
{
private static ISqlConnectionFactory _factory;
public static class Bootstrapper
{
private static ISqlConnectionFactory _factory;

public static ISqlConnectionFactory Init()
{
if (_factory != null) return _factory;
public static ISqlConnectionFactory Init()
{
if (_factory != null) return _factory;

MoranbernateInitializer.Initialize(typeof(Bootstrapper).GetTypeInfo().Assembly);
MoranbernateInitializer.Initialize(typeof(Bootstrapper).GetTypeInfo().Assembly);

var connectionString = ConfigurationManager.ConnectionStrings["MySql"].ConnectionString;

var factory = new SqlConnectionFactory(new DefaultMetricsReporter());
factory.Add(connectionString, MoranbernateInitializer.GetMappedTypes());

_factory = factory;
_factory = new SqlConnectionFactory(connectionString);

return _factory;
}
}
return _factory;
}
}
}
32 changes: 0 additions & 32 deletions src/OhioBox.Storage.MySql/Bootstrap/MoranbernateBootstrap.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/OhioBox.Storage.MySql/Bootstrap/Registration.cs

This file was deleted.

14 changes: 0 additions & 14 deletions src/OhioBox.Storage.MySql/ClassHasNotBeenRegisteredException.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/OhioBox.Storage.MySql/DictionaryExtentions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace OhioBox.Storage.MySql.Moranbernate
namespace OhioBox.Storage.MySql
{
public interface IPerfLogger<T>
{
Expand Down
5 changes: 0 additions & 5 deletions src/OhioBox.Storage.MySql/ISqlConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,5 @@ namespace OhioBox.Storage.MySql
public interface ISqlConnectionFactory
{
IDbConnection GetConnection<T>();
ISqlConnectionProvider GetConnectionProvider<T>();
IEnumerable<ISqlConnectionProvider> Providers { get; }
IList<KeyValuePair<string, string>> GetTypeFullNameAndAonnectionString();

void Add(string connectionString, IEnumerable<Type> types);
}
}
26 changes: 18 additions & 8 deletions src/OhioBox.Storage.MySql/Moranbernate/AggregatedQueryRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using OhioBox.Moranbernate.Querying;
using OhioBox.Storage.MySql.Bootstrap;


namespace OhioBox.Storage.MySql.Moranbernate
Expand All @@ -12,30 +13,39 @@ public interface IAggregatedQueryRunner<T>
IEnumerable<QueryResult<T>> QueryAggregated(Action<IQueryBuilder<T>> query = null);
}

internal class AggregatedQueryRunner<T> : IAggregatedQueryRunner<T>
public class AggregatedQueryRunner<T> : IAggregatedQueryRunner<T>
where T : class, new()
{
private readonly ISqlConnectionProvider _connectionProvider;
private readonly ISqlConnectionFactory _connectionFactory;
private readonly IPerfLogger<T> _perfLogger;
private readonly IMetricsReporter _metricsReporter;

private readonly string _space = $"Storage.Moranbernate.{typeof(T).Name}";

public AggregatedQueryRunner(ISqlConnectionFactory connectionFactory,
IPerfLogger<T> perfLogger,
IMetricsReporter metricsReporter)
IPerfLogger<T> perfLogger = null,
IMetricsReporter metricsReporter = null)
{
_perfLogger = perfLogger;
_metricsReporter = metricsReporter;
_connectionProvider = connectionFactory.GetConnectionProvider<T>();
_connectionFactory = connectionFactory;
_perfLogger = perfLogger ?? new DefaultPerfLogger<T>();
_metricsReporter = metricsReporter ?? new DefaultMetricsReporter();
}

public AggregatedQueryRunner(string connectionString,
IPerfLogger<T> perfLogger = null,
IMetricsReporter metricsReporter = null)
{
_perfLogger = perfLogger ?? new DefaultPerfLogger<T>();
_metricsReporter = metricsReporter ?? new DefaultMetricsReporter();
_connectionFactory = new SqlConnectionFactory(connectionString, _metricsReporter);
}

public IEnumerable<QueryResult<T>> QueryAggregated(Action<IQueryBuilder<T>> query = null)
{
try
{
using (_metricsReporter.Report($"{_space}.Aggregate"))
using (var connection = _connectionProvider.GetOpenConnection())
using (var connection = _connectionFactory.GetConnection<T>())
{
return connection
.QueryAggregated<T>(q =>
Expand Down
48 changes: 26 additions & 22 deletions src/OhioBox.Storage.MySql/Moranbernate/MoranbernateStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,46 @@
using System.Linq.Expressions;
using OhioBox.Moranbernate.Querying;
using OhioBox.Moranbernate.Utils;
using OhioBox.Storage.MySql.Bootstrap;

namespace OhioBox.Storage.MySql.Moranbernate
{
public class MoranbernateStorage<T> : IStorage<T>
where T : class, new()
{
private readonly ISqlConnectionProvider _connectionProvider;
private readonly ISqlConnectionFactory _connectionFactory;
private readonly IPerfLogger<T> _perfLogger;
private readonly IMetricsReporter _metricsReporter;

private readonly string _space = $"Storage.Moranbernate.{typeof(T).Name}";
private const int QueryRuntimeThreshold = 1000;


public MoranbernateStorage(ISqlConnectionFactory connectionFactory,
IPerfLogger<T> perfLogger,
IMetricsReporter metricsReporter)
IPerfLogger<T> perfLogger = null,
IMetricsReporter metricsReporter = null)
{
_perfLogger = perfLogger;
_metricsReporter = metricsReporter;
_connectionProvider = connectionFactory.GetConnectionProvider<T>();
_connectionFactory = connectionFactory;
_perfLogger = perfLogger ?? new DefaultPerfLogger<T>();
_metricsReporter = metricsReporter ?? new DefaultMetricsReporter();
}

public QueryResults<TResult> RunQuery<TResult>(Func<IQueryable<T>, IQueryable<TResult>> queryOverQueryable)
public MoranbernateStorage(string connectionString,
IPerfLogger<T> perfLogger = null,
IMetricsReporter metricsReporter = null)
{
throw new NotImplementedException();
_perfLogger = perfLogger ?? new DefaultPerfLogger<T>();
_metricsReporter = metricsReporter ?? new DefaultMetricsReporter();
_connectionFactory = new SqlConnectionFactory(connectionString, _metricsReporter);
}

public int Count(Action<IQueryBuilder<T>> queryManipulator)
{
using (_metricsReporter.Report($"{_space}.Count"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
var count = conn.Count<T>(restrictable => {
if (queryManipulator != null)
queryManipulator(new MoranbernateRestrictions<T>(restrictable));
var count = conn.Count<T>(restrictable =>
{
queryManipulator?.Invoke(new MoranbernateRestrictions<T>(restrictable));
});
return Convert.ToInt32(count);
}
Expand All @@ -52,7 +56,7 @@ public void Delete(IList<T> ts)
if (ts.Count == 0)
return;
using (_metricsReporter.Report($"{_space}.Delete"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
foreach (var t in ts)
conn.Delete(t);
Expand All @@ -62,7 +66,7 @@ public void Delete(IList<T> ts)
public T GetById(object t)
{
using (_metricsReporter.Report($"{_space}.GetByid"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
return conn.GetById<T>(t);
}
Expand All @@ -82,7 +86,7 @@ public IList<T> Query(Action<IQueryBuilder<T>> queryManipulator)
public void Add(T t)
{
using (_metricsReporter.Report($"{_space}.Add"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
conn.Insert(t);
}
Expand All @@ -91,7 +95,7 @@ public void Add(T t)
public void Update(T t)
{
using (_metricsReporter.Report($"{_space}.Update"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
conn.Update(t);
}
Expand All @@ -100,7 +104,7 @@ public void Update(T t)
public void Delete(T t)
{
using (_metricsReporter.Report($"{_space}.Delete"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
conn.Delete(t);
}
Expand All @@ -109,7 +113,7 @@ public void Delete(T t)
public void Save(T t)
{
using (_metricsReporter.Report($"{_space}.Save"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
conn.Upsert(t);
}
Expand Down Expand Up @@ -165,7 +169,7 @@ private TR ExecuteQuery<TR>(Func<IDbConnection, (TR result, int rows)> action, s
var rows = 0;
try
{
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
var result = action(conn);
rows = result.rows;
Expand All @@ -190,7 +194,7 @@ private void LogSlowQuery(long elapsedMilliseconds, int rowsReturned, string mes
public void Add(IList<T> ts)
{
using (_metricsReporter.Report($"{_space}.BulkInsert"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
conn.BulkInsert(ts);
}
Expand All @@ -199,7 +203,7 @@ public void Add(IList<T> ts)
public void Save(IList<T> ts)
{
using (_metricsReporter.Report($"{_space}.BulkUpsert"))
using (var conn = _connectionProvider.GetOpenConnection())
using (var conn = _connectionFactory.GetConnection<T>())
{
conn.BulkUpsert(ts);
}
Expand Down
Loading