3

I have a class A containing a collection of class B. This last class is very heavy and, for a new module I'm working in, I just need a couple of fields of it.

For improving performance when loading entities from SQL server using nhibernate I'd like to create a lite version of this class for using in this module I'm developing (class BLite).

This BLite class will be used on collections and many-to-one relations. I know I could create a separated mapping for this new class containing just the fields I need but, if possible, I'd like to avoid this (using components or similar feature).

Is it possible to use a "lite" version of a class without creating an extra mapping?

StackOverflower
  • 5,463
  • 13
  • 58
  • 89
  • Did you accidentally post this question before finishing it? It does not contain an actual question. – phoog Dec 26 '12 at 13:36
  • Maybe some DTOs could help you. You will not need to map this objects and you can create queries to get this type. Take a look here: http://stackoverflow.com/questions/1530345/best-approach-for-building-nhibernate-dtos – Felipe Oriani Dec 26 '12 at 13:37
  • You could move all required fields (in the lite and heavy version) to the lite version and inherit from that: public class BLite { public string Name { get; set; } } public class B : BLite { public string OtherProperty { get; set; } } I'm not exactly sure how NHibernate handles inheritance, but maybe it could help. – Sascha Dec 26 '12 at 13:38
  • Do you want to perform inserts and updates with the light class or just reads? – Jamie Ide Dec 26 '12 at 15:16

3 Answers3

3

What could help in your scenario is a lazy property mapping, documentation 5.1.9. property point 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.

This kind of mapping could be suitable for you, because these properties are available if needed (while object is connected to session), but never loaded implicitly. In queries, the fetch mode can still be adjusted (to be part of one SELECT clause).

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
0

You can use AutoMapper for the same purpose. (You need to define lite class, and specify maps for AutoMapper)

Using fluent interface, you can include/exclude only specific fields.

Example

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • My objective is to improve performance loading entities from sql with nhibernate, haven't took a very deep but not sure how automapper would help. Can't move to fluent interface at this stage of the project. – StackOverflower Dec 26 '12 at 13:56
0

This is an alternative to Automapper that I find easier to use http://valueinjecter.codeplex.com/

There is a comparison between them here AutoMapper vs ValueInjecter

In any case the easiest way to create a simple or light version of a heavy class is probably to use a mapper

Community
  • 1
  • 1
ScruffyDuck
  • 2,606
  • 3
  • 34
  • 50
  • My objective is to improve performance loading entities from sql with nhibernate, haven't took a very deep but not sure how automapper or valueinjecter would help – StackOverflower Dec 26 '12 at 14:13
  • Maybe you are correct. I am not really familiar with NHibernate. However I have some very heavy classes where I need to use one of two fields. I use ValueInjector to populate a new lite class (DTO really), do some work and send the fields back. If that is not what you are trying to do or if you don't have control of instantiating the lite class then I guess these will not help – ScruffyDuck Dec 26 '12 at 14:20