-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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; | ||
} | ||
} | ||
} | ||
} |
This file contains 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,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; | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -6,5 +6,11 @@ namespace Samples | |
{ | ||
class Misc | ||
{ | ||
|
||
void Asdf() | ||
{ | ||
List<int> ints = new List<int>(); | ||
ints.BinarySearch | ||
} | ||
} | ||
} |
This file contains 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,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 | ||
} | ||
} | ||
|
||
} |