Created
March 9, 2019 22:42
-
-
Save anthonydotnet/63115da9c5c73fb21cc918140f2f2259 to your computer and use it in GitHub Desktop.
This method used to be really clean until I added the event hooks. Is there any cleaner way to code this?
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 async Task<ICreateResponse<TEntity>> CreateAsync(TEntity poco) | |
{ | |
bool exitMethod = false; | |
//-- Event: Method start | |
var eventResult = OnCreate_MethodStart != null ? OnCreate_MethodStart(poco, out exitMethod) : null; | |
if (exitMethod) { return eventResult; } | |
try | |
{ | |
var entityId = ReflectionHelper.GetPropertyValue(poco, _repoIdentityProperty); | |
if (!string.IsNullOrWhiteSpace(entityId as string)) | |
{ | |
// check for existance first! | |
var doc = await _repository.GetByIdAsync(entityId); | |
if (doc != null) | |
{ | |
//-- Event: Already exists | |
eventResult = OnCreate_HasConflict != null ? OnCreate_HasConflict(poco, out exitMethod) : null; | |
if (exitMethod) { return eventResult; } | |
return new ConflictResponse<TEntity>(entityId, poco); | |
} | |
} | |
//-- Event: Doc NOT exists, so create! | |
eventResult = OnCreate_NoConflict != null ? OnCreate_NoConflict(poco, out exitMethod) : null; | |
if (exitMethod) { return eventResult; } | |
var res = await _repository.CreateAsync(poco); | |
var response = new CreateResponse<TEntity>(res); | |
//-- Event: Successful execution | |
eventResult = OnCreate_Success != null ? OnCreate_Success(poco, out exitMethod) : null; | |
if (exitMethod) { return eventResult; } | |
return response; | |
} | |
catch (Exception ex) | |
{ | |
//-- Event: Error during create | |
eventResult = OnCreate_Exception != null ? OnCreate_Exception(poco, out exitMethod) : null; | |
if (exitMethod) { return eventResult; } | |
return new CreateErrorResponse<TEntity>(poco, ErrorMessage.Build("Create", poco.ToString()), ex); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment