问题描述
已成功使示例程序运行,我现在开始使用 Fluent NHibernate 进行实际工作 - 尝试在我的项目类上使用 Automapping等级制度.
Having successfully gotten a sample program working, I'm now startingto do Real Work with Fluent NHibernate - trying to use Automapping on my project's classheirarchy.
这是一个科学仪器应用程序,我正在学习的课程映射有几个属性是浮点数组,例如
It's a scientific instrumentation application, and the classes I'mmapping have several properties that are arrays of floats e.g.
private float[] _rawY;
public virtual float[] RawY
{
get
{
return _rawY;
}
set
{
_rawY = value;
}
}
这些数组最多可以包含 500 个值.
These arrays can contain a maximum of 500 values.
我没想到自动映射可以在数组上工作,但还是尝试了,起初取得了一些成功.每个数组都自动映射到一个 BLOB(使用 SQLite),这似乎是一个可行的解决方案.
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.
当我尝试调用 SaveOrUpdate 时,第一个问题出现了包含数组的对象 - 我得到浮动 [] 没有持久性"例外.
The first problem came when I tried to call SaveOrUpdate on theobjects containing the arrays - I got "No persister for float[]"exceptions.
所以我的下一个想法是将我所有的数组转换为 ILists,例如
So my next thought was to convert all my arrays into ILists e.g.
public virtual IList<float> RawY { get; set; }
但现在我明白了:
NHibernate.MappingException: Association references unmapped class: System.Single
由于 Automapping 可以处理复杂对象的列表,因此它永远不会我突然想到它无法映射基本类型的列表.但在搜索了一些解决方案之后,情况似乎就是这样.有些人似乎已经解决了问题,但是示例代码我看到比我现在需要更多的 NHibernate 知识 - 我不明白.
Since Automapping can deal with lists of complex objects, it neveroccured to me it would not be able to map lists of basic types. Butafter doing some Googling for a solution, this seems to be the case.Some people seem to have solved the problem, but the sample code Isaw requires more knowledge of NHibernate than I have right now - Ididn't understand it.
问题:
1.如何使用 Automapping 实现此功能?
2.另外,对于这个应用程序使用数组还是列表更好?
如有必要,我可以修改我的应用程序以使用其中任何一个(尽管我更喜欢列表).
I can modify my app to use either if necessary (though I preferlists).
我研究了映射字符串集合中的代码,并且我看到源代码中有设置字符串 IList 的测试代码,例如
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();
}
所以这一定可以使用纯 Automapping 来实现,但我的运气为零,这可能是因为我没有使用 NHibernate 手动映射的必要知识.
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.
结束编辑
这是我的 CreateSessionFactory 方法,如果这有助于制定一个回复...
Here's my CreateSessionFactory method, if that helps formulate areply...
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;
}
推荐答案
自从我发布我的问题以来,Fluent NHibernate 团队已经解决了这个问题.
Since I posted my question, the Fluent NHibernate team have fixed this problem.
您现在可以自动映射 C# 值类型(字符串、整数、浮点数等)的 IList.
You can now automap ILists of C# value types (strings, ints, floats, etc).
只需确保您拥有最新版本的 FNH.
Just make sure you have a recent version of FNH.
编辑
我最近从 FNH 1.0 升级到 FNH 1.3.
I recently upgraded from FNH 1.0 to FNH 1.3.
此版本还将自动映射数组 - float[]、int[] 等
This version will also automap arrays - float[], int[], etc.
似乎将它们映射为 BLOB.我认为这会比 IList 更有效,但尚未进行任何分析以确认.
Seems to map them as BLOBs. I assume this will be more efficient than ILists, but have not done any profiling to confirm.
这篇关于你如何自动映射 List<float>或使用 Fluent NHibernate 浮动 []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!