I am trying to have my tasks return same object response instead of having each one with a different return. I wrote the following class,
public class ApiMethodResult : IDisposable { public ApiMethodResult () { Errors = new List<string> (); Warnings = new List<string> (); Value = null; } private dynamic Value { get; set; } private List<string> Errors { get; set; } private List<string> Warnings { get; set; } public void AddError (string errorMessage) { this.Errors.Add (errorMessage); } public void AddWarning (string warningMessage) { this.Warnings.Add (warningMessage); } public List<string> GetErrors () { return this.Errors; } public List<string> GetWarningList () { return this.Warnings; } public bool IsSuccess () { if (this.Errors.Count > 0) return false; return true; } public bool IsWarning () { if (this.Warnings.Count > 0) return true; return false; } public void AddValue (dynamic value) { this.Value = value; } public dynamic GetValue () { return this.Value; } private bool disposedValue = false; protected virtual void Dispose (bool disposing) { if (!disposedValue) { if (disposing) { } disposedValue = true; } } void IDisposable.Dispose () { Dispose (true); } }
I will explain more in this example,
private async Task<ApiMethodResult> NotifyUser (ApplicationUser user, IdentityTracker tracker = null) { ApiMethodResult response = new ApiMethodResult (); try { var code = await this.applicationUserRepository.GenerateEmailConfirmationTokenAsync (user); var callbackUrl = Url.EmailConfirmationLink (user.Id, code, Request.Scheme);
await templateService.SendNewAccountConfirmation (callbackUrl, user); await templateService.SendNewAccounSMStConfirmation (callbackUrl, user); return response; } catch (Exception ex) { ex.LogException ("3rd party error"); response.AddError (ex.Message); return response; } }
await templateService.SendNewAccountConfirmation (callbackUrl, user); await templateService.SendNewAccounSMStConfirmation (callbackUrl, user);
are two methods are using third party APIs, throwing an exception from any will stop the execution of my code, using "ApiMethodResult" will allow my code to continue and inspect the progress success as well.
I have two questions here,
is it a bad practice implementing "ApiMethodResult" in all my code?
if not, as shown above, I implemented "IDisposable" pattern, Do I need it or is it an overhead here?
Thank you.