Created
December 1, 2019 16:59
-
-
Save JYCabello/a5b1abf781c4c2786328f856b6386652 to your computer and use it in GitHub Desktop.
Imperative approach to insertOrUpdate
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
namespace Sandbox.InserOrUpdate | |
{ | |
public class FruitRepository | |
{ | |
public int InsertOrUpdate(FruitMetadata fruit) | |
{ | |
var id = FindByAuthorityId(fruit.FruitAuthorityId) ?? FindByCouncilId(fruit.FruitCouncilId) ?? FindByName(fruit.Name); | |
return id.HasValue ? Update(id.Value, fruit) : Insert(fruit); | |
} | |
// Actual database access has been ommited for brevity. | |
private int? FindByAuthorityId(int authorityId) => null; | |
private int? FindByCouncilId(int councilId) => null; | |
private int? FindByName(string name) => null; | |
private int Insert(FruitMetadata fruit) => 1; | |
private int Update(int id, FruitMetadata fruit) => id; | |
} | |
public class FruitMetadata | |
{ | |
public string Color { get; set; } | |
public int FruitAuthorityId { get; set; } | |
public int FruitCouncilId { get; set; } | |
public string Name { get; set; } | |
public string Shape { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment