Reputation: 836
I'm trying to wrap the NHibernate Fetch extension method so that I can test queries using it without a full blown NHibernate session.
I've followed this approach and it seems to work OK, however using it means I need to give a different name to the new Fetch method. (EagerlyFetch is used in the example)
If I rename it to Fetch, I get a infinite loop since the wrapper calls it's own Fetch extension method instead of the NHibernate.Linq one.
Does anybody know a way around this. I guess I can add another level of abstraction, but just hoping there is something a little more graceful.
Upvotes: 1
Views: 1189
Reputation: 9864
You have to qualify the NHibernate extension by its holder class for disambiguating it from your own. It results in a simple static method call instead of an extension call, but that should not be an issue for your case.
using NHibernate.Linq;
namespace YourOwnExtensionsNameSpace
{
public static class YourOwnExtensionClass
{
public static INhFetchRequest<TOriginating, TRelated>
Fetch<TOriginating, TRelated>(
this IQueryable<TOriginating> query,
Expression<Func<TOriginating, TRelated>> relatedObjectSelector)
{
EagerFetchingExtensionMethods.Fetch(query, relatedObjectSelector);
}
...
}
}
But then, for avoiding name conflicts, you would have to ban any using NHibernate.Linq;
in files in which you wish to call your own Fetch
.
See here for all the eager fetching extensions. I have taken the first one for this example.
Upvotes: 1
Reputation: 16956
You cannot.
*You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.*
Upvotes: 2