Is there any way to query stored procedure in Fluent Nhibernate without creating an hbm.xml file mapping?
You should have a class for returning result set in my case it's GameActivity class
public class GameActivity
{
public virtual DateTime Date { get; set; }
public virtual string GameRoundId { get; set; }
public virtual int GameProvider { get; set; }
public virtual string GameName { get; set; }
public virtual decimal RealBet { get; set; }
public virtual decimal RealWin { get; set; }
public virtual decimal BonusBet { get; set; }
public virtual decimal BonusWin { get; set; }
public virtual decimal BonusContribution { get; set; }
public virtual int IsRoundCompleted { get; set; }
public virtual int IsRoundCancelled { get; set; }
}
Calling stored procedure "GetMemberGameActivity" to get the list
var result = session.CreateSQLQuery("exec GetMemberGameActivity :mToken, :StartDate, :EndDate")
.SetResultTransformer(Transformers.AliasToBean())
.SetParameter("mToken", token)
.SetParameter("StartDate", startDate)
.SetParameter("EndDate", endDate)
.List().ToList();
I assume you use the standard
Session.GetNamedQuery(....
instead, you can use
var result = Session.CreateSQLQuery("exec MyStoredProc :pUserId, :pIsLocked")
.AddEntity(typeof(MyDomainObject))
.SetParameter("pUserId", userId)
.SetParameter("pIsLocked", isLocked)
.List<MyDomainObject>();
This allows you to call the stored proc but still get back a domain object (or list of) without needing a .hbm.xml file.
Actually there's this post
some good answers here however I want to make a more generic solution where I can pass in the object I want out and dynamically set my stored procedure parameters.
public RequestList<T> FetchExport<T>(Integration_ExportType exportType)
{
var returnRequest = new RequestList<T>{Success = true};
try
{
string sql = "EXEC "+exportType.StoredProcedure+" :@" + string.Join(", :@",exportType.Parameters.GetType().GetProperties().Select(pinfo => pinfo.Name).ToArray());
var session = Session.CreateSQLQuery(sql).SetResultTransformer(Transformers.AliasToBean<T>());
foreach (var parameter in exportType.Parameters.GetType().GetProperties())
{
session.SetParameter("@" + parameter.Name, parameter.GetValue(exportType.Parameters,null));
}
returnRequest.Obj = session.List<T>().ToList();
return returnRequest;
}
catch (Exception exception )
{
returnRequest.Success = false;
returnRequest.Message = exception.Message;
returnRequest.Exception = exception;
return returnRequest;
}
}
An example of a generic type - mapped to the output of the stored procedure & create an un-mapped object for the stored procedure parameters that gets attached to a patent object.
public class Integration_ExportRERW_Scores
{
public string SchoolId { get; set; }
public string SSID { get; set; }
public int LessonId { get; set; }
public int ClassPeriod { get; set; }
public string TeacherId { get; set; }
public string LessonTitle { get; set; }
public int RW4ComprehensionScore { get; set; }
public int RW4WordPowerScore { get; set; }
public int REComprehensionScore { get; set; }
public int REVocabularyScore { get; set; }
public int RE2ComprehensionScore { get; set; }
public int RE2VocabularyScore { get; set; }
}
public class Integration_ExportRERW_Scores_Parameters
{
public int DistrictId { get; set; }
}
Finally how it's implemented
var export = service.ListSubscriptions().First().Export;
var parameters = new Integration_ExportRERW_Scores_Parameters
{
DistrictId = 12060
};
export.Parameters = parameters;
var fetch = service.ExportData<Integration_ExportRERW_Scores>(export);