Skip to content

Commit

Permalink
🎨 Improve id type
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagor87 committed Jul 21, 2019
1 parent 139c2d5 commit d444de5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Core/AggregateRoot.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Seedwork.DomainDriven.Core
{
public abstract class AggregateRoot : Entity
public abstract class AggregateRoot<TId> : Entity<TId>
{
protected AggregateRoot(object id) : base(id)
protected AggregateRoot(TId id) : base(id)
{
}

Expand Down
16 changes: 8 additions & 8 deletions src/Core/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Seedwork.DomainDriven.Core
{
public abstract class Entity : IEquatable<Entity>
public abstract class Entity<TId> : IEquatable<Entity<TId>>
{
private readonly List<DomainEvent> _domainEvents;
private readonly Guid _transientId;

protected Entity(object id) : this()
protected Entity(TId id) : this()
{
Id = id;
}
Expand All @@ -21,12 +21,12 @@ protected Entity()
_transientId = Guid.NewGuid();
}

public object Id { get; protected set; }
public TId Id { get; protected set; }
public ReadOnlyCollection<DomainEvent> DomainEvents => _domainEvents.AsReadOnly();

public DateTime CreatedAt { get; private set; }

public virtual bool Equals(Entity other)
public virtual bool Equals(Entity<TId> other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
Expand All @@ -49,13 +49,13 @@ public void ClearDomainEvents()
_domainEvents.Clear();
}

public static bool operator ==(Entity left, Entity right)
public static bool operator ==(Entity<TId> left, Entity<TId> right)
{
if (ReferenceEquals(left, null) || ReferenceEquals(right, null)) return false;
return left.Equals(right);
}

public static bool operator !=(Entity left, Entity right)
public static bool operator !=(Entity<TId> left, Entity<TId> right)
{
return !(left == right);
}
Expand All @@ -67,7 +67,7 @@ public override int GetHashCode()

private bool IsTransient()
{
return Id == null;
return Equals(Id, default(TId));
}

private object GetId()
Expand All @@ -78,7 +78,7 @@ private object GetId()

public override bool Equals(object obj)
{
return obj is Entity other && Equals(other);
return obj is Entity<TId> other && Equals(other);
}
}
}
2 changes: 1 addition & 1 deletion tests/UnitTests/Stubs/OrderAgg/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Seedwork.DomainDriven.UnitTests.Stubs.OrderAgg
{
public class Order : AggregateRoot
public class Order : AggregateRoot<long>
{
public Order(long id) : base(id)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTests/Stubs/PersonAgg/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Seedwork.DomainDriven.UnitTests.Stubs.PersonAgg
{
public class Person : AggregateRoot
public class Person : AggregateRoot<long>
{
public Person(long id, Name name) : base(id)
{
Expand Down
21 changes: 21 additions & 0 deletions tests/UnitTests/ValueObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ public void Given_value_object_when_value_null_should_return_zero(string value)
name.GetHashCode().Should().Be(0);
}

[Fact(DisplayName = @"GIVEN value_object, WHEN other null, SHOULD be different")]
public void Given_value_object_when_other_null_should_be_different()
{
var name = new Name("Name");
name.Equals((object) null).Should().BeFalse();
}

[Fact(DisplayName = @"GIVEN value_object, WHEN same reference, SHOULD be equals")]
public void Given_value_object_when_same_reference_as_object_should_be_equal()
{
var name = new Name("Name");
name.Equals((object) name).Should().BeTrue();
}

[Fact(DisplayName = @"GIVEN value_object, WHEN same reference, SHOULD be equals")]
public void Given_value_object_when_same_reference_should_be_equal()
{
var name = new Name("Name");
name.Equals(name).Should().BeTrue();
}

[Fact(DisplayName = @"GIVEN value objects, WHEN some is null, SHOULD not be null")]
public void Given_value_objects_when_some_null_should_not_be_equal()
{
Expand Down

0 comments on commit d444de5

Please sign in to comment.