Skip to content

Instantly share code, notes, and snippets.

@JYCabello
Created December 1, 2019 16:59
Show Gist options
  • Save JYCabello/a5b1abf781c4c2786328f856b6386652 to your computer and use it in GitHub Desktop.
Save JYCabello/a5b1abf781c4c2786328f856b6386652 to your computer and use it in GitHub Desktop.
Imperative approach to insertOrUpdate
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