これはおそらくいくつかの愚かな見落としですが、ここに行きます:
public class Entity<TId> where TId : IEquatable<TId>
{
public virtual TId Id { get; private set; }
}
public class Client : Entity<Guid> { }
public class State : Entity<short> { }
public class Helper
{
protected IList<Client> clients;
protected IList<State> states;
//Works
public T Get<T>()
{
return default(T);
}
public T Get<T>(Guid id) where T : Entity<Guid>
{
return default(T);
}
public T Get<T>(short id) where T : Entity<short>
{
return default(T);
}
}
両方のクラスで機能するGet関数をどのように作成すればよいですか?そして、エンティティから継承する他のすべてのものとは?
反対票が多すぎないことを願っています。:(
編集
これが使用される場所です。したがって、2つのクラスだけではありません。しかし、基本的に私のモデルのすべてのクラス。
//What should I declare here?
{
TEntity result = default(TEntity);
try
{
using (var tx = session.BeginTransaction())
{
result = session.Query<TEntity>().Where(e => e.Id == Id).FirstOrDefault();
}
return result;
}
catch (Exception ex)
{
throw;
}
}
SWekoによって与えられた解決策で
public TEntity Get<TEntity, TId>(TId Id)
where TEntity : Entity<TId>
where TId : IEquatable<TId>
{
try
{
TEntity result = default(TEntity);
using (var tx = statefullSession.BeginTransaction())
{
result = statefullSession.Query<TEntity>().Where(e => e.Id.Equals(Id)).FirstOrDefault();
}
return result;
}
catch (Exception ex)
{
throw;
}
}