I can’t quite figure this one out. To make it simple I’ll explain what I’m trying to do.
Currently, I am working on a nHibernate project which uses a repository pattern. Inside the database are a bunch of tables that have “Description” fields. Now, I am trying to localize these tables to use a common “Resource” table.
So, I have these 2 tables:
Table 1 – Resources :
ResourceId, LanguageId, Value
Table 2 – LinkLabels :
LinkLabelId, Description, ResourceId
Then there is a session token that contains a “LanguageId” which is the preferred language of the user.
Okay, with that said, what I need to do is get the “Value” from the ResourcesTable where ResourceId from LinkLabels = ResourceId from Resources for objects that have a “ResourceId”. But I’ve got to do this with a Generic type.
Here is what I have so far:
public IQueryable<T> ToQueryable<T>()
{
try
{
PropertyInfo resourceProperty = typeof(T).GetProperty("ResourceKey");
if (resourceProperty != null)
{
// Somthing like this, but of course this won't work
// because x.ResourceId doesn't exist because of generic, and the select
// statement is invalue.
//
// return from x in this.session.Query<T>()
// join p in this.session.Query<Resource>() on x.ResourceId equals p.ResourceId
// where p.LanguageId = 1 // this will be from a session object, hardcoded to 1 for now
// select x where x.Description is p.value;
}
else
{
return this.session.Query<T>();
}
}
catch (Exception ex)
{
throw ex;
}
}
Create an interface like this:
interface IResource {
int ResourceId { get; set;}
}
Then use a constraint:
public IQueryable ToQueryable() where T: IResource
{
…
}
Then you have to make all your types implement IResource as well.