Last active
March 7, 2019 08:19
-
-
Save anthonydotnet/a82807b8b32c893d4c9c8f6f840e865c to your computer and use it in GitHub Desktop.
CRUD service returning strongly typed responses (with status codes) in c#
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
ICrudResponse response; | |
// create 1 | |
response = service.CreateAsync(poco1).Result; | |
System.Console.WriteLine($"CreateAsync. {response.StatusCode}"); | |
if (response is CreateResponse<Profile> typedCreateRes) | |
{ | |
System.Console.WriteLine($"Success - Id {typedCreateRes.Entity.Id}"); | |
} | |
else if (response is ConflictResponse typedConflictRes) | |
{ | |
System.Console.WriteLine($"Conflicted. Id {typedConflictRes.Id}"); | |
} | |
else if (response is ErrorResponse typedErrorRes) | |
{ | |
System.Console.WriteLine($"Server error. Id {typedErrorRes.Message}"); | |
} | |
// create 2 | |
//var createdResult2 = service.CreateAsync(poco2).Result as CreateResult<Profile>; | |
//System.Console.WriteLine($"CreateAsync. {createdResult2.StatusCode}. Id {createdResult2.Entity.Id}"); | |
// get | |
response = service.GetByIdAsync("1234").Result; | |
System.Console.WriteLine($"\nGetByIdAsync. {response.StatusCode}."); | |
if (response is GetResponse<Profile> typedGetRes) | |
{ | |
System.Console.WriteLine($"{typedGetRes.Entity.Id}, {typedGetRes.Entity.FirstName} {typedGetRes.Entity.LastName}"); | |
} | |
// update | |
var docToUpdate = service.GetByIdAsync("1234").Result as GetResponse<Profile>; | |
var modifiedModel = docToUpdate.Entity; | |
modifiedModel.Occupation = "Singer"; | |
response = service.UpdateAsync(modifiedModel).Result; | |
System.Console.WriteLine($"\nUpdateAsync. {response.StatusCode}."); | |
if (response is UpdatedResponse<Profile> typedUpdateRes) | |
{ | |
System.Console.WriteLine($"{typedUpdateRes.Entity.Id}, {typedUpdateRes.Entity.FirstName} {typedUpdateRes.Entity.LastName}, {typedUpdateRes.Entity.Occupation}"); | |
} | |
var nonExistingDoc = new Profile { Id = "Not-a-real-id" }; | |
response = service.UpdateAsync(nonExistingDoc).Result; | |
System.Console.WriteLine($"\nUpdateAsync. {response.StatusCode}."); | |
if (response is NotFoundResponse typedNotFoundRes) | |
{ | |
System.Console.WriteLine($"{typedNotFoundRes.Id}"); | |
} | |
// get all | |
response = service.GetAllAsync().Result; | |
System.Console.WriteLine($"\nGetAllAsync. {response.StatusCode}."); | |
if (response is QueryResponse<Profile> typedGetAllRes) | |
{ | |
typedGetAllRes.Entities.ToList().ForEach(x => System.Console.WriteLine($"{x.Id}, {x.FirstName} {x.LastName}")); | |
} | |
// query | |
response = service.QueryAsync("SELECT * FROM Profile p WHERE p.firstName = 'Milo'").Result; | |
System.Console.Write($"\nQueryAsync. {response.StatusCode}."); | |
if (response is QueryResponse<Profile> typedQueryRes) | |
{ | |
typedQueryRes.Entities.ToList().ForEach(x => System.Console.WriteLine($"{x.Id}, {x.FirstName} {x.LastName}")); | |
} | |
// count | |
response = service.CountAsync().Result; | |
System.Console.WriteLine($"\nCountAsync. {response.StatusCode}."); | |
if (response.StatusCode == StatusCode.Ok) | |
{ | |
var castedCountResult = response as CountResponse; | |
System.Console.WriteLine($"{castedCountResult.Count}"); | |
} | |
// count by query | |
response = service.CountAsync("SELECT * FROM Profile p WHERE p.firstName = 'Milo'").Result; | |
System.Console.WriteLine($"\nCountAsync by SQL. {response.StatusCode}."); | |
if (response is CountResponse typedCountRes) | |
{ | |
System.Console.WriteLine($"{typedCountRes.Count}"); | |
} | |
// first or default by query | |
response = service.FirstOrDefaultAsync("SELECT * FROM Profile p WHERE p.firstName = 'Milo'").Result; | |
System.Console.WriteLine($"\nFirstOrDefaultAsync. {response.StatusCode}."); | |
if (response is QueryFirstResponse<Profile> typedFirstRes) | |
{ | |
System.Console.WriteLine($"{typedFirstRes.Entity.Id}, {typedFirstRes.Entity.FirstName} {typedFirstRes.Entity.LastName}"); | |
} | |
// delete | |
response = service.DeleteAsync("1234").Result; | |
System.Console.WriteLine($"\nDeleteAsync. {response.StatusCode}."); | |
if (response is DeletedResponse typedDeleteRes) | |
{ | |
System.Console.WriteLine($"{typedDeleteRes.Id}"); | |
} | |
// delete | |
response = service.DeleteAsync("Not-a-real-ID").Result; | |
System.Console.WriteLine($"\nDeleteAsync. {response.StatusCode}."); | |
if (response is NotFoundResponse typedNotFoundDelRes) | |
{ | |
System.Console.WriteLine($"{typedNotFoundDelRes.Id}"); | |
} | |
AddStoredProc(documentClient, databaseName, collectionName, "my_sproc"); | |
response = service.ExecuteStoredProcedureAsync<string>("my_sproc", "Milo").Result; | |
System.Console.WriteLine($"\nExecuteStoredProcedureAsync {response.StatusCode}"); | |
if (response is SprocResponse<string> typedSprocRes) | |
{ | |
System.Console.WriteLine($"{typedSprocRes.Result}"); | |
} | |
// cleanup | |
System.Console.WriteLine($"\nDeleting {databaseName}"); | |
documentClient.DeleteDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName)).Wait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated using pattern matching.
eg.
if (response is CreateResponse typedCreateRes)
{
System.Console.WriteLine($"Success - Id {typedCreateRes.Entity.Id}");
}