Created
August 7, 2010 15:49
-
-
Save jfromaniello/512913 to your computer and use it in GitHub Desktop.
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 characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using NHibernate.Cfg.MappingSchema; | |
namespace NHibernate.Cfg | |
{ | |
public static class HqlConfigurationExtensions | |
{ | |
/// <summary> | |
/// Find a query with the given queryName in the given assembly and add to the configuration. | |
/// </summary> | |
public static void AddQuery(this Configuration configuration, Assembly assembly, string queryName, | |
Action<HbmQuery> configure = null) | |
{ | |
string fullQueryName = | |
assembly.GetManifestResourceNames().Where(r => r.EndsWith(string.Format(".{0}.hql", queryName))).First(); | |
string query = GetQueryAsString(assembly, fullQueryName); | |
ConfigureQueryAndAdd(queryName, query, configure, configuration); | |
} | |
/// <summary> | |
/// Find all the queries in the given assembly and add them to the configuration. | |
/// </summary> | |
public static void AddAllQueriesFromAssembly(this Configuration configuration, Assembly assembly, | |
Action<HbmQuery> configure = null) | |
{ | |
IEnumerable<string> queryNames = assembly.GetManifestResourceNames().Where(rn => rn.EndsWith(".hql")); | |
foreach (string queryName in queryNames) | |
{ | |
string query = GetQueryAsString(assembly, queryName); | |
ConfigureQueryAndAdd(queryName, query, configure, configuration); | |
} | |
} | |
/// <summary> | |
/// Find all queries in the assembly that match with the namespace and add to the configuration. | |
/// </summary> | |
public static void AddQueriesFromNamespace(this Configuration configuration, Assembly assembly, string @namespace, | |
Action<HbmQuery> configure = null) | |
{ | |
IEnumerable<string> queryNames = | |
assembly.GetManifestResourceNames().Where(rn => rn.EndsWith(".hql") && rn.StartsWith(@namespace)); | |
foreach (string queryName in queryNames) | |
{ | |
string query = GetQueryAsString(assembly, queryName); | |
ConfigureQueryAndAdd(queryName, query, configure, configuration); | |
} | |
} | |
private static string GetQueryAsString(Assembly assembly, string fullQueryName) | |
{ | |
Stream stream = assembly.GetManifestResourceStream(fullQueryName); | |
if (stream == null) | |
{ | |
return null; | |
} | |
using (var reader = new StreamReader(stream)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
private static void ConfigureQueryAndAdd(string queryName, string query, Action<HbmQuery> configure, | |
Configuration configuration) | |
{ | |
var mapping = new HbmMapping(); | |
var hbmQuery = new HbmQuery { name = queryName, Text = new[] { query } }; | |
mapping.Items1 = new object[] { hbmQuery }; | |
if (configure != null) configure(hbmQuery); | |
configuration.AddDeserializedMapping(mapping, queryName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment