你如何自动映射列表< float>或漂浮[]与流利NHibernate的? [英] How do you automap List<float> or float[] with Fluent NHibernate?

查看:121
本文介绍了你如何自动映射列表< float>或漂浮[]与流利NHibernate的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经成功地获得了一个示例程序,我现在开始
做Fluent NHibernate的实际工作 - 尝试在我的项目类
heirarchy上使用Automapping。

这是一个科学的工具应用程序,类
mapping有几个属性,它们是浮点数组,例如

  private float [] _rawY; 
public virtual float [] RawY
{
get
{
return _rawY;
}
set
{
_rawY = value;




$ p $这些数组最多可以包含500个值。

我没有想到Automapping可以在数组上工作,但是无论如何也试过了
,而且一开始就取得了一些成功。每个数组都被自动映射到一个BLOB
(使用SQLite),这似乎是一个可行的解决方案。

当我尝试在包含数组的
对象上调用SaveOrUpdate时,第一个问题就出现了 - 我得到了没有persist为float []
例外。



所以我接下来的想法是将我所有的数组转换为IList,例如

 公共虚拟IList< float> RawY {get;组; } 

但现在我得到:

 NHibernate.MappingException:关联引用未映射类:System.Single 

由于自动映射可以处理复杂对象的列表,它从来没有发生过
,因此无法映射基本类型的列表。但是在做一些谷歌搜索解决方案之后的
,这似乎是这样的。
有些人似乎已经解决了这个问题,但是示例代码I
看起来需要更多的NHibernate知识,比我现在有更多的了解 - 我
不理解它。

问题:

如何使用Automapping进行这项工作?

2。另外,为这个应用程序使用数组或列表更好吗?

我可以修改我的应用程序以在必要时使用(尽管我更喜欢
列表)。

编辑

我研究了映射字符串集合,我看到在源代码中有测试代码,它建立了一个IList例如

 公共虚拟IList< string> ListOfSimpleChildren {get;组; } 

[Test]
public void CanSetAsElement()
{
New MappingTester< OneToManyTarget>()
.ForMapping(m => m。 HasMany(x => x.ListOfSimpleChildren).Element(columnName))
.Element(class / bag / element)。Exists();





$ b

所以这个必须使用纯自动映射,但是我没有运气得到任何东西的工作,可能是因为我没有与NHibernate手动映射必要的知识。



开始认为我将不得不破解这个将浮点数组作为单个字符串进行编码,或者创建一个包含单个浮点数的类,然后将其聚合到我的列表中),除非有人能够告诉我如何正确地执行此操作。



$ p
$ b $ p这里是我的CreateSessionFactory方法,如果这有助于形成一个
的回复...



$ pre $ private $ $ $ $ {
ISessionFactory sessionFactory = null;


const string autoMapExportDir =AutoMapExport;
if(!Directory.Exists(autoMapExportDir))
Directory.CreateDirectory(autoMapExportDir);

$ b $ try
{
var autoPersistenceModel =
AutoMap.AssemblyOf< DlsAppOverlordExportRunData>()
.Where(t => t。命名空间==DlsAppAutomapped)
.Conventions.Add(DefaultCascade.All())
;

$ b sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard
.UsingFile(DbFile)
.ShowSql()

.Mappings(m => m.AutoMappings.Add(autoPersistenceModel)
.ExportTo(autoMapExportDir)

.ExposeConfiguration(BuildSchema)
.BuildSessionFactory )
;

catch(Exception e)
{
Debug.WriteLine(e);
}


return sessionFactory;

$ / code $ / $ p

解决方案

你可以自动映射C#值类型(字符串,整数,浮点数等)的ILists。

只要确保您有最新版本的FNH即可。
$ b 编辑

我最近从FNH 1.0升级到FNH 1.3。这个版本也会自动映射数组 - float [],int []等。 / p>

似乎将它们映射为BLOB。我认为这会比IList更有效率,但还没有做任何分析确认。

Having successfully gotten a sample program working, I'm now starting to do Real Work with Fluent NHibernate - trying to use Automapping on my project's class heirarchy.

It's a scientific instrumentation application, and the classes I'm mapping have several properties that are arrays of floats e.g.

    private float[] _rawY; 
    public virtual float[] RawY 
    { 
        get 
        { 
            return _rawY; 
        } 
        set 
        { 
            _rawY = value; 
        } 
    } 

These arrays can contain a maximum of 500 values.

I didn't expect Automapping to work on arrays, but tried it anyway, with some success at first. Each array was auto mapped to a BLOB (using SQLite), which seemed like a viable solution.

The first problem came when I tried to call SaveOrUpdate on the objects containing the arrays - I got "No persister for float[]" exceptions.

So my next thought was to convert all my arrays into ILists e.g.

public virtual IList<float> RawY { get; set; } 

But now I get:

NHibernate.MappingException: Association references unmapped class: System.Single 

Since Automapping can deal with lists of complex objects, it never occured to me it would not be able to map lists of basic types. But after doing some Googling for a solution, this seems to be the case. Some people seem to have solved the problem, but the sample code I saw requires more knowledge of NHibernate than I have right now - I didn't understand it.

Questions:

1. How can I make this work with Automapping?

2. Also, is it better to use arrays or lists for this application?

I can modify my app to use either if necessary (though I prefer lists).

Edit:

I've studied the code in Mapping Collection of Strings, and I see there is test code in the source that sets up an IList of strings, e.g.

public virtual IList<string> ListOfSimpleChildren { get; set; }

[Test] 
public void CanSetAsElement() 
{ 
    new MappingTester<OneToManyTarget>() 
        .ForMapping(m => m.HasMany(x => x.ListOfSimpleChildren).Element("columnName")) 
        .Element("class/bag/element").Exists(); 
} 

so this must be possible using pure Automapping, but I've had zero luck getting anything to work, probably because I don't have the requisite knowlege of manually mapping with NHibernate.

Starting to think I'm going to have to hack this (by encoding the array of floats as a single string, or creating a class that contains a single float which I then aggregate into my lists), unless someone can tell me how to do it properly.

End Edit

Here's my CreateSessionFactory method, if that helps formulate a reply...

    private static ISessionFactory CreateSessionFactory() 
    { 
        ISessionFactory sessionFactory = null; 


        const string autoMapExportDir = "AutoMapExport"; 
        if( !Directory.Exists(autoMapExportDir) ) 
            Directory.CreateDirectory(autoMapExportDir); 


        try 
        { 
            var autoPersistenceModel = 
                AutoMap.AssemblyOf<DlsAppOverlordExportRunData>() 
                       .Where(t => t.Namespace == "DlsAppAutomapped") 
                       .Conventions.Add( DefaultCascade.All() ) 
                ; 


            sessionFactory = Fluently.Configure() 
                .Database(SQLiteConfiguration.Standard 
                              .UsingFile(DbFile) 
                              .ShowSql() 
                         ) 
                .Mappings(m => m.AutoMappings.Add(autoPersistenceModel) 
                                             .ExportTo(autoMapExportDir) 
                         ) 
                .ExposeConfiguration(BuildSchema) 
                .BuildSessionFactory() 
                ; 
        } 
        catch (Exception e) 
        { 
            Debug.WriteLine(e); 
        } 


        return sessionFactory; 
    } 

解决方案

Since I posted my question, the Fluent NHibernate team have fixed this problem.

You can now automap ILists of C# value types (strings, ints, floats, etc).

Just make sure you have a recent version of FNH.

Edit

I recently upgraded from FNH 1.0 to FNH 1.3.

This version will also automap arrays - float[], int[], etc.

Seems to map them as BLOBs. I assume this will be more efficient than ILists, but have not done any profiling to confirm.

这篇关于你如何自动映射列表&lt; float&gt;或漂浮[]与流利NHibernate的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆