So, this is a query question?
Since you are new to NH I give a bit wider answer than required. The first two classes should answer your question in their own.
public class DataQuery : AbstractQueryObject<IList<DataQueryResult>>
{
public override IList<DataQueryResult> GetResult()
{
DataMore1 dm1 = null;
DataMore2 dm2 = null;
var list =
this.Session.QueryOver<Data>()
.JoinAlias(data => data.DataMore1, () => dm1, NHibernate.SqlCommand.JoinType.LeftOuterJoin, Expression.Eq("Segment", 0))
.JoinAlias(data => data.DataMore2, () => dm2, NHibernate.SqlCommand.JoinType.LeftOuterJoin, Expression.Eq("Segment", 0))
.Select(p =>
new DataQueryResult
{
PlantID = p.PlantID,
AreaID = p.AreaID,
CellID = p.CellID,
DeviceID = p.DeviceID,
StartDateTime = p.StartDateTime,
DataPoint01 = p.DataPoint01,
DataMore1Segment = dm1.Segment,
DataMore1DataPoint101 = dm1.DataPoint101,
DataMore2Segment = dm2.Segment,
DataMore2DataPoint201 = dm2.DataPoint201
})
.List<DataQueryResult>();
return list;
}
}
public class DataQueryResult
{
internal DataQueryResult() { }
public int PlantID { get; internal set; }
public int AreaID { get; internal set; }
public int CellID { get; internal set; }
public int DeviceID { get; internal set; }
public DateTime StartDateTime { get; internal set; }
public float DataPoint01 { get; internal set; }
public byte? DataMore1Segment { get; internal set; }
public float? DataMore1DataPoint101 { get; internal set; }
public byte? DataMore2Segment { get; internal set; }
public float? DataMore2DataPoint201 { get; internal set; }
}
public abstract class AbstractQueryObject<TResult> : IQueryObject<TResult>
{
protected ISession Session;
public void Configure(object parameter)
{
/*obviously this NH query object implementation has to be provided with an ISession before it executes.
for that purpose I have an generic repository which holds an ISession and exposes the method
public interface IRepository { .. TResult ExecuteQuery<TResult>(IQueryObject<TResult> query); .. }
and has the usual Save<T>/Delete<T> methods.
obviously, to get the results you use: var data = repository.ExecuteQuery(new DataQuery());
i like this pattern a lot, so forgive my promotion :)
now, one repo instance keeps alive its session, so all entities from it are from the same isession.
when you dispose the repo, you kill the session. if you use DI in your classes, the repo instance is injected.
very perky is that your classes depend on the iqo and irepo ifcs which means you can swap them for EF which sometimes we do:)
easy unit testing.. etc.. */
if (!(parameter is ISession))
throw new ArgumentException("Argument of wrong type.");
this.Session = parameter as ISession;
}
public abstract TResult GetResult();
}
public interface IQueryObject<TResult>
{
void Configure(object parameter);
TResult GetResult();
}