0

I have a class Client like that:

public class Client{
   public Person Pers { get; set; }
}

And I have 2 Person´s child class :

public class PersonType1 : Person {...}
public class PersonType2 : Person {...}

Now I loaded a client... And I need to get the PersonType1 or PersonType2 attributes ..

I tried that:

var _pj = ((PersonType1 ) _client.Pers);

But it does not work, because the _client.Pers type is a Proxy (Lazy load true) ...

Is there a way to do that? I have several attributes in each PersonType, so It is not a good idea to create a virtual/override for each attribute (Person->PersonType1) ...

Thanks

1
  • Does this work even with lazy="false"? If 'Pers' is a proxy type of PersonType1 that cast would work by nature of polymorphism. My guess is that it isn't returning 'PersonType1', but 'Person'. Commented Nov 10, 2009 at 19:00

2 Answers 2

2

You could try to eagerly fetch the Pers property:

var client = session
    .CreateCriteria<Client>()
    .CreateCriteria("Pers", JoinType.LeftOuterJoin)
    .Add(Expression.IdEq(1))
    .UniqueResult<Client>();
var pj = (PersonType1)client.Pers;
Sign up to request clarification or add additional context in comments.

Comments

0

If you use NH in the server, and move the objects to the client, you can't use lazy load. What NH knows (in the server) doesn't help the client, which has neither Session nor knowledge how to fetch the extra data when needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.