I doubt you will find a solid solution to this. This is just not how
NHibernate is intended to work.

If you insist, you'll have to use HQL queries. id (all lowercase) always
refers to an entity's identifier. You may also refer to the mapped _id
field, but you can't use it when the compiler checks access, like QueryOver
and LINQ.

On Mon, Jun 14, 2010 at 9:04 AM, Trinition <[email protected]> wrote:

> Sure, an int is more strongly typed than 'var', but one can't
> distinguish an int representing a Client's ID from an int representing
> an Organization's ID.  But surely you wouldn't want to confuse the
> two.
>
> So far, I have been pointing to a private field.  The problem occurs
> with NHIbernate's LINQ because the public property I'm exposing to my
> domain (including my repositories which use LINQ) aren't known to be
> mapped by NHibernate, so it can't convert them to a query.  For
> example:
>
> class Client {
>  private int _id; // Mapped as the identity in Client.hbm.xml
>  public ClientId Id { get { return new ClientId(_id); } } // A
> generated, strongly-typed wrapper
> }
> class ClientRepository {
>  public IList<Client> GetSpecificClients(IList<ClientId> clientIds)
> { // This is a contrived example
>    return _unitOfWork.Find<Client>().Where(client =>
> clientIds.Contains(client.Id)).ToList(); // 'Id' property of Client is
> not known to NHibernate!
>  }
> }
>
> That is why I'd prefer to use my ClientId class directly as the
> identity type for Client.
>
> Regards,
> Brian.
>
> On Jun 11, 9:04 pm, Diego Mijelshon <[email protected]> wrote:
> > How is an int _not_ strongly-typed? What value do those wrappers provide?
> > In any case, you can always point NHibernate to your private field, and
> it
> > will skip your wrapper completely:
> >
> > <id name="_Id" access="field" type="Int32">
> >   <generator class="..."/>
> > </id>
> >
> >    Diego
> >
> >
> >
> > On Fri, Jun 11, 2010 at 16:12, Trinition <[email protected]> wrote:
> > > In my domain, I'd really prefer to have strongly-typed IDs (simple
> > > immutable wrappers around ints).  This would allow me to have clear
> > > method signatures and allow Intellisense to assist me more accurately:
> >
> > > public void ResetFirmLockout(FirmId firmId); // Consider this strongly-
> > > typed identity...
> > > public void ResetFirmLockout(intfirmId); // Consider this weakly-typed
> > > identity...
> >
> > > It may sound trivial, but I've pulled it off half-way and it has been
> > > quite nice to have it this way (I'm using a private int field "_id"
> > > but exposing a readonly-property to the domain of type FirmId that is
> > > constructed on the fly).
> >
> > > The only problem is that I can't get NHibernate to recognize a
> > > component as it's identiy.  The "<id...>" takes a Type that must be an
> > > NHibernate type like "Int32".  I can't specify a user type such as
> > > "FirmId".
> >
> > > I've also considered <composite-id>, but it only works with
> > > application-assigned identifiers, whereas I do want to rely upon DB-
> > > generated identities.
> >
> > > I considered a custom IIdentityGenerator, but as best I can tell, it
> > > would still have to be an NHibernate type (e.g. "Int32").
> >
> > > Has anyone else ever accomplished this?
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to