Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Src/DDD.Application/AutoMapper/AutoMapperConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ public class AutoMapperConfig
// });
// }

public static Type[] RegisterMappings()
{
return new Type[]
public static Type[] RegisterMappings() => new Type[]
{
typeof(DomainToViewModelMappingProfile),
typeof(ViewModelToDomainMappingProfile),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@ namespace DDD.Application.AutoMapper;

public class DomainToViewModelMappingProfile : Profile
{
public DomainToViewModelMappingProfile()
{
CreateMap<Customer, CustomerViewModel>();
}
public DomainToViewModelMappingProfile() => CreateMap<Customer, CustomerViewModel>();
}
56 changes: 18 additions & 38 deletions Src/DDD.Application/Services/CustomerAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,25 @@

namespace DDD.Application.Services;

public class CustomerAppService : ICustomerAppService
public class CustomerAppService(
IMapper mapper,
ICustomerRepository customerRepository,
IMediatorHandler bus,
IEventStoreRepository eventStoreRepository) : ICustomerAppService
{
private readonly IMapper _mapper;
private readonly ICustomerRepository _customerRepository;
private readonly IEventStoreRepository _eventStoreRepository;
private readonly IMediatorHandler _bus;
private readonly IMapper _mapper = mapper;
private readonly ICustomerRepository _customerRepository = customerRepository;
private readonly IEventStoreRepository _eventStoreRepository = eventStoreRepository;
private readonly IMediatorHandler _bus = bus;

public CustomerAppService(
IMapper mapper,
ICustomerRepository customerRepository,
IMediatorHandler bus,
IEventStoreRepository eventStoreRepository)
{
_mapper = mapper;
_customerRepository = customerRepository;
_bus = bus;
_eventStoreRepository = eventStoreRepository;
}
public IEnumerable<CustomerViewModel> GetAll() =>
_customerRepository.GetAll().ProjectTo<CustomerViewModel>(_mapper.ConfigurationProvider);

public IEnumerable<CustomerViewModel> GetAll()
{
return _customerRepository.GetAll().ProjectTo<CustomerViewModel>(_mapper.ConfigurationProvider);
}
public IEnumerable<CustomerViewModel> GetAll(int skip, int take) =>
_customerRepository.GetAll(new CustomerFilterPaginatedSpecification(skip, take))
.ProjectTo<CustomerViewModel>(_mapper.ConfigurationProvider);

public IEnumerable<CustomerViewModel> GetAll(int skip, int take)
{
return _customerRepository.GetAll(new CustomerFilterPaginatedSpecification(skip, take))
.ProjectTo<CustomerViewModel>(_mapper.ConfigurationProvider);
}

public CustomerViewModel GetById(Guid id)
{
return _mapper.Map<CustomerViewModel>(_customerRepository.GetById(id));
}
public CustomerViewModel GetById(Guid id) => _mapper.Map<CustomerViewModel>(_customerRepository.GetById(id));

public void Register(CustomerViewModel customerViewModel)
{
Expand All @@ -68,13 +53,8 @@ public void Remove(Guid id)
_bus.SendCommand(removeCommand);
}

public IList<CustomerHistoryData> GetAllHistory(Guid id)
{
return CustomerHistory.ToJavaScriptCustomerHistory(_eventStoreRepository.All(id));
}
public IList<CustomerHistoryData> GetAllHistory(Guid id) =>
CustomerHistory.ToJavaScriptCustomerHistory(_eventStoreRepository.All(id));

public void Dispose()
{
GC.SuppressFinalize(this);
}
public void Dispose() => GC.SuppressFinalize(this);
}
3 changes: 2 additions & 1 deletion Src/DDD.CLI.Migration/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Configuration;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -48,6 +48,7 @@ static void Main(string[] args)
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Success!");
Console.ResetColor();

return;
}
}
5 changes: 1 addition & 4 deletions Src/DDD.Domain.Core/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public abstract class Command : Message

public ValidationResult ValidationResult { get; set; }

protected Command()
{
Timestamp = DateTime.Now;
}
protected Command() => Timestamp = DateTime.Now;

public abstract bool IsValid();
}
5 changes: 1 addition & 4 deletions Src/DDD.Domain.Core/Events/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ public abstract class Event : Message, INotification
{
public DateTime Timestamp { get; private set; }

protected Event()
{
Timestamp = DateTime.Now;
}
protected Event() => Timestamp = DateTime.Now;
}
5 changes: 1 addition & 4 deletions Src/DDD.Domain.Core/Events/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ public abstract class Message : IRequest<bool>

public Guid AggregateId { get; protected set; }

protected Message()
{
MessageType = GetType().Name;
}
protected Message() => MessageType = GetType().Name;
}
15 changes: 3 additions & 12 deletions Src/DDD.Domain.Core/Models/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,9 @@ public override bool Equals(object obj)
return a.Equals(b);
}

public static bool operator !=(Entity a, Entity b)
{
return !(a == b);
}
public static bool operator !=(Entity a, Entity b) => !(a == b);

public override int GetHashCode()
{
return (GetType().GetHashCode() * 907) + Id.GetHashCode();
}
public override int GetHashCode() => (GetType().GetHashCode() * 907) + Id.GetHashCode();

public override string ToString()
{
return GetType().Name + " [Id=" + Id + "]";
}
public override string ToString() => GetType().Name + " [Id=" + Id + "]";
}
15 changes: 3 additions & 12 deletions Src/DDD.Domain.Core/Models/ValueObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@ namespace DDD.Domain.Core.Models;
public abstract class ValueObject<T>
where T : ValueObject<T>
{
public override bool Equals(object obj)
{
return obj is T valueObject && EqualsCore(valueObject);
}
public override bool Equals(object obj) => obj is T valueObject && EqualsCore(valueObject);

public override int GetHashCode()
{
return GetHashCodeCore();
}
public override int GetHashCode() => GetHashCodeCore();

protected abstract int GetHashCodeCore();

Expand All @@ -32,8 +26,5 @@ public override int GetHashCode()
return a.Equals(b);
}

public static bool operator !=(ValueObject<T> a, ValueObject<T> b)
{
return !(a == b);
}
public static bool operator !=(ValueObject<T> a, ValueObject<T> b) => !(a == b);
}
18 changes: 5 additions & 13 deletions Src/DDD.Domain.Core/Notifications/DomainNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@

namespace DDD.Domain.Core.Notifications;

public class DomainNotification : Event
public class DomainNotification(string key, string value) : Event
{
public Guid DomainNotificationId { get; private set; }
public Guid DomainNotificationId { get; private set; } = Guid.NewGuid();

public string Key { get; private set; }
public string Key { get; private set; } = key;

public string Value { get; private set; }
public string Value { get; private set; } = value;

public int Version { get; private set; }

public DomainNotification(string key, string value)
{
DomainNotificationId = Guid.NewGuid();
Version = 1;
Key = key;
Value = value;
}
public int Version { get; private set; } = 1;
}
20 changes: 4 additions & 16 deletions Src/DDD.Domain.Core/Notifications/DomainNotificationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ public class DomainNotificationHandler : INotificationHandler<DomainNotification
{
private List<DomainNotification> _notifications;

public DomainNotificationHandler()
{
_notifications = new List<DomainNotification>();
}
public DomainNotificationHandler() => _notifications = new List<DomainNotification>();

public Task Handle(DomainNotification message, CancellationToken cancellationToken)
{
Expand All @@ -23,18 +20,9 @@ public Task Handle(DomainNotification message, CancellationToken cancellationTok
return Task.CompletedTask;
}

public virtual List<DomainNotification> GetNotifications()
{
return _notifications;
}
public virtual List<DomainNotification> GetNotifications() => _notifications;

public virtual bool HasNotifications()
{
return GetNotifications().Any();
}
public virtual bool HasNotifications() => GetNotifications().Any();

public void Dispose()
{
_notifications = new List<DomainNotification>();
}
public void Dispose() => _notifications = new List<DomainNotification>();
}
16 changes: 5 additions & 11 deletions Src/DDD.Domain/CommandHandlers/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@

namespace DDD.Domain.CommandHandlers;

public class CommandHandler
public class CommandHandler(IUnitOfWork uow, IMediatorHandler bus, INotificationHandler<DomainNotification> notifications)
{
private readonly IUnitOfWork _uow;
private readonly IMediatorHandler _bus;
private readonly DomainNotificationHandler _notifications;

public CommandHandler(IUnitOfWork uow, IMediatorHandler bus, INotificationHandler<DomainNotification> notifications)
{
_uow = uow;
_notifications = (DomainNotificationHandler)notifications;
_bus = bus;
}
private readonly IUnitOfWork _uow = uow;
private readonly IMediatorHandler _bus = bus;
private readonly DomainNotificationHandler _notifications = (DomainNotificationHandler)notifications;

public bool Commit()
{
Expand All @@ -33,6 +26,7 @@ public bool Commit()
}

_bus.RaiseEvent(new DomainNotification("Commit", "We had a problem during saving your data."));

return false;
}

Expand Down
31 changes: 13 additions & 18 deletions Src/DDD.Domain/CommandHandlers/CustomerCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,24 @@

namespace DDD.Domain.CommandHandlers;

public class CustomerCommandHandler : CommandHandler,
public class CustomerCommandHandler(
ICustomerRepository customerRepository,
IUnitOfWork uow,
IMediatorHandler bus,
INotificationHandler<DomainNotification> notifications) : CommandHandler(uow, bus, notifications),
IRequestHandler<RegisterNewCustomerCommand, bool>,
IRequestHandler<UpdateCustomerCommand, bool>,
IRequestHandler<RemoveCustomerCommand, bool>
{
private readonly ICustomerRepository _customerRepository;
private readonly IMediatorHandler _bus;

public CustomerCommandHandler(
ICustomerRepository customerRepository,
IUnitOfWork uow,
IMediatorHandler bus,
INotificationHandler<DomainNotification> notifications)
: base(uow, bus, notifications)
{
_customerRepository = customerRepository;
_bus = bus;
}
private readonly ICustomerRepository _customerRepository = customerRepository;
private readonly IMediatorHandler _bus = bus;

public Task<bool> Handle(RegisterNewCustomerCommand message, CancellationToken cancellationToken)
{
if (!message.IsValid())
{
NotifyValidationErrors(message);

return Task.FromResult(false);
}

Expand All @@ -45,6 +39,7 @@ public Task<bool> Handle(RegisterNewCustomerCommand message, CancellationToken c
if (_customerRepository.GetByEmail(customer.Email) != null)
{
_bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));

return Task.FromResult(false);
}

Expand All @@ -63,6 +58,7 @@ public Task<bool> Handle(UpdateCustomerCommand message, CancellationToken cancel
if (!message.IsValid())
{
NotifyValidationErrors(message);

return Task.FromResult(false);
}

Expand All @@ -74,6 +70,7 @@ public Task<bool> Handle(UpdateCustomerCommand message, CancellationToken cancel
if (!existingCustomer.Equals(customer))
{
_bus.RaiseEvent(new DomainNotification(message.MessageType, "The customer e-mail has already been taken."));

return Task.FromResult(false);
}
}
Expand All @@ -93,6 +90,7 @@ public Task<bool> Handle(RemoveCustomerCommand message, CancellationToken cancel
if (!message.IsValid())
{
NotifyValidationErrors(message);

return Task.FromResult(false);
}

Expand All @@ -106,8 +104,5 @@ public Task<bool> Handle(RemoveCustomerCommand message, CancellationToken cancel
return Task.FromResult(true);
}

public void Dispose()
{
_customerRepository.Dispose();
}
public void Dispose() => _customerRepository.Dispose();
}
1 change: 1 addition & 0 deletions Src/DDD.Domain/Commands/RegisterNewCustomerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public RegisterNewCustomerCommand(string name, string email, DateTime birthDate)
public override bool IsValid()
{
ValidationResult = new RegisterNewCustomerCommandValidation().Validate(this);

return ValidationResult.IsValid;
}
}
1 change: 1 addition & 0 deletions Src/DDD.Domain/Commands/RemoveCustomerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public RemoveCustomerCommand(Guid id)
public override bool IsValid()
{
ValidationResult = new RemoveCustomerCommandValidation().Validate(this);

return ValidationResult.IsValid;
}
}
1 change: 1 addition & 0 deletions Src/DDD.Domain/Commands/UpdateCustomerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public UpdateCustomerCommand(Guid id, string name, string email, DateTime birthD
public override bool IsValid()
{
ValidationResult = new UpdateCustomerCommandValidation().Validate(this);

return ValidationResult.IsValid;
}
}
Loading