using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using DapperExtensions.Sql; using DapperExtensions.Mapper; using System.Data.Common; namespace DapperExtensions { public static class DapperExtensions { private readonly static object _lock = new object(); private static Func _instanceFactory; private static IDapperImplementor _instance; private static IDapperExtensionsConfiguration _configuration; /// /// Gets or sets the default class mapper to use when generating class maps. If not specified, AutoClassMapper is used. /// DapperExtensions.Configure(Type, IList, ISqlDialect) can be used instead to set all values at once /// public static Type DefaultMapper { get { return _configuration.DefaultMapper; } set { Configure(value, _configuration.MappingAssemblies, _configuration.Dialect); } } /// /// Gets or sets the type of sql to be generated. /// DapperExtensions.Configure(Type, IList, ISqlDialect) can be used instead to set all values at once /// public static ISqlDialect SqlDialect { get { return _configuration.Dialect; } set { Configure(_configuration.DefaultMapper, _configuration.MappingAssemblies, value); } } /// /// Get or sets the Dapper Extensions Implementation Factory. /// public static Func InstanceFactory { get { if (_instanceFactory == null) { _instanceFactory = config => new DapperImplementor(new SqlGeneratorImpl(config)); } return _instanceFactory; } set { _instanceFactory = value; Configure(_configuration.DefaultMapper, _configuration.MappingAssemblies, _configuration.Dialect); } } /// /// Gets the Dapper Extensions Implementation /// private static IDapperImplementor Instance { get { if (_instance == null) { lock (_lock) { if (_instance == null) { _instance = InstanceFactory(_configuration); } } } return _instance; } } static DapperExtensions() { Configure(typeof(AutoClassMapper<>), new List(), new SqlServerDialect()); } /// /// Add other assemblies that Dapper Extensions will search if a mapping is not found in the same assembly of the POCO. /// /// public static void SetMappingAssemblies(IList assemblies) { Configure(_configuration.DefaultMapper, assemblies, _configuration.Dialect); } /// /// Configure DapperExtensions extension methods. /// /// /// /// public static void Configure(IDapperExtensionsConfiguration configuration) { _instance = null; _configuration = configuration; } /// /// Configure DapperExtensions extension methods. /// /// /// /// public static void Configure(Type defaultMapper, IList mappingAssemblies, ISqlDialect sqlDialect) { Configure(new DapperExtensionsConfiguration(defaultMapper, mappingAssemblies, sqlDialect)); } /// /// Executes a query for the specified id, returning the data typed as per T /// public static T Get(this DbConnection connection, dynamic id, DbTransaction transaction = null, int? commandTimeout = null) where T : class { var result = Instance.Get(connection, id, transaction, commandTimeout); return (T)result; } /// /// Executes an insert query for the specified entity. /// public static void Insert(this DbConnection connection, IEnumerable entities, DbTransaction transaction = null, int? commandTimeout = null) where T : class { Instance.Insert(connection, entities, transaction, commandTimeout); } /// /// Executes an insert query for the specified entity, returning the primary key. /// If the entity has a single key, just the value is returned. /// If the entity has a composite key, an IDictionary<string, object> is returned with the key values. /// The key value for the entity will also be updated if the KeyType is a Guid or Identity. /// public static dynamic Insert(this DbConnection connection, T entity, DbTransaction transaction = null, int? commandTimeout = null) where T : class { return Instance.Insert(connection, entity, transaction, commandTimeout); } /// /// Executes an update query for the specified entity. /// public static bool Update(this DbConnection connection, T entity, DbTransaction transaction = null, int? commandTimeout = null) where T : class { return Instance.Update(connection, entity, transaction, commandTimeout); } /// /// Executes a delete query for the specified entity. /// public static bool Delete(this DbConnection connection, T entity, DbTransaction transaction = null, int? commandTimeout = null) where T : class { return Instance.Delete(connection, entity, transaction, commandTimeout); } /// /// Executes a delete query using the specified predicate. /// public static bool Delete(this DbConnection connection, object predicate, DbTransaction transaction = null, int? commandTimeout = null) where T : class { return Instance.Delete(connection, predicate, transaction, commandTimeout); } /// /// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T. /// public static IEnumerable GetList(this DbConnection connection, object predicate = null, IList sort = null, DbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class { return Instance.GetList(connection, predicate, sort, transaction, commandTimeout, buffered); } /// /// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T. /// Data returned is dependent upon the specified page and resultsPerPage. /// public static IEnumerable GetPage(this DbConnection connection, object predicate, IList sort, int page, int resultsPerPage, DbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class { return Instance.GetPage(connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered); } /// /// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T. /// Data returned is dependent upon the specified firstResult and maxResults. /// public static IEnumerable GetSet(this DbConnection connection, object predicate, IList sort, int firstResult, int maxResults, DbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class { return Instance.GetSet(connection, predicate, sort, firstResult, maxResults, transaction, commandTimeout, buffered); } /// /// Executes a query using the specified predicate, returning an integer that represents the number of rows that match the query. /// public static int Count(this DbConnection connection, object predicate, DbTransaction transaction = null, int? commandTimeout = null) where T : class { return Instance.Count(connection, predicate, transaction, commandTimeout); } /// /// Executes a select query for multiple objects, returning IMultipleResultReader for each predicate. /// public static IMultipleResultReader GetMultiple(this DbConnection connection, GetMultiplePredicate predicate, DbTransaction transaction = null, int? commandTimeout = null) { return Instance.GetMultiple(connection, predicate, transaction, commandTimeout); } /// /// Gets the appropriate mapper for the specified type T. /// If the mapper for the type is not yet created, a new mapper is generated from the mapper type specifed by DefaultMapper. /// public static IClassMapper GetMap() where T : class { return Instance.SqlGenerator.Configuration.GetMap(); } /// /// Clears the ClassMappers for each type. /// public static void ClearCache() { Instance.SqlGenerator.Configuration.ClearCache(); } /// /// Generates a COMB Guid which solves the fragmented index issue. /// See: http://davybrion.com/blog/2009/05/using-the-guidcomb-identifier-strategy /// public static Guid GetNextGuid() { return Instance.SqlGenerator.Configuration.GetNextGuid(); } } }