Skip to content

Instantly share code, notes, and snippets.

@KerryRitter
Last active November 3, 2016 14:16
Show Gist options
  • Save KerryRitter/a7b00a37af2fbc11b3088f4b38c321b8 to your computer and use it in GitHub Desktop.
Save KerryRitter/a7b00a37af2fbc11b3088f4b38c321b8 to your computer and use it in GitHub Desktop.
Dapper.Contrib test
using System.Data.SqlClient;
using System.Configuration;
namespace DapperTest
{
public static class ContextFactory
{
public static SqlConnection Create() => new SqlConnection(ConfigurationManager.ConnectionStrings["MyCxn"].ConnectionString);
}
}
using System;
using Dapper.Contrib.Extensions;
namespace DapperTest.Models
{
[Table("Country")]
public class Country
{
[ExplicitKey]
public string CountryCode { get; set; }
public string Description { get; set; }
public DateTime CreateDate { get; set; }
public string CreateUser { get; set; }
public DateTime? LastUpdate { get; set; }
public string LastUser { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using Dapper.Contrib.Extensions;
using DapperTest.Models;
namespace DapperTest
{
public class CountryRepository
{
public List<Country> GetAll()
{
using (var context = ContextFactory.Create())
{
return context.GetAll<Country>().ToList();
}
}
public Country Get(string countryCode)
{
using (var context = ContextFactory.Create())
{
return context.Get<Country>(countryCode);
}
}
public void Insert(Country country)
{
using (var context = ContextFactory.Create())
{
context.Insert(country);
}
}
public void Insert(List<Country> countries)
{
using (var context = ContextFactory.Create())
{
context.Insert(countries);
}
}
public void Update(string countryCode, Country country)
{
using (var context = ContextFactory.Create())
{
country.CountryCode = countryCode;
context.Update(country);
}
}
public void UpdateBatch(Dictionary<string, Country> countriesToUpdate)
{
using (var context = ContextFactory.Create())
{
var countries = countriesToUpdate.Select(kv =>
{
kv.Value.CountryCode = kv.Key;
return kv.Value;
});
context.Update(countries);
}
}
public void Delete(string countryCode)
{
using (var context = ContextFactory.Create())
{
context.Delete(new Country { CountryCode = countryCode });
}
}
public void DeleteBatch(List<string> countryCodes)
{
using (var context = ContextFactory.Create())
{
context.Delete(countryCodes.Select(c => new Country { CountryCode = c }));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using DapperTest.Models;
namespace DapperTest
{
public class CountryRepository2
{
public List<Country> GetAll()
{
using (var context = ContextFactory.Create())
{
return context.Query<Country>("SELECT * FROM Country").ToList();
}
}
public Country Get(string countryCode)
{
using (var context = ContextFactory.Create())
{
return context.QueryFirstOrDefault<Country>(
"SELECT * FROM Country WHERE CountryCode = @countryCode",
new {
countryCode
});
}
}
public void Insert(Country country)
{
using (var context = ContextFactory.Create())
{
context.Execute(
"INSERT INTO [dbo].[Country] ([CountryCode],[Description],[CreateDate],[CreateUser],[LastUpdate],[LastUser]) " +
"VALUES(@countryCode, @description, @createDate, @createUser, @lastUpdate, @lastUser)",
country);
}
}
public void Update(string countryCode, Country country)
{
using (var context = ContextFactory.Create())
{
context.Execute(
"UPDATE [dbo].[Country] SET " +
"[CountryCode] = @countryCode," +
"[Description] = @description," +
"[CreateDate] = @createDate," +
"[CreateUser] = @createUser," +
"[LastUpdate] = @lastUpdate," +
"[LastUser] = @lastUser" +
"WHERE [CountryCode] = @countryCode",
country);
}
}
public void Delete(string countryCode)
{
using (var context = ContextFactory.Create())
{
context.Execute(
"DELETE FROM [dbo].[Country] WHERE[CountryCode] = @countryCode",
new {countryCode});
}
}
public void InsertBatch(List<Country> countries)
{
throw new NotImplementedException();
}
public void UpdateBatch(Dictionary<string, Country> countriesToUpdate)
{
throw new NotImplementedException();
}
public void DeleteBatch(List<string> countryCodes)
{
throw new NotImplementedException();
}
}
}
using System;
using DapperTest.Models;
using Newtonsoft.Json;
namespace DapperTest
{
internal class Program
{
internal static void Main(string[] args)
{
var repo = new CountryRepository();
var countries = repo.GetAll();
Console.WriteLine($"Found {countries.Count} country records.");
repo.Insert(new Country
{
CountryCode = "KLR",
CreateDate = DateTime.Now,
CreateUser = "kritter",
Description = "13 COLONIES OF KERRY RITTER"
});
Console.WriteLine($"Inserted country record.");
var newCountry = repo.Get("KLR");
Console.WriteLine($"Found created country: {JsonConvert.SerializeObject(newCountry)}");
repo.Update("KLR", new Country
{
CreateDate = DateTime.Now,
CreateUser = "kritter",
Description = "UNITED STATES OF KERRY RITTER"
});
Console.WriteLine($"Found country: {JsonConvert.SerializeObject(newCountry)}");
var updatedCountry = repo.Get("KLR");
Console.WriteLine($"Found updated country: {JsonConvert.SerializeObject(updatedCountry)}");
repo.Delete("KLR");
var deletedCountry = repo.Get("KLR");
Console.WriteLine($"Found updated country: {JsonConvert.SerializeObject(deletedCountry)}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment