c# - Return type IResult<T> -
i try solve problem in wpf app mvvm design. need return on database access more "data":
- result messages
- return value
- and if database successful
so create interface return type, here it:
public interface iresult<t> { bool issuccess { get; set; } string resultmessage { get; set; } t returnvalue { get; set; } }
this interface implements class:
public class dbresult: iresult<ilist<archive>> { public bool issuccess{ get; set;} public string resultmessage{ get; set;} public ilist<archive> returnvalue { get; set; } }
method in class on databases access have return type iresult<archive>
, archive class generated linq sql.:
public interface iarchivedbmanager { iresult<archive> loadconversationto(string nick, datetime todt); } [export(typeof(iarchivedbmanager))] public partial class archivedbmanager : iarchivedbmanager { public iresult<archive> loadconversationto(string nick, datetime todt) { var result = new dbresult(); try { var query = m in _dc.archive m.nick == nick m.time <= todt orderby m.time select m; result.returnvalue = query.tolist(); if (query.count() == 0) { result.resultmessage = "for specified search criteria found no record in database."; result.issuccess = false; } else { result.resultmessage = string.format("for specified search criteria found {0} record in database.", query.count()); result.issuccess = true; } return result; } catch (exception exception) { throw exception; } } }
class archivedbmanager inject mef in view model class.
i have question:
what correct solution problem in mvvm? scenario view model create through archivedbmanager access database, example if database table empty, archivedbmanager class return message "archive empty" , view model show message in view user.
it right create report of search results in database in "archivedbmanager class (it class on database access)"
what think solution?
keep simple: don't need interface or class.
you need ask result, , method should throw exception error message goes wrong.
this kind of success result messages: result.resultmessage = "for specified search criteria found no record in database.";
not belong business logic or data layer. ui layer should responsible showing messages user.
Comments
Post a Comment