-
-
Save nickpreston24/a5831c47ec158c7d43f3e81c140a8b7a to your computer and use it in GitHub Desktop.
Revisions
-
rflechner revised this gist
Apr 14, 2017 . 1 changed file with 31 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,11 @@ static void Main(string[] args) Console.WriteLine($"Seller {seller.Name} is {seller.Age} years old."); } foreach (var seller in repository.GetSellersHavingInvoiceCountOf(42)) { Console.WriteLine($"Seller {seller.Name} is {seller.Age} years old."); } Console.ReadKey(true); } } @@ -30,6 +35,17 @@ public class SellerModel public class SellerStatsRepository: RepositoryBase { public IList<SellerModel> GetSellersHavingInvoiceCountOf(int invoiceCount) { var param = new { invoiceCount }; return ExecuteView(record => new SellerModel { Name = (string)record["Name"], Age = (int)record["Age"] }, param).ToList().AsReadOnly(); } public IList<SellerModel> GetSellers() { return ExecuteView(record => new SellerModel @@ -42,16 +58,19 @@ public IList<SellerModel> GetSellers() public abstract class RepositoryBase { public static IDictionary<string, object> ToDictionary(object o) => o?.GetType()?.GetProperties()?.ToDictionary(member => member.Name, member => member.GetValue(o)) ?? new Dictionary<string, object>(); [MethodImpl(MethodImplOptions.NoInlining)] //object is type of record set and builder contains logic of mapping //TODO: change Dictionary<string, object> type to ORM record type public IEnumerable<T> ExecuteView<T>(Func<Dictionary<string, object>, T> builder, object args = null) { var parameters = ToDictionary(args); var stackTrace = new StackTrace(); var frame = stackTrace.GetFrame(1); var method = frame.GetMethod(); var assembly = method.DeclaringType.Assembly; using (var stream = assembly.GetManifestResourceStream($"{method.DeclaringType.Namespace}.{method.Name}.sql")) @@ -60,7 +79,13 @@ public IEnumerable<T> ExecuteView<T>(Func<Dictionary<string, object>, T> builder { var sql = reader.ReadToEnd(); Console.WriteLine("Executing SQL {0}", sql); Console.WriteLine("With params:"); foreach (var parameter in parameters) { Console.WriteLine($"- {parameter.Key}: {parameter.Value}"); } // TODO: execute SQL passing parameters var records = new List<Dictionary<string, object>> { new Dictionary<string, object> -
rflechner created this gist
Apr 14, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ -- this file Embedded resource SELECT * FROM "Sellers" This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; namespace ConsoleApp2 { class Program { static void Main(string[] args) { var repository = new SellerStatsRepository(); foreach (var seller in repository.GetSellers()) { Console.WriteLine($"Seller {seller.Name} is {seller.Age} years old."); } Console.ReadKey(true); } } public class SellerModel { public string Name { get; set; } public int Age { get; set; } } public class SellerStatsRepository: RepositoryBase { public IList<SellerModel> GetSellers() { return ExecuteView(record => new SellerModel { Name = (string) record["Name"], Age = (int) record["Age"] }).ToList().AsReadOnly(); } } public abstract class RepositoryBase { [MethodImpl(MethodImplOptions.NoInlining)] //object is type of record set and builder contains logic of mapping //TODO: change Dictionary<string, object> type to ORM record type public IEnumerable<T> ExecuteView<T>(Func<Dictionary<string, object>, T> builder) { StackTrace stackTrace = new StackTrace(); var frame = stackTrace.GetFrame(1); MethodBase method = frame.GetMethod(); var assembly = method.DeclaringType.Assembly; using (var stream = assembly.GetManifestResourceStream($"{method.DeclaringType.Namespace}.{method.Name}.sql")) { using (var reader = new StreamReader(stream)) { var sql = reader.ReadToEnd(); Console.WriteLine("Executing SQL {0}", sql); // TODO: execute SQL var records = new List<Dictionary<string, object>> { new Dictionary<string, object> { { "Name", "nom 1" }, { "Age", 32 } }, new Dictionary<string, object> { { "Name", "nom 2" }, { "Age", 35 } } }; return records.Select(builder); } } } } }