Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Last active April 24, 2019 11:09
Show Gist options
  • Select an option

  • Save mattbrailsford/c5ebbf2883784b1a4b2adbbc555e0d80 to your computer and use it in GitHub Desktop.

Select an option

Save mattbrailsford/c5ebbf2883784b1a4b2adbbc555e0d80 to your computer and use it in GitHub Desktop.
MethodStatus.cs
public void Delete(IMyEntity myEntity, out MethodStatus status)
{
var eventArgs = new MyEventArgs<IMyEntity>(myEntity);
if (myEntity.CanDelete()) // Some arbitrary check before deleting
{
status = MethodStatus.Error("Entity can't be deleted");
return;
}
using (var uow = _uowProvider.Create())
using (var repo = _repositoryFactory.CreateMyEntityRepository(uow))
{
OnDeleting(this, eventArgs);
if (eventArgs.Cancel)
{
status = MethodStatus.Cancelled();
return;
}
repo.Delete(myEntity);
uow.Complete();
}
OnDeleted(this, eventArgs);
status = MethodStatus.Ok();
}
public struct MethodStatus
{
public int Code { get; private set; }
public string Message { get; private set; }
private MethodStatus(int code, string msg)
{
Code = code;
Message = msg;
}
public bool IsOk => Code == MethodStatusCode.Ok;
public static MethodStatus Ok(string msg = null)
{
return new MethodStatus(MethodStatusCode.Ok, msg);
}
public static MethodStatus Cancelled(string msg = null)
{
return new MethodStatus(MethodStatusCode.Cancelled, msg);
}
public static MethodStatus Error(string msg = null)
{
return new MethodStatus(MethodStatusCode.Error, msg);
}
}
public static class MethodStatusCode
{
public static int Ok = 200;
public static int Cancelled = 300;
public static int Error = 400;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment