public Mappings CreateMappings(Dialect.Dialect dialect)
That method stay there from loooong time ago.
As we are doing in NH-Core each “binder” must use at least two classes (to create new metadata):
- an instance of NHibernate.Cfg.Mappings
- the instance of the configured Dialect
The configuration extension
public class Configuration : NHibernate.Cfg.Configuration{ public void Register(Type entity) { Dialect dialect = Dialect.GetDialect(Properties); Mappings mappings = CreateMappings(dialect); SetDefaultMappingsProperties(mappings); new EntityMapper(mappings, dialect).Bind(entity); } private static void SetDefaultMappingsProperties(Mappings mappings) { mappings.SchemaName = null; mappings.DefaultCascade = "none"; mappings.DefaultAccess = "property"; mappings.DefaultLazy = true; mappings.IsAutoImport = true; mappings.DefaultNamespace = null; mappings.DefaultAssembly = null; } }For each class, I’m going to register, I’m getting the configured dialect and a new instance of Mappings class. Then I’m setting some default values and at the end I’m biding the entity type (EntityMapper(mappings, dialect).Bind(entity)).
The EntityMapper
Without boring you, with the full code, the heart is herepublic void Bind(Type entity) { var rootClass = new RootClass(); BindClass(entity, rootClass); } private void BindClass(Type entity, PersistentClass pclass) { pclass.IsLazy = true; pclass.EntityName = entity.FullName; pclass.ClassName = entity.AssemblyQualifiedName; pclass.ProxyInterfaceName = entity.AssemblyQualifiedName; string tableName = GetClassTableName(pclass); Table table = mappings.AddTable(null, null, tableName, null, pclass.IsAbstract.GetValueOrDefault()); ((ITableOwner) pclass).Table = table; pclass.IsMutable = true; PropertyInfo[] propInfos = entity.GetProperties(); PropertyInfo toExclude = new IdBinder(this, propInfos).Bind(pclass, table); pclass.CreatePrimaryKey(dialect); BindProperties(pclass, propInfos.Where(x => x != toExclude)); mappings.AddClass(pclass); string qualifiedName = pclass.MappedClass == null ? pclass.EntityName : pclass.MappedClass.AssemblyQualifiedName; mappings.AddImport(qualifiedName, pclass.EntityName); if (mappings.IsAutoImport && pclass.EntityName.IndexOf('.') > 0) { mappings.AddImport(qualifiedName, StringHelper.Unqualify(pclass.EntityName)); } }Including everything the EntityMapper.cs have 198 lines.
Metadata classes used are: RootClass, PersistentClass, Table, SimpleValue, Property and Column.
Conclusions
To use NHibernate without write a single XML mapping, is possible. Create mapped classes or others artifacts (as typedef, database-objects, named-queries, stored-procedures, filters and so on) without use XML, is possible. Because I’m extending the NHibernate.Cfg.Configuration, add some other artifacts or override mapping written using XML is possible. Write a “bridge” using EntityFramework attributes instead XMLs, is possible.Write a new framework avoiding XML at all, is possible that is completely different than “is easy”. In general a framework is to make something easy to the framework users and not to the framework developers; no?
Code available here.
Posted nov 16 2008, 09:13 p.m. by Fabio Maulo
Filed under: BuildSessionFactory, HowTo, NHibernate, Mapping source
No comments:
Post a Comment