-
Notifications
You must be signed in to change notification settings - Fork 1
Generic DataLayer and Testing #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ncostar
wants to merge
11
commits into
main
Choose a base branch
from
remove-person-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e73f896
Remove Person Test
ncostar 89416ea
Fix + Name check
ncostar 6e952d7
CodeCamp Updates
ncostar 0a92796
Remove Person + Tests
ncostar c93e55b
Person class consistent with earlier example
ncostar 68390f9
Get and Update - CodeCamp
ncostar 0510e7f
Added Remove 7 Update Methods + Tests
ncostar d71c3cd
Replace concurrency example with Update method
ncostar 1fad861
Remove method refactor
ncostar 2d5710b
CodeCamp Generic Refactor + Added Interface
ncostar abedc2d
Codecamp amendments
ncostar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using Microsoft.Azure.Cosmos; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace DataLayer | ||
| { | ||
| /// <summary> | ||
| /// This is a Generic Data Access Layer (DAL) object. | ||
| /// </summary> | ||
| public class DataLayer<T> : IDataLayer<T> where T : Model | ||
| { | ||
| private readonly Container _container; | ||
| private string ContainerId { get; set; } | ||
|
|
||
| public DataLayer(CosmosClient client, string databaseId, string containerId) | ||
| { | ||
| ContainerId = containerId; | ||
| _container = client.GetContainer(databaseId, containerId); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets all items. | ||
| /// </summary> | ||
| /// <returns><see cref="IEnumerable{T}"/></returns> | ||
| public async Task<IEnumerable<T>> GetAll() | ||
| { | ||
| // Define a Query | ||
| var query = new QueryDefinition($"SELECT * FROM {ContainerId}"); | ||
|
|
||
| // Get an Iterator | ||
| var iterator = _container.GetItemQueryIterator<T>(query); | ||
|
|
||
| // Get the first page of results | ||
| var response = await iterator.ReadNextAsync(); | ||
|
|
||
| //TODO: Paging | ||
|
|
||
| return response.Resource; | ||
| } | ||
|
|
||
| public async Task Remove(T item) | ||
| => await _container.DeleteItemAsync<T>( | ||
| item.Id, | ||
| new PartitionKey(item.Id), | ||
| new ItemRequestOptions { IfMatchEtag = item.ETag }); | ||
|
|
||
| public async Task<T> Create(T item) | ||
| { | ||
| var response = await _container.CreateItemAsync(item); | ||
| return response.Resource; | ||
| } | ||
|
|
||
| public async Task<T> Get(string id) | ||
| { | ||
| var response = await _container.ReadItemAsync<T>(id, new PartitionKey(id)); | ||
| return response.Resource; | ||
| } | ||
|
|
||
| public async Task<T> Update(T item) | ||
| { | ||
| var response = await _container.ReplaceItemAsync(item, | ||
| item.Id, | ||
| new PartitionKey(item.Id), | ||
| new ItemRequestOptions { IfMatchEtag = item.ETag }); | ||
| return response.Resource; | ||
| } | ||
|
|
||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace DataLayer | ||
| { | ||
| public interface IDataLayer<T> where T : Model | ||
| { | ||
| Task<T> Create(T item); | ||
| Task<T> Get(string id); | ||
| Task<IEnumerable<T>> GetAll(); | ||
| Task Remove(T item); | ||
| Task<T> Update(T item); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using Newtonsoft.Json; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace DataLayer | ||
| { | ||
| public abstract class Model | ||
| { | ||
| [JsonProperty("id")] | ||
| public string Id { get; set; } | ||
|
|
||
| [JsonProperty("_eTag")] | ||
| public string ETag { get; set; } | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using System; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Person.cs should be moved to the Test project and deleted from this project as per #2 |
||
| using Newtonsoft.Json; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
@@ -10,8 +11,15 @@ namespace DataLayer | |
| /// A Person Model | ||
| /// </summary> | ||
| /// <remarks>AKA DTO (data transfer object), POCO (plain old c# object)</remarks> | ||
| public class Person | ||
| public class Person : Model | ||
| { | ||
| //TODO: Properties | ||
| public string FirstName { get; set; } | ||
|
|
||
| public string LastName { get; set; } | ||
|
|
||
| public double HoursWorked { get; set; } | ||
|
|
||
| public string Phone { get; set; } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace DataLayer | ||
| { | ||
| public class Pet : Model | ||
| { | ||
| public string Name { get; set; } | ||
|
|
||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
and this should serialise / de-serialise from Cosmos without the need for an ItemWithETag helper