Skip to content

Commit

Permalink
Implement simple repository pattern (#1)
Browse files Browse the repository at this point in the history
Implement simple repository pattern
  • Loading branch information
mehmetseckin authored Oct 15, 2019
2 parents aeef9e8 + 1b10221 commit 05fbf2a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Todo.CLI/Handlers/ListCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static ICommandHandler Create(IServiceProvider serviceProvider)
{
return CommandHandler.Create(async () =>
{
var todoItemRetriever = (ITodoItemRetriever)serviceProvider.GetService(typeof(ITodoItemRetriever));
var todoItemRetriever = (ITodoItemRepository)serviceProvider.GetService(typeof(ITodoItemRepository));
var todoItems = await todoItemRetriever.ListAsync();
foreach (var item in todoItems)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Todo.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Todo.Core;
using Microsoft.Graph;
using Todo.CLI.Auth;
using Todo.Core.Repository;

namespace Todo.CLI
{
Expand All @@ -25,7 +26,7 @@ static int Main(string[] args)

var services = new ServiceCollection()
.AddSingleton(typeof(TodoCliConfiguration), todoCliConfig)
.AddTransient<ITodoItemRetriever>(factory => new TodoItemRetriever(TodoCliAuthenticationProviderFactory.GetAuthenticationProvider(factory)));
.AddTransient<ITodoItemRepository>(factory => new TodoItemRepository(TodoCliAuthenticationProviderFactory.GetAuthenticationProvider(factory)));

var serviceProvider = services.BuildServiceProvider();

Expand Down
11 changes: 11 additions & 0 deletions src/Todo.Core/Model/TodoItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Todo.Core.Model
{
public class TodoItem
{
public string Subject { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Todo.Core.Model;

namespace Todo.Core
{
public interface ITodoItemRetriever
public interface ITodoItemRepository
{
Task<IEnumerable<OutlookTask>> ListAsync();
Task<IEnumerable<TodoItem>> ListAsync();
}
}
17 changes: 17 additions & 0 deletions src/Todo.Core/Repository/RepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.Text;

namespace Todo.Core.Repository
{
public abstract class RepositoryBase
{
protected IAuthenticationProvider AuthenticationProvider { get; }

public RepositoryBase(IAuthenticationProvider authenticationProvider)
{
AuthenticationProvider = authenticationProvider;
}
}
}
28 changes: 28 additions & 0 deletions src/Todo.Core/Repository/TodoItemRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Graph;
using Todo.Core.Model;

namespace Todo.Core.Repository
{
public class TodoItemRepository : RepositoryBase, ITodoItemRepository
{
public TodoItemRepository(IAuthenticationProvider authenticationProvider)
: base(authenticationProvider)
{
}

public async Task<IEnumerable<TodoItem>> ListAsync()
{
var graphServiceClient = new GraphServiceClient(AuthenticationProvider);
var tasks = await graphServiceClient.Me.Outlook.Tasks.Request().GetAsync();
return tasks.Select(task => new TodoItem()
{
Subject = task.Subject
});
}
}
}
24 changes: 0 additions & 24 deletions src/Todo.Core/TodoItemRetriever.cs

This file was deleted.

0 comments on commit 05fbf2a

Please sign in to comment.