Created
November 26, 2015 23:14
-
-
Save brendanmckenzie/33a439d9cba88668e146 to your computer and use it in GitHub Desktop.
This file contains 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 characters
public static class NpgsqlConnectionExtensions | |
{ | |
public static void ExecuteFromResource(this NpgsqlConnection connection, string resourceName, object parameters = null) | |
{ | |
var sql = ResourceUtil.Get<NpgsqlContentTreeProvider>($"Sql/{resourceName}.sql"); | |
connection.Execute(sql, parameters); | |
} | |
public static IEnumerable<T> QueryFromResource<T>(this NpgsqlConnection connection, string resourceName, object parameters = null) | |
{ | |
var sql = ResourceUtil.Get<NpgsqlContentTreeProvider>($"Sql/{resourceName}.sql"); | |
return connection.Query<T>(sql, parameters); | |
} | |
public static T QuerySingleFromResource<T>(this NpgsqlConnection connection, string resourceName, object parameters = null) | |
{ | |
return QueryFromResource<T>(connection, resourceName, parameters).SingleOrDefault(); | |
} | |
} |
This file contains 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 characters
public static class ResourceUtil | |
{ | |
#region Public Methods | |
public static string Get<TAssembly>(string name) | |
{ | |
if (name == null) { throw new ArgumentNullException(nameof(name)); } | |
var assembly = typeof(TAssembly).GetTypeInfo().Assembly; | |
name = name.Replace('/', '.'); | |
name = assembly.GetName().Name + '.' + name; | |
using (var stream = assembly.GetManifestResourceStream(name)) | |
{ | |
if (stream == null) | |
{ | |
throw new ResourceNotFoundException(name); | |
} | |
using (var reader = new StreamReader(stream)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
} | |
#endregion | |
} | |
public class ResourceNotFoundException : Exception | |
{ | |
#region Public Constructors | |
public ResourceNotFoundException(string resourceName) | |
: base($"Resource not found {resourceName}") | |
{ | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment