Skip to content
Permalink
Browse files

Adding Entity

  • Loading branch information...
farajfarook committed Feb 27, 2019
1 parent 702f1bf commit acd88b2c34504f67d22a3ba568a0cd389cf45872
Showing with 24 additions and 20 deletions.
  1. +21 −17 Enbiso.NLib.Domain/Entity.cs
  2. +3 −3 Enbiso.NLib.Tests/Enbiso.NLib.Domain.Tests/EntityTests.cs
@@ -23,32 +23,36 @@ public interface IEntityEvent
/// </summary>
public interface IEntity
{
List<IEntityEvent> Events { get; set; }
List<IEntityEvent> GetEvents();
}

/// <inheritdoc />
/// <summary>
/// Domain Entity abstraction
/// </summary>
/// <typeparam name="TKey"></typeparam>
public abstract class Entity<TKey>: IEntity
public abstract class Entity: IEntity
{
private int? _requestedHashCode;
public virtual TKey Id { get; set; }


public List<IEntityEvent> Events { get; set; } = new List<IEntityEvent>();

private readonly List<IEntityEvent> _events = new List<IEntityEvent>();

public void AddEvent(IEntityEvent @event)
{
Events.Add(@event?? throw new ArgumentNullException());
_events.Add(@event?? throw new ArgumentNullException());
}

public void RemoveEvent(IEntityEvent @event)
{
Events.Remove(@event ?? throw new ArgumentNullException());
_events.Remove(@event ?? throw new ArgumentNullException());
}

public List<IEntityEvent> GetEvents() => _events;
}

/// <inheritdoc />
/// <summary>
/// Domain Entity abstraction
/// </summary>
/// <typeparam name="TKey"></typeparam>
public abstract class Entity<TKey>: Entity
{
private int? _requestedHashCode;
public virtual TKey Id { get; set; }

public bool IsTransient()
{
return default(TKey)?.Equals(Id) ?? true;
@@ -103,13 +107,13 @@ public static void ClearEvents(this IEnumerable<IEntity> entities)
{
foreach (var entity in entities)
{
entity.Events.Clear();
entity.GetEvents().Clear();
}
}

public static IEnumerable<TEvent> GetEvents<TEvent>(this IEnumerable<IEntity> entities) where TEvent: IEntityEvent
{
return entities.ToList().SelectMany(e => e.Events).OfType<TEvent>();
return entities.ToList().SelectMany(e => e.GetEvents()).OfType<TEvent>();
}
}
}
@@ -43,10 +43,10 @@ public void AddDomainEvent_ListOfIDomainEvent_Success()
{
var entity = _fixture.Create<Entity<Guid>>();
var events = _fixture.Create<List<IEntityEvent>>();
entity.Events.Clear();
entity.GetEvents().Clear();

events.ForEach(entity.AddEvent);
Assert.Equal(entity.Events.Count, events.Count);
Assert.Equal(entity.GetEvents().Count, events.Count);
}

[Fact]
@@ -57,7 +57,7 @@ public void RemoveDomainEvent_ValidEvent_Success()

events.ForEach(entity.AddEvent);
entity.RemoveEvent(events.First());
Assert.Equal(entity.Events.Count, events.Count - 1);
Assert.Equal(entity.GetEvents().Count, events.Count - 1);
}


0 comments on commit acd88b2

Please sign in to comment.
You can’t perform that action at this time.