是否有任何编程方法来注入nhibernate UserTypes?

时间:2011-08-02 16:42:34

标签: nhibernate sqlite decimal

我有一个使用Nhibernate和Microsoft Sql Server开发的应用程序,但现在我正在尝试将其迁移到SQLite。我发现SQLite存在问题,它没有给我一个合适的定点数格式和准确的货币值算法。因此,为了解决这个问题,我模拟了自定义IResultTransformer和UserType(FixedPointDecimalUserType)的定点行为。另外,我想在应用程序之间共享映射。

我有一个扩展,可以在类型为十进制的属性和组件映射中注入我的UserType

<property name="Quantity" not-null="true" type="Decimal(8,3)"/>

我的扩展程序是这样的:

public static class NHExtensions
    {

        public static NHibernate.Cfg.Configuration AdaptToSQLite(this NHibernate.Cfg.Configuration configuration)
        {
            if (configuration.ClassMappings.Count == 0)
                throw new InvalidOperationException("Es necesario añadir el assembly al configuration antes de llamar a este método");

            if (configuration.Properties[NHibernate.Cfg.Environment.Dialect].IsIn(typeof(SQLiteDialect).FullName, typeof(SQLiteDialect).AssemblyQualifiedName))
            {
                foreach (var mapping in configuration.ClassMappings)
                    foreach (var property in mapping.PropertyIterator)
                        AdaptProperty(property);

                foreach (var mapping in configuration.CollectionMappings)
                    How to get inject FixedPointUserType?

            }
            return configuration;
        }

        public static void AdaptProperty(Property property)
        {
            if (property.IsComposite)
            {
                var component = property.Value as Component;
                component.PropertyIterator.Each(AdaptProperty);
            }

            if (property.Type.GetType() == typeof(DecimalType))
            {
                var simpleValue = property.Value as SimpleValue;
                if (simpleValue != null)        
                    simpleValue.TypeName = typeof(FixedPointDecimalUserType).AssemblyQualifiedName;
            }
        }
    }

但是当我找到一个NHibernate.Mapping.List&lt;&gt;我不知道如何注入我的UserType,例如SoldProduct。

<list name="Addins" table="TicketLineAddin" fetch="join">
    <key column="TicketLineId" ..../>
    <index column="AddinIndex"/>
    <composite-element class="Domain.Catalogue.SoldProduct, Domain">
        <property name="ProductId" not-null="true"/>
        <property name="ProductName" not-null="true" length="120"/>
        <property name="Price" not-null="true" type="Decimal(12,3)"/>
        <property name="VatId" column="VatId"  not-null="true"/>
        <property name="VatRate" column="VatRate"  not-null="true" type="Decimal(12,3)"/>
        <property name="PreparationType" not-null="true" type="Common.Ordering.PreparationType, Common"/>
        <property name="PriceListId" not-null="true"/>
        <property name="PrintMode" not-null="true"/>
    </composite-element>
</list>

替代方案可以是SQL Server和SQLite应用程序的不同自定义UserType实现,并替换.hbm文件中的所有小数类型

<property name="Quantity" not-null="true" type="FixedPointUserType"/>

但是,有什么程序方法可以解决这个问题吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

foreach (var mapping in config.CollectionMappings)
{
    if (mapping.Element.Type.IsComponentType)
    {
        var subtypes = ((ComponentType)mapping.Element.Type).Subtypes;
        for (int i =0; i < subtypes.Length; i++)
        {
            if (subtypes[i].GetType() == typeof(DecimalType))
            {
                subtypes[i] = new FixedPointDecimalUserType();
            }
        }
    }
}

不能测试它

答案 1 :(得分:0)

使用此代码可以正常工作,谢谢

configuration.BuildMappings();

    ...
foreach(...)
    if (mapping.Element.Type.IsComponentType)
    {
        var subtypes = ((ComponentType)elementType).Subtypes;
        for (var i = 0; i < subtypes.Length; i++)
            if (subtypes[i].GetType() == typeof(DecimalType))
                subtypes[i] = new CustomType(typeof(FixedPointDecimalUserType), new Dictionary<string, string>());

    }