Calling Get in the following code works fine:
public class ContractService : IContractService
{
private readonly IRepository<Contract> repository;
public ContractService(IRepository<Contract> repository)
{
this.repository = repository;
}
public Contract Get(int contractId)
{
return repository.Query().Where(x => x.Id == contractId).FirstOrDefault();
}
but when i do this:
public class ContractService : CRUDService<Contract>, IContractService
{
public ContractService(IRepository<Contract> repository) : base(repository)
{
}
}
public class CRUDService<TEntity> : ICRUDService<TEntity> where TEntity : IEntity
{
protected readonly IRepository<TEntity> repository;
public CRUDService(IRepository<TEntity> repository)
{
this.repository = repository;
}
public TEntity Get(int id)
{
var entities = this.repository.Query().Where(s => s.Id == id);
return entities.FirstOrDefault();
}
"entities" inside the get method throws an exception when you iterate over it:
Invalid cast from 'System.Int32' to 'TEntity' (where TEntity is the type name)
Anyone got any idea why?
Edit: here's what the different expressions look like:
In the generic version (top one), it seems to be trying to convert x for some reason, which must be because of the generics :s
{value(NHibernate.Linq.Query`1[Contract]).Where(x => (Convert(x).Id = value(CRUDService`1+<>c__DisplayClass0[Contract]).Id)).FirstOrDefault()}
{value(NHibernate.Linq.Query`1[Contract]).Where(x => (x.Id = value(ContractService+<>c__DisplayClass2).Id)).FirstOrDefault()}
(namespaces omitted for clarity)
2nd Edit: It seems to be when it tries to convert between IEntity and the instance type (TEntity)
here is IEntity:
public interface IEntity
{
int Id { get; }
}
3rd Edit: it seems to be the Convert(x) that causes the AssociationVisitor to not properly visit the expression tree and convert "Convert(x).Id"
4th Edit: And there we go, someones already found the bug http://nhjira.koah.net/browse/NHLQ-11!
Thanks
Andrew