Thanks for the link on the ClassResolver approach Fabio. I didn't try it
and it wasn't clear to me if it would support querying against a parent
interface.
With my current mapping, when I use Get or Load, for example:
a = (IA)s.Load(typeof(IA), a.Id);
a = s.Load<IA>(a.Id);
a = (IA)s.Get(typeof(IA), a.Id);
a = s.Get<IA>(a.Id);
It always complains that it can't find an entity persister for IA. I did
try the criteria API as Diego mentioned, and that does work. The Criteria
API uses ISessionFactoryImplementor.GetImplementors to map from the
interface name to the concrete class, but the Get and Load methods don't.
Is there any particular reason why they couldn't? If there's only a single
implementor for a given interface or class, it should just be able to go
ahead and load that type. Even if there are multiple implementations, I
believe it could do something like pick the base class.
Patrick Earl
On Fri, Sep 24, 2010 at 9:24 AM, Diego Mijelshon <[email protected]>wrote:
> This is already supported and you don't have to do anything.
>
> Given this class:
>
> public class Foo : IBar, IBaz {...}
>
> Mapped with just a single <class> that doesn't mention the interfaces, the
> following test passes:
>
> var foo = session.CreateCriteria<IBar>().Add(/*filter on IBar or common
> properties*/).UniqueResult();
> var sameFoo = session.CreateCriteria<IBaz>().Add(/*filter on IBaz or
> common properties*/).UniqueResult();
> Assert.AreSame(foo, sameFoo);
>
> For HQL you need to import the interface (
> http://nhforge.org/doc/nh/en/index.html#mapping-declaration-import), but
> that's it.
>
> Diego
>
>
>
> On Fri, Sep 24, 2010 at 12:11, Patrick Earl <[email protected]> wrote:
>
>> Since I like talking to myself so much, I thought I'd reply with some
>> other backing thoughts.
>>
>> We currently implement this within our own repository class. When it
>> sees a request for ISomething, it consults a map and converts that to
>> a request for Something. I've been looking at using ActiveRecord
>> again (we moved away from it due to this interface mapping issue) and
>> I've considered implementing the interface mapping in ActiveRecord.
>> When I thought about it further though, it seemed more like something
>> that ActiveRecord shouldn't need to worry about (aside from the
>> mapping perspective) and something that should be implemented in
>> NHibernate itself. Obviously it would be nice if things like Linq and
>> QueryOver worked against the interfaces as well.
>>
>> So, to summarize, I'm pretty sure that the ORM is the one that should
>> take responsibility, and that's why I'm interested in implementing
>> support for this in NHibernate.
>>
>> Patrick Earl
>>
>
>