Skip to content

Commit

Permalink
TooManyIfs and EfficientDoesntExist
Browse files Browse the repository at this point in the history
  • Loading branch information
leosperry committed Dec 4, 2020
1 parent ef2fdf1 commit eaa36a6
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Samples/EfficientDoesntExist/AccountLogic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Samples.EfficientDoesntExist
{
class Account
{

}

interface IAccountProvider
{
Task<Account> GetById(int id);
}

interface ILogger
{
void LogException(Exception ex);
}

class AccountLogic
{
ILogger _logger;
IAccountProvider _acctProvider;

public async Task<Account> GetById(int id)
{
try
{
return await _acctProvider.GetById(id);
}
catch (Exception ex)
{
_logger.LogException(ex);
throw;
}
}
}
}
19 changes: 19 additions & 0 deletions Samples/EfficientDoesntExist/CategoryProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Samples.EfficientDoesntExist
{
class Category
{ }

class CategoryProvider
{
Task<IEnumerable<Category>> GetGatagories(bool includeEmpty = false)
{
//call database with parameter
return default;
}
}
}
6 changes: 6 additions & 0 deletions Samples/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ namespace Samples
{
class Misc
{

void Asdf()
{
List<int> ints = new List<int>();
ints.BinarySearch
}
}
}
85 changes: 85 additions & 0 deletions Samples/TooManyIfs/BookController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Samples.TooManyIfs
{
class Book
{

}

class Category
{
public int ID { get; set; }
}

class Author { }

interface IBookProvider
{
Task<IEnumerable<Book>> Search(string query);
Task Create(string title, string description, int authorId, IEnumerable<int> categoryIds);
}

interface IAuthorProvider
{
Task<Author> GetById(int authorId);
}

interface ICategoryProvider
{
Task<Category> GetById(int cid);
}

interface IBookLogic
{

}

class BookController
{
IAuthorProvider _authorProvider;
IBookProvider _bookProvider;
ICategoryProvider _catProvider;

public Task<IEnumerable<Book>> Search(string query)
{
if (string.IsNullOrEmpty(query) || query.Length > 50)
{
// return non-200
throw new Exception("meaningful error");
}
else
{
return _bookProvider.Search(query);
}
}

public async Task Create(string title, string description, int authorId, IEnumerable<int> categoryIds)
{
if (string.IsNullOrEmpty(title) && authorId > 0)
{
if (await _authorProvider.GetById(authorId) != null)
{
if (categoryIds == null)
{
categoryIds = Enumerable.Empty<int>();
}
var catTasks = categoryIds.Select(cid => _catProvider.GetById(cid));
var categories = await Task.WhenAll(catTasks);

if (categories.All(c => c != null))
{
await _bookProvider.Create(title, description, authorId, categoryIds);
return;
}
}
}
//return non-200
}
}

}

0 comments on commit eaa36a6

Please sign in to comment.