我有一个 A
类,其中包含 B
类的集合。最后一个类非常繁重,对于我正在使用的新模块,我只需要其中的几个字段。
为了提高使用 nhibernate 从 SQL Server 加载实体时的性能,我想创建此类的精简版,以便在我正在开发的这个模块中使用(类 BLite
)。
这个 BLite
类将用于集合和多对一关系。我知道我可以为这个新类创建一个单独的映射,只包含我需要的字段,但如果可能的话,我想避免这种情况(使用组件或类似功能)。
是否可以在不创建额外映射的情况下使用类的“精简版”?
最佳答案
lazy
属性映射对您的场景有帮助,文档 5.1.9. property第 9 点):
lazy (optional - defaults to false): Specifies that this property is lazy. A lazy property is not loaded when the object is initially loaded, unless the fetch mode has been overriden in a specific query. Values for lazy properties are loaded when any lazy property of the object is accessed.
这种映射可能适合您,因为这些属性在需要时可用(当对象连接到 session 时),但不会隐式加载。在查询中,获取模式仍然可以调整(作为一个 SELECT 子句的一部分)。
关于c# - 现有类(class)的精简版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14041302/