Tuesday, March 5, 2019

Use Dapper to map to an object what comes back from a stored procedure assuming the property names are identical.

Go to: Tools > NuGet Package Manager > Manage NuGet Packages for Solution... ...in Visual Studio Preview 2019. Then click the "Browse" tab and search for "Dapper" to install the Dapper microORM at your application. Use it like so in C#:

using MyThing.Core.ExternalDependencies.Repositories;
using MyThing.Core.Objects;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Dapper;
namespace MyThing.Infrastructure.ExternalDependencies.Repositories
{
   public class FooRepository : IFooRepository
   {
      public IEnumerable<Foo> GetFoos(string connectionString)
      {
         using (IDbConnection db = new SqlConnection(connectionString))
         {
            return db.Query<Foo>("EXEC myStoredProcedure");
         }
      }
   }
}

No comments:

Post a Comment