Last active
April 24, 2019 11:09
-
-
Save mattbrailsford/c5ebbf2883784b1a4b2adbbc555e0d80 to your computer and use it in GitHub Desktop.
MethodStatus.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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