My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

using ConfOrm;
using ConfOrm.Mappers;
using ConfOrm.NH;
using NHibernate.Cfg.MappingSchema;
using NUnit.Framework;

using MapDemo.Domain;
using NHibernate;
using NHibernate.Type;

namespace MapDemo
{
[TestFixture]
public class Demo
{
public class StringPropertyConvention : IPatternApplier<MemberInfo, IPropertyMapper>
{
public void Apply(MemberInfo subject, IPropertyMapper applyTo)
{
applyTo.Column(cm => cm.Length(50));
}

public bool Match(MemberInfo subject)
{
var propertyInfo = (PropertyInfo)subject;
return propertyInfo.PropertyType == typeof(string);
}
}

public class DescriptionPropertyConvention : IPatternApplier<MemberInfo, IPropertyMapper>
{
public void Apply(MemberInfo subject, IPropertyMapper applyTo)
{
applyTo.Column(cm => cm.Length(200));
}

public bool Match(MemberInfo subject)
{
var propertyInfo = (PropertyInfo)subject;
return propertyInfo.PropertyType == typeof(string)
&& propertyInfo.Name == "Description";
}
}

[Test][Explicit]
public void MapDomain()
{
var allDomainTypes =
typeof(Base)
.Assembly
.GetTypes()
.Where(t => t.Namespace == typeof(Base).Namespace)
.ToList();

var rootTypes =
allDomainTypes
.Where(t => t.BaseType == typeof(Base))
.ToList();

var domainInspector = new ObjectRelationalMapper();
domainInspector.TablePerClassHierarchy(rootTypes);

var mapper = new Mapper(domainInspector);
mapper.PropertyPatternsAppliers.Add(new StringPropertyConvention());
mapper.PropertyPatternsAppliers.Add(new DescriptionPropertyConvention());

// class specific customisations
mapper.Customize<SpecialLibrary>(SpecialLibrary.CustomiseMapping);
mapper.Customize<DetailedLibrary>(DetailedLibrary.CustomiseMapping);

var mappings = mapper.CompileMappingForEach(allDomainTypes);
var xml = Serialize(mappings);

Console.WriteLine(xml);
}

protected static string Serialize(IEnumerable<HbmMapping> hbmMappings)
{
var allXml = "";
Directory.CreateDirectory("DemoMapping");
foreach (var hbmMapping in hbmMappings)
{
var fileName = "DemoMapping\\" + GetFileName(hbmMapping);
var xml = Serialize(hbmMapping);
allXml += xml;
File.WriteAllText(fileName, xml);
}
return allXml;
}

private static string GetFileName(HbmMapping hbmMapping)
{
var name = "MyMapping";
var rc = hbmMapping.RootClasses.FirstOrDefault();
if(rc!=null)
{
name= rc.Name;
}
var sc = hbmMapping.SubClasses.FirstOrDefault();
if (sc != null)
{
name = sc.Name;
}
var jc = hbmMapping.JoinedSubclasses.FirstOrDefault();
if (jc != null)
{
name = jc.Name;
}
var uc = hbmMapping.UnionSubclasses.FirstOrDefault();
if (uc != null)
{
name = uc.Name;
}
return name + ".hbm.xml";
}

protected static string Serialize(HbmMapping hbmElement)
{
var setting = new XmlWriterSettings { Indent = true };
var serializer = new XmlSerializer(typeof(HbmMapping));
using (var memStream = new MemoryStream(2048))
using (var xmlWriter = XmlWriter.Create(memStream, setting))
{
serializer.Serialize(xmlWriter, hbmElement);
memStream.Flush();
memStream.Position = 0;
using (var sr = new StreamReader(memStream))
{
return sr.ReadToEnd();
}
}
}
}
}

Change log

r182 by FlukeFan on Feb 21, 2010   Diff
Added class specific mapping
customisation.
Go to: 
Project members, sign in to write a code review

Older revisions

r181 by FlukeFan on Feb 21, 2010   Diff
Added property convention to demo.
r180 by FlukeFan on Feb 20, 2010   Diff
Back to original attempt.
r179 by FlukeFan on Feb 20, 2010   Diff
Updated convention style.
All revisions of this file

File info

Size: 3642 bytes, 142 lines
Powered by Google Project Hosting