Created
December 11, 2009 15:21
-
-
Save mhinze/254267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// interfaces | |
public interface IResult<T> | |
{ | |
T GetResult(); | |
} | |
public interface IEntityQuerier | |
{ | |
void Execute(ISession session, Guid parameter); | |
} | |
public interface IEntityResult<TEntity> : IResult<TEntity>, IEntityQuerier where TEntity : PersistentObject { } | |
// abstract base | |
public abstract class HqlQuerierBase<TResult> : IEntityResult<TResult> where TResult : PersistentObject | |
{ | |
private TResult _result; | |
public void SetResult(TResult result) | |
{ | |
_result = result; | |
} | |
public TResult GetResult() | |
{ | |
return _result; | |
} | |
public void Execute(ISession session, Guid parameter) | |
{ | |
_result = ExecuteCore(session, parameter); | |
} | |
protected virtual TResult ExecuteCore(ISession session, Guid parameter) | |
{ | |
return session.CreateQuery(GetHql()) | |
.SetParameter("id", parameter) | |
.SetResultTransformer(new DistinctRootEntityResultTransformer()) | |
.UniqueResult<TResult>(); | |
} | |
protected abstract string GetHql(); | |
} | |
// impl | |
public class PaymentHistoryQuery : HqlQuerierBase<Customer> | |
{ | |
protected override string GetHql() | |
{ | |
return | |
@"from Customer c | |
left join fetch c.PaymentHistory ph | |
left join fetch ph.Payment | |
where c.id = :id"; | |
} | |
public static implicit operator PaymentHistoryQuery(Customer customer) | |
{ | |
var query = new PaymentHistoryQuery(); | |
query.SetResult(customer); | |
return query; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment