-
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
base: main
Are you sure you want to change the base?
Changes from 10 commits
e73f896
89416ea
6e952d7
0a92796
c93e55b
68390f9
0510e7f
d71c3cd
1fad861
2d5710b
abedc2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| 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 ItemWithETag(response); | ||
| } | ||
|
|
||
| public async Task<T> Get(string id) | ||
| { | ||
| var response = await _container.ReadItemAsync<T>(id, new PartitionKey(id)); | ||
| return ItemWithETag(response); | ||
| } | ||
|
|
||
| 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 ItemWithETag(response); | ||
| } | ||
| /// <summary> | ||
| /// Method taking the response object and parsing it into a item with the ETag property extracted. | ||
| /// </summary> | ||
| private T ItemWithETag(ItemResponse<T> response) | ||
| { | ||
| T item = response.Resource; | ||
| item.ETag = response.ETag; | ||
| return item; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 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. MockPeopleData can be deleted |
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace DataLayer | ||
| { | ||
| public class MockPeopleData : IDataLayer<Person> | ||
| { | ||
| public Task<Person> Create(Person item) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public Task<Person> Get(string id) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public Task<IEnumerable<Person>> GetAll() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public Task Remove(Person item) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| public Task<Person> Update(Person item) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| 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; } | ||
|
|
||
| public string ETag { get; set; } | ||
|
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. Add [JsonProperty("_etag")]and this should serialise / de-serialise from Cosmos without the need for an ItemWithETag helper |
||
| } | ||
| } | ||
This file was deleted.
| 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; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using System; | ||
|
ncostar marked this conversation as resolved.
|
||
| 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; } | ||
|
|
||
| } | ||
| } | ||
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.
Actually, I think we can just decorate the ETag property with a JsonProperty. See the Model class to see what I mean.