Skip to content

Commit

Permalink
Snack machine repository
Browse files Browse the repository at this point in the history
EnOSeChi committed Mar 13, 2021
1 parent ceda4ee commit dde3a34
Showing 3 changed files with 62 additions and 0 deletions.
29 changes: 29 additions & 0 deletions DDDInPractice.Application/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using DDDInPractice.Logic;
using System;
using System.Collections.Generic;
using System.Text;

namespace DDDInPractice.Application
{
public class Repository<T> where T : AggregateRoot
{
public T GetById(long id)
{
using (var ctx = ContextFactory.DefaultContext())
{
return ctx.Find<T>(id);
}
}

public void Save(T aggregateRoot)
{
using (var ctx = ContextFactory.DefaultContext())
using(var tran = ctx.Database.BeginTransaction())
{
ctx.Update(aggregateRoot);
ctx.SaveChanges();
tran.Commit();
}
}
}
}
11 changes: 11 additions & 0 deletions DDDInPractice.Application/SnackMachineRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using DDDInPractice.Logic;
using System;
using System.Collections.Generic;
using System.Text;

namespace DDDInPractice.Application
{
public class SnackMachineRepository : Repository<SnackMachine>
{
}
}
22 changes: 22 additions & 0 deletions DDDInPractice.Logic.Tests/TemporaryTests.cs
Original file line number Diff line number Diff line change
@@ -10,6 +10,28 @@ namespace DDDInPractice.Logic.Tests
{
public class TemporaryTests
{
[Fact]
public void Test3()
{
SnackMachine snackMachine;
using (var ctx = ContextFactory.DefaultContext())
{

ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
snackMachine = new SnackMachine();
snackMachine.LoadSnack(1, new SnackPile(new Snack("Chips"), 10, 0.01m));
snackMachine.InsertMoney(Money.OneCent);
snackMachine.BuySnack(1);

ctx.SnackMachines.Add(snackMachine);
ctx.SaveChanges();
}

var repository = new SnackMachineRepository();
var snackMachine2 = repository.GetById(snackMachine.Id);
}

[Fact]
public void Test()
{

0 comments on commit dde3a34

Please sign in to comment.