comparison Source/Mapping/Fluent/FluentConfig.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.CodeDom;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Reflection;
6
7 using BLToolkit.Data;
8 using BLToolkit.Data.DataProvider;
9 using BLToolkit.Reflection.Extension;
10
11 namespace BLToolkit.Mapping.Fluent
12 {
13 /// <summary>
14 /// Configure BLToolkit in fluent style
15 /// </summary>
16 public static class FluentConfig
17 {
18 private static Dictionary<Assembly, ExtensionList> _hash = new Dictionary<Assembly, ExtensionList>();
19
20 /// <summary>
21 /// Configure DbManager
22 /// </summary>
23 /// <param name="dbManager"></param>
24 public static MappingConfigurator Configure(DbManager dbManager)
25 {
26 MappingSchema mappingSchema = dbManager.MappingSchema ?? (dbManager.MappingSchema = Map.DefaultSchema);
27 return Configure(mappingSchema);
28 }
29
30 /// <summary>
31 /// Configure DataProvider
32 /// </summary>
33 /// <param name="dataProvider"></param>
34 public static MappingConfigurator Configure(DataProviderBase dataProvider)
35 {
36 MappingSchema mappingSchema = dataProvider.MappingSchema ?? (dataProvider.MappingSchema = Map.DefaultSchema);
37 return Configure(mappingSchema);
38 }
39
40 /// <summary>
41 /// Configure MappingSchema
42 /// </summary>
43 /// <param name="mappingSchema"></param>
44 public static MappingConfigurator Configure(MappingSchema mappingSchema)
45 {
46 ExtensionList extensionList = mappingSchema.Extensions ?? (mappingSchema.Extensions = new ExtensionList());
47 return Configure(extensionList);
48 }
49
50 /// <summary>
51 /// Configure ExtensionList
52 /// </summary>
53 /// <param name="extensionList"></param>
54 public static MappingConfigurator Configure(ExtensionList extensionList)
55 {
56 return new MappingConfigurator(extensionList);
57 }
58
59 public class MappingConfigurator
60 {
61 private ExtensionList _extensions;
62
63 public MappingConfigurator(ExtensionList extensions)
64 {
65 this._extensions = extensions;
66 }
67
68 /// <summary>
69 /// Mapping IFluentMap-Type
70 /// </summary>
71 /// <typeparam name="T"></typeparam>
72 /// <returns></returns>
73 public void MapingFromType<T>() where T : IFluentMap
74 {
75 MapingFromType(typeof(T));
76 }
77
78 public void MapingFromType(Type T)
79 {
80 var res = new ExtensionList();
81 var map = (IFluentMap)Activator.CreateInstance(T);
82
83 map.MapTo(res);
84
85 FluentMapHelper.MergeExtensions(res, ref this._extensions);
86 }
87 /// <summary>
88 /// Mapping from assembly contains type
89 /// </summary>
90 /// <typeparam name="T"></typeparam>
91 /// <returns></returns>
92 public void MapingFromAssemblyOf<T>()
93 {
94 this.MapingFromAssembly(typeof(T).Assembly);
95 }
96
97 /// <summary>
98 /// Mapping from assembly
99 /// </summary>
100 /// <param name="assembly"></param>
101 /// <returns></returns>
102 public void MapingFromAssembly(Assembly assembly)
103 {
104 ExtensionList res;
105 if (!_hash.TryGetValue(assembly, out res))
106 {
107 res = new ExtensionList();
108 _hash.Add(assembly, res);
109
110 string fluentType = typeof(IFluentMap).FullName;
111 var mapTypes = from type in assembly.GetTypes()
112 where type.IsClass && !type.IsAbstract && !type.IsGenericType
113 && (null != type.GetInterface(fluentType)) // Is IFluentMap
114 && (null != type.GetConstructor(new Type[0])) // Is defaut ctor
115 select type;
116 foreach (var fluentMapType in mapTypes)
117 {
118 MapingFromType(fluentMapType);
119 }
120 }
121 //FluentMapHelper.MergeExtensions(res, ref this._extensions);
122 }
123
124 #region Conventions
125
126 public static Func<Type, string> GetTableName;
127 public static Func<MappedProperty, string> GetColumnName;
128 //public static Func<MappedProperty, MappedProperty, string> GetManyToManyTableName;
129
130 #endregion
131 }
132 }
133 }