概术:
企业应用级别当中,客户往往都会有在不改动程序的前提下对应用对象进行扩展。
自定义字段就是通过用户自定义功能,在不改变程序前提下,实现对数据表功能的自动增加。
我们开发的环境是.net的面对对象结构,数据库访问采用NHibernate框架。
基本有两种解决办法:
1、通过数据库扩展,实现数据1对多的操作。
2、自动增加数据库字段。
第一种比较基本的解决办法,但如果要查询日期范围将非常的困难
了解NHibernate框架都知道,实体对象是通过xml配置,然后嵌套入项目当中,所以不能通过更改xml节点数据,达到数据库映射功能。
利用NHibernaet的dynamic-component映射一个新的字段,然后就可以HQL的语句读取相应的字段值,查的网上很多资料,都未找到相关的文档及实例,真是困难重重,毕竟这种开源的项目学习资料比较少,倒是在Java的Hibernate有一些相关的文章,不知道Hibernate下是如何配置xml,是不是也要嵌入项目?
Hibernate的参考文献:Using Hibernate to Support Custom Domain Object Fields
参考文章:
asp.net实现自定义字段方法【Custom Domain using nHibernate dynamic-component】
private static void AddMappingExtensions()
{
//get the mappings from the web.config you will need to implement a custom configuration section to do this.
foreach (ClassMappingElement mapping in WebConfiguration.NHibernate.Mappings)
{
var persistentClass = _configuration.GetClassMapping(ReflectUtil.ClassForName(mapping.Type));
if (persistentClass != null)
{
var componentProperty = persistentClass.GetProperty(mapping.DynamicComponent);
if (componentProperty != null)
{
var component = (Component)componentProperty.Value;
foreach (PropertyElement property in mapping.MappingProperties)
{
component.AddProperty(CreateDynamicProperty(property, persistentClass));
}
}
}
}
}
private static Property CreateDynamicProperty(PropertyElement propertyElement, PersistentClass persistentClass)
{
// Create a simple value that contains the column and will be set to the property as information.
SimpleValue simpleValue = new SimpleValue(persistentClass.Table);
simpleValue.TypeName = propertyElement.Type;
// Create a new db column specification.
Column column = new Column(Configuration.NamingStrategy.ColumnName(propertyElement.Column));
column.Value = simpleValue;
if (propertyElement.Length > 0)
{
column.Length = propertyElement.Length;
}
column.IsNullable = !propertyElement.NotNull;
if (propertyElement.Scale > 0)
{
column.Scale = propertyElement.Scale;
}
if (propertyElement.Precision > 0)
{
column.Precision = propertyElement.Precision;
}
if (!string.IsNullOrEmpty(propertyElement.Default))
{
column.DefaultValue = propertyElement.Default;
}
if (persistentClass.Table != null)
{
persistentClass.Table.AddColumn(column);
}
simpleValue.AddColumn(column);
// Create a new property.
return new Property { Name = propertyElement.Name, Value = simpleValue };
}
作者提到ClassMappingElement 类操作不是非常清楚。
本文探讨了在.NET环境中使用NHibernate框架时如何实现用户自定义字段,以满足在不修改程序的情况下扩展应用对象的需求。两种主要方法是通过数据库扩展和自动增加字段。由于NHibernate实体对象的xml配置,直接更改xml来映射数据库变得复杂。文章提到可以利用dynamic-component映射新字段,并通过HQL查询,但相关文档和实例较少。
380

被折叠的 条评论
为什么被折叠?



