Skip to content

Commit c3ba229

Browse files
committed
phase 1: more test, more fix 🐛
1 parent 9317dfe commit c3ba229

File tree

6 files changed

+81
-24
lines changed

6 files changed

+81
-24
lines changed

.vs/netcorekit/v16/.suo

6 KB
Binary file not shown.

src/NetCoreKit.Domain/AggregateRoot.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public abstract class AggregateRootWithIdBase<TId> : EntityWithIdBase<TId>, IAgg
3232
private readonly IDictionary<Type, Action<object>> _handlers = new ConcurrentDictionary<Type, Action<object>>();
3333
private readonly List<IEvent> _uncommittedEvents = new List<IEvent>();
3434

35+
protected AggregateRootWithIdBase() : this(default)
36+
{
37+
}
38+
3539
protected AggregateRootWithIdBase(TId id) : base(id)
3640
{
3741
Created = GenerateDateTime();

src/NetCoreKit.Domain/Entity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IEntity : IEntityWithId<Guid>
1111

1212
/// <inheritdoc />
1313
/// <summary>
14-
/// Supertype for all Entity types
14+
/// Supertype for all Entity types
1515
/// </summary>
1616
public interface IEntityWithId<TId> : IIdentityWithId<TId>
1717
{
@@ -30,7 +30,7 @@ protected EntityBase(Guid id) : base(id)
3030

3131
/// <inheritdoc />
3232
/// <summary>
33-
/// Source: https://github.com/VaughnVernon/IDDD_Samples_NET
33+
/// Source: https://github.com/VaughnVernon/IDDD_Samples_NET
3434
/// </summary>
3535
public abstract class EntityWithIdBase<TId> : IEntityWithId<TId>
3636
{

src/NetCoreKit.Infrastructure.EfCore.Tests/NetCoreKit.Infrastructure.EfCore.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.2</TargetFramework>
5-
5+
<LangVersion>7.3</LangVersion>
66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

src/NetCoreKit.Infrastructure.EfCore.Tests/RepositoryTest.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,24 @@
1010

1111
namespace NetCoreKit.Infrastructure.EfCore.Tests
1212
{
13+
public class TestEvent : EventBase
14+
{
15+
}
16+
1317
public class TestEntity : AggregateRootBase
1418
{
19+
public TestEntity()
20+
{
21+
AddEvent(new TestEvent());
22+
}
23+
}
24+
25+
public class TestEntityWithId : AggregateRootWithIdBase<int>
26+
{
27+
public TestEntityWithId()
28+
{
29+
AddEvent(new TestEvent());
30+
}
1531
}
1632

1733
public class TestDbContext : AppDbContext
@@ -46,9 +62,16 @@ public async Task CanCommandOnGenericRepo()
4662
{
4763
// command
4864
var uow = _services.BuildServiceProvider().GetService<IUnitOfWorkAsync>();
65+
66+
// without id type, default is Guid
4967
var testCommandRepo = uow.RepositoryAsync<TestEntity>();
5068
await testCommandRepo.AddAsync(new TestEntity());
5169
await testCommandRepo.AddAsync(new TestEntity());
70+
71+
// with int type for id
72+
var testCommandRepoWithId = uow.RepositoryAsync<TestEntityWithId, int>();
73+
await testCommandRepoWithId.AddAsync(new TestEntityWithId());
74+
5275
var result = await uow.SaveChangesAsync(default);
5376

5477
// assert
@@ -60,11 +83,18 @@ public async Task CanQueryOnGenericRepo()
6083
{
6184
// query
6285
var repoFactory = _services.BuildServiceProvider().GetService<IQueryRepositoryFactory>();
86+
87+
// without id type, default is Guid
6388
var testQueryRepo = repoFactory.QueryRepository<TestEntity>();
64-
var result = await testQueryRepo.ListAsync<TestDbContext, TestEntity>();
89+
var result1 = await testQueryRepo.ListAsync<TestDbContext, TestEntity>();
90+
91+
// with int type for id
92+
var testQueryRepoWithId = repoFactory.QueryRepository<TestEntityWithId, int>();
93+
var result2 = await testQueryRepoWithId.ListAsync<TestDbContext, TestEntityWithId, int>();
6594

6695
// assert
67-
Assert.NotNull(result.ToList());
96+
Assert.NotNull(result1.ToList());
97+
Assert.NotNull(result2.ToList());
6898
}
6999
}
70100
}

src/NetCoreKit.Infrastructure.EfCore/Db/AppDbContext.cs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,39 +54,62 @@ public override int SaveChanges()
5454
}
5555

5656
/// <summary>
57-
/// Source:
58-
/// https://github.com/ardalis/CleanArchitecture/blob/master/src/CleanArchitecture.Infrastructure/Data/AppDbContext.cs
57+
/// Source: https://github.com/ardalis/CleanArchitecture/blob/master/src/CleanArchitecture.Infrastructure/Data/AppDbContext.cs
5958
/// </summary>
6059
private void SaveChangesWithEvents(IDomainEventDispatcher domainEventDispatcher)
6160
{
62-
var entitiesWithEvents = ChangeTracker
63-
.Entries()
64-
.Select(e => e.Entity)
61+
var entities = ChangeTracker.Entries().Select(e => e.Entity);
62+
63+
entities
6564
.Where(e =>
6665
!e.GetType().BaseType.IsGenericType &&
6766
typeof(AggregateRootBase).IsAssignableFrom(e.GetType()))
68-
.Where(e => ((IAggregateRoot)e).GetUncommittedEvents().Any())
67+
.Select(aggregateRoot =>
68+
{
69+
var events = ((IAggregateRoot)aggregateRoot).GetUncommittedEvents();
70+
71+
foreach (var domainEvent in events)
72+
domainEventDispatcher.Dispatch(domainEvent);
73+
74+
((IAggregateRoot)aggregateRoot).GetUncommittedEvents().Clear();
75+
return aggregateRoot;
76+
})
6977
.ToArray();
7078

71-
foreach (var entity in entitiesWithEvents)
72-
{
73-
var events = ((IAggregateRoot)entity).GetUncommittedEvents().ToArray();
74-
((IAggregateRoot)entity).GetUncommittedEvents().Clear();
75-
foreach (var domainEvent in events)
76-
domainEventDispatcher.Dispatch(domainEvent);
77-
}
79+
entities
80+
.Where(e =>
81+
e.GetType().BaseType.IsGenericType &&
82+
typeof(AggregateRootWithIdBase<>).IsAssignableFrom(e.GetType().BaseType.GetGenericTypeDefinition()))
83+
.Select(aggregateRoot =>
84+
{
85+
//todo: need a better code to avoid dynamic
86+
var events = ((dynamic)aggregateRoot).GetUncommittedEvents();
87+
88+
foreach (var domainEvent in events)
89+
domainEventDispatcher.Dispatch(domainEvent);
90+
91+
((dynamic)aggregateRoot).GetUncommittedEvents().Clear();
92+
return aggregateRoot;
93+
})
94+
.ToArray();
7895
}
7996

8097
private static void RegisterEntities(ModelBuilder modelBuilder, IEnumerable<Type> typeToRegisters)
8198
{
8299
var concreteTypes = typeToRegisters.Where(x => !x.GetTypeInfo().IsAbstract && !x.GetTypeInfo().IsInterface);
83-
var types = concreteTypes
84-
.Where(x =>
85-
typeof(EntityBase).IsAssignableFrom(x) ||
86-
typeof(AggregateRootBase).IsAssignableFrom(x)
87-
);
100+
var types = new List<Type>();
88101

89-
foreach (var type in types) modelBuilder.Entity(type);
102+
foreach (var concreteType in concreteTypes)
103+
{
104+
if (concreteType.BaseType != null &&
105+
(typeof(AggregateRootBase).IsAssignableFrom(concreteType) ||
106+
(concreteType.GetTypeInfo().BaseType.IsGenericType &&
107+
typeof(AggregateRootWithIdBase<>).IsAssignableFrom(concreteType.GetTypeInfo().BaseType.GetGenericTypeDefinition())
108+
)))
109+
{
110+
modelBuilder.Entity(concreteType);
111+
}
112+
}
90113
}
91114

92115
private static void RegisterConvention(ModelBuilder modelBuilder)

0 commit comments

Comments
 (0)