forked from diegose/NHPad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NHibernateStaticDataContextDriver.cs
82 lines (71 loc) · 2.94 KB
/
NHibernateStaticDataContextDriver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using LINQPad.Extensibility.DataContext;
using NHibernate;
namespace NHPad
{
public class NHibernateStaticDataContextDriver : StaticDataContextDriver
{
public override string GetConnectionDescription(IConnectionInfo cxInfo)
{
return cxInfo.CustomTypeInfo.GetCustomTypeDescription();
}
public override bool ShowConnectionDialog(IConnectionInfo cxInfo, bool isNewConnection)
{
cxInfo.CustomTypeInfo.CustomTypeName = typeof(NHibernateContext).FullName;
cxInfo.CustomTypeInfo.CustomAssemblyPath = Assembly.GetExecutingAssembly().Location;
return new ConnectionDialog(cxInfo).ShowDialog().GetValueOrDefault();
}
public override string Name
{
get { return "NHibernate"; }
}
public override string Author
{
get { return "Diego Mijelshon"; }
}
public override List<ExplorerItem> GetSchema(IConnectionInfo cxInfo, Type customType)
{
return NHibernateSchemaReader.GetSchema(GetSessionFactory(cxInfo));
}
public override IEnumerable<string> GetNamespacesToAdd(IConnectionInfo cxInfo)
{
yield return "NHibernate";
yield return "NHibernate.Criterion";
yield return "NHibernate.Linq";
var entityNamespaces = GetSessionFactory(cxInfo).GetAllClassMetadata()
.Select(x => x.Value.GetMappedClass(EntityMode.Poco).Namespace).Distinct();
foreach (var entityNamespace in entityNamespaces)
yield return entityNamespace;
}
public override IEnumerable<string> GetAssembliesToAdd(IConnectionInfo cxInfo)
{
yield return GetClientAssembly(cxInfo).FullName;
}
public override void InitializeContext(IConnectionInfo cxInfo, object context, QueryExecutionManager executionManager)
{
GetClientAssembly(cxInfo);
var nhContext = (NHibernateContext)context;
nhContext.Session = GetSessionFactory(cxInfo).OpenSession();
}
public override bool AreRepositoriesEquivalent(IConnectionInfo c1, IConnectionInfo c2)
{
return XNode.DeepEquals(c1.DriverData, c2.DriverData);
}
ISessionFactory GetSessionFactory(IConnectionInfo cxInfo)
{
var assembly = GetClientAssembly(cxInfo);
var method = assembly.GetExportedTypes().SelectMany(x => x.GetMethods())
.First(x => x.IsStatic &&
x.ReturnType == typeof(ISessionFactory));
return (ISessionFactory)method.Invoke(null, null);
}
static Assembly GetClientAssembly(IConnectionInfo cxInfo)
{
return LoadAssemblySafely((string)cxInfo.DriverData.Element("ClientAssembly"));
}
}
}