How do I handle Database Connections with Dapper in .NET?

前端 未结 9 2030
闹比i
闹比i 2020-12-07 07:52

I\'ve been playing with Dapper, but I\'m not sure of the best way to handle the database connection.

Most examples show the connection object being created in the

相关标签:
9条回答
  • 2020-12-07 08:21

    I wrap connection with the helper class:

    public class ConnectionFactory
    {
        private readonly string _connectionName;
    
        public ConnectionFactory(string connectionName)
        {
            _connectionName = connectionName;
        }
    
        public IDbConnection NewConnection() => new SqlConnection(_connectionName);
    
        #region Connection Scopes
    
        public TResult Scope<TResult>(Func<IDbConnection, TResult> func)
        {
            using (var connection = NewConnection())
            {
                connection.Open();
                return func(connection);
            }
        }
    
        public async Task<TResult> ScopeAsync<TResult>(Func<IDbConnection, Task<TResult>> funcAsync)
        {
            using (var connection = NewConnection())
            {
                connection.Open();
                return await funcAsync(connection);
            }
        }
    
        public void Scope(Action<IDbConnection> func)
        {
            using (var connection = NewConnection())
            {
                connection.Open();
                func(connection);
            }
        }
    
        public async Task ScopeAsync<TResult>(Func<IDbConnection, Task> funcAsync)
        {
            using (var connection = NewConnection())
            {
                connection.Open();
                await funcAsync(connection);
            }
        }
    
        #endregion Connection Scopes
    }
    

    Examples of usage:

    public class PostsService
    {
        protected IConnectionFactory Connection;
    
        // Initialization here ..
    
        public async Task TestPosts_Async()
        {
            // Normal way..
            var posts = Connection.Scope(cnn =>
            {
                var state = PostState.Active;
                return cnn.Query<Post>("SELECT * FROM [Posts] WHERE [State] = @state;", new { state });
            });
    
            // Async way..
            posts = await Connection.ScopeAsync(cnn =>
            {
                var state = PostState.Active;
                return cnn.QueryAsync<Post>("SELECT * FROM [Posts] WHERE [State] = @state;", new { state });
            });
        }
    }
    

    So I don't have to explicitly open the connection every time. Additionally, you can use it this way for the convenience' sake of the future refactoring:

    var posts = Connection.Scope(cnn =>
    {
        var state = PostState.Active;
        return cnn.Query<Post>($"SELECT * FROM [{TableName<Post>()}] WHERE [{nameof(Post.State)}] = @{nameof(state)};", new { state });
    });
    

    What is TableName<T>() can be found in this answer.

    0 讨论(0)
  • 2020-12-07 08:22

    Everyone appears to be opening their connections entirely too early? I had this same question, and after digging through the Source here - https://github.com/StackExchange/dapper-dot-net/blob/master/Dapper/SqlMapper.cs

    You will find that every interaction with the database checks the connection to see if it is closed, and opens it as necessary. Due to this, we simply utilize using statements like above without the conn.open(). This way the connection is opened as close to the interaction as possible. If you notice, it also immediately closes the connection. This will also be quicker than it closing automatically during disposal.

    One of the many examples of this from the repo above:

        private static int ExecuteCommand(IDbConnection cnn, ref CommandDefinition command, Action<IDbCommand, object> paramReader)
        {
            IDbCommand cmd = null;
            bool wasClosed = cnn.State == ConnectionState.Closed;
            try
            {
                cmd = command.SetupCommand(cnn, paramReader);
                if (wasClosed) cnn.Open();
                int result = cmd.ExecuteNonQuery();
                command.OnCompleted();
                return result;
            }
            finally
            {
                if (wasClosed) cnn.Close();
                cmd?.Dispose();
            }
        }
    

    Below is a small example of how we use a Wrapper for Dapper called the DapperWrapper. This allows us to wrap all of the Dapper and Simple Crud methods to manage connections, provide security, logging, etc.

      public class DapperWrapper : IDapperWrapper
      {
        public IEnumerable<T> Query<T>(string query, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
        {
          using (var conn = Db.NewConnection())
          {
              var results = conn.Query<T>(query, param, transaction, buffered, commandTimeout, commandType);
              // Do whatever you want with the results here
              // Such as Security, Logging, Etc.
              return results;
          }
        }
      }
    
    0 讨论(0)
  • 2020-12-07 08:22

    Hi @donaldhughes I'm new on it too, and I use to do this: 1 - Create a class to get my Connection String 2 - Call the connection string class in a Using

    Look:

    DapperConnection.cs

    public class DapperConnection
    {
    
        public IDbConnection DapperCon {
            get
            {
                return new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ToString());
    
            }
        }
    }
    

    DapperRepository.cs

      public class DapperRepository : DapperConnection
      {
           public IEnumerable<TBMobileDetails> ListAllMobile()
            {
                using (IDbConnection con = DapperCon )
                {
                    con.Open();
                    string query = "select * from Table";
                    return con.Query<TableEntity>(query);
                }
            }
         }
    

    And it works fine.

    0 讨论(0)
  • 2020-12-07 08:26

    It was asked about 4 years ago... but anyway, maybe the answer will be useful to someone here:

    I do it like this in all the projects. First, I create a base class which contains a few helper methods like this:

    public class BaseRepository
    {
        protected T QueryFirstOrDefault<T>(string sql, object parameters = null)
        {
            using (var connection = CreateConnection())
            {
                return connection.QueryFirstOrDefault<T>(sql, parameters);
            }
        }
    
        protected List<T> Query<T>(string sql, object parameters = null)
        {
            using (var connection = CreateConnection())
            {
                return connection.Query<T>(sql, parameters).ToList();
            }
        }
    
        protected int Execute(string sql, object parameters = null)
        {
            using (var connection = CreateConnection())
            {
                return connection.Execute(sql, parameters);
            }
        }
    
        // Other Helpers...
    
        private IDbConnection CreateConnection()
        {
            var connection = new SqlConnection(...);
            // Properly initialize your connection here.
            return connection;
        }
    }
    

    And having such a base class I can easily create real repositories without any boilerplate code:

    public class AccountsRepository : BaseRepository
    {
        public Account GetById(int id)
        {
            return QueryFirstOrDefault<Account>("SELECT * FROM Accounts WHERE Id = @Id", new { id });
        }
    
        public List<Account> GetAll()
        {
            return Query<Account>("SELECT * FROM Accounts ORDER BY Name");
        }
    
        // Other methods...
    }
    

    So all the code related to Dapper, SqlConnection-s and other database access stuff is located in one place (BaseRepository). All real repositories are clean and simple 1-line methods.

    I hope it will help someone.

    0 讨论(0)
  • 2020-12-07 08:33

    I created extension methods with a property that retrieves the connection string from configuration. This lets the callers not have to know anything about the connection, whether it's open or closed, etc. This method does limit you a bit since you're hiding some of the Dapper functionality, but in our fairly simple app it's worked fine for us, and if we needed more functionality from Dapper we could always add a new extension method that exposes it.

    internal static string ConnectionString = new Configuration().ConnectionString;
    
        internal static IEnumerable<T> Query<T>(string sql, object param = null)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                return conn.Query<T>(sql, param);
            }
        }
    
        internal static int Execute(string sql, object param = null)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                return conn.Execute(sql, param);
            }
        }
    
    0 讨论(0)
  • 2020-12-07 08:33

    Best practice is a real loaded term. I like a DbDataContext style container like Dapper.Rainbow promotes. It allows you to couple the CommandTimeout, transaction and other helpers.

    For example:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    using Dapper;
    
    // to have a play, install Dapper.Rainbow from nuget
    
    namespace TestDapper
    {
        class Program
        {
            // no decorations, base class, attributes, etc 
            class Product 
            {
                public int Id { get; set; }
                public string Name { get; set; }
                public string Description { get; set; }
                public DateTime? LastPurchase { get; set; }
            }
    
            // container with all the tables 
            class MyDatabase : Database<MyDatabase>
            {
                public Table<Product> Products { get; set; }
            }
    
            static void Main(string[] args)
            {
                var cnn = new SqlConnection("Data Source=.;Initial Catalog=tempdb;Integrated Security=True");
                cnn.Open();
    
                var db = MyDatabase.Init(cnn, commandTimeout: 2);
    
                try
                {
                    db.Execute("waitfor delay '00:00:03'");
                }
                catch (Exception)
                {
                    Console.WriteLine("yeah ... it timed out");
                }
    
    
                db.Execute("if object_id('Products') is not null drop table Products");
                db.Execute(@"create table Products (
                        Id int identity(1,1) primary key, 
                        Name varchar(20), 
                        Description varchar(max), 
                        LastPurchase datetime)");
    
                int? productId = db.Products.Insert(new {Name="Hello", Description="Nothing" });
                var product = db.Products.Get((int)productId);
    
                product.Description = "untracked change";
    
                // snapshotter tracks which fields change on the object 
                var s = Snapshotter.Start(product);
                product.LastPurchase = DateTime.UtcNow;
                product.Name += " World";
    
                // run: update Products set LastPurchase = @utcNow, Name = @name where Id = @id
                // note, this does not touch untracked columns 
                db.Products.Update(product.Id, s.Diff());
    
                // reload
                product = db.Products.Get(product.Id);
    
    
                Console.WriteLine("id: {0} name: {1} desc: {2} last {3}", product.Id, product.Name, product.Description, product.LastPurchase);
                // id: 1 name: Hello World desc: Nothing last 12/01/2012 5:49:34 AM
    
                Console.WriteLine("deleted: {0}", db.Products.Delete(product.Id));
                // deleted: True 
    
    
                Console.ReadKey();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题