Commit be66bb06 authored by hasan khaddour's avatar hasan khaddour

Initiate Shared kernel Structure , Add Abstract Base Classes.

parent 84d14974
using PSManagement.SharedKernel.Entities;
namespace PSManagement.SharedKernel.Aggregate
{
// Apply this marker interface only to aggregate root entities
// Repositories will only work with aggregate roots, not their children
public class IAggregateRoot : BaseEntity
{
}
}
using MediatR;
using System;
namespace PSManagement.SharedKernel.Events
{
public abstract class BaseDomainEvent : INotification
{
public DateTime DateOccurred { get; protected set; } = DateTime.UtcNow;
}
}
using System;
namespace PSManagement.SharedKernel.DomainException
{
public class DomainException : Exception
{
public DomainException(string message) : base(message)
{
}
}
}
using PSManagement.SharedKernel.Events;
using System.Collections.Generic;
namespace PSManagement.SharedKernel.Entities
{
public class BaseEntity
{
public int Id { get; set; }
public List<BaseDomainEvent> Events = new List<BaseDomainEvent>();
public static bool operator ==(BaseEntity first, BaseEntity second)
{
if (first is null && second is null)
{
return true;
}
if (first is null || second is null)
{
return false;
}
return first.Equals(second);
}
public static bool operator !=(BaseEntity first, BaseEntity second)
{
return !(first == second);
}
public bool Equals(BaseEntity other)
{
if (other is null || other.GetType() != GetType())
{
return false;
}
return other.Id == Id;
}
public override bool Equals(object obj)
{
// Check if the two have same type.
if (obj is null || obj.GetType() != GetType())
{
return false;
}
// Check If the obj if of type Entity.
if (obj is not BaseEntity entity)
{
return false;
}
return entity.Id == Id;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace PSManagement.SharedKernel.Interfaces
{
public interface ISpecification<T>
{
Expression<Func<T, bool>> Criteria { get; }
List<Expression<Func<T, object>>> Includes { get; }
List<string> IncludeStrings { get; }
Expression<Func<T, object>> OrderBy { get; }
Expression<Func<T, object>> OrderByDescending { get; }
int Take { get; }
int Skip { get; }
bool isPagingEnabled { get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSManagement.SharedKernel.Interfaces
{
public interface IUnitOfWork
{
}
}
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Events\" /> <PackageReference Include="MediatR" Version="5.1.0" />
<Folder Include="Abstraction\" />
<Folder Include="Interfaces\" />
<Folder Include="Exception\" />
</ItemGroup> </ItemGroup>
</Project> </Project>
using PSManagement.SharedKernel.Entities;
using PSManagement.SharedKernel.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PSManagement.SharedKernel.Repositories
{
public interface IReadRepository<T> where T : BaseEntity
{
Task<T> GetByIdAsync(int id);
Task<List<T>> ListAsync();
Task<List<T>> ListAsync(ISpecification<T> spec);
}
}
using PSManagement.SharedKernel.Entities;
using PSManagement.SharedKernel.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PSManagement.SharedKernel.Repositories
{
public interface IRepository<T> where T :BaseEntity
{
Task<T> GetByIdAsync(int id);
Task<List<T>> ListAsync();
Task<List<T>> ListAsync(ISpecification<T> spec);
Task<T> AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(T entity);
}
}
using PSManagement.SharedKernel.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
// Specification Pattern from : https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/infrastructure-persistence-layer-implemenation-entity-framework-core
namespace PSManagement.SharedKernel.Specification
{
public class BaseSpecification<T> : ISpecification<T>
{
protected BaseSpecification(Expression<Func<T, bool>> criteria)
{
Criteria = criteria;
}
public Expression<Func<T, bool>> Criteria { get; }
public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();
public List<string> IncludeStrings { get; } = new List<string>();
public Expression<Func<T, object>> OrderBy { get; private set; }
public Expression<Func<T, object>> OrderByDescending { get; private set; }
public int Take { get; private set; }
public int Skip { get; private set; }
public bool isPagingEnabled { get; private set; } = false;
protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
{
Includes.Add(includeExpression);
}
protected virtual void AddInclude(string includeString)
{
IncludeStrings.Add(includeString);
}
protected virtual void ApplyPaging(int skip, int take)
{
Skip = skip;
Take = take;
isPagingEnabled = true;
}
protected virtual void ApplyOrderBy(Expression<Func<T, object>> orderByExpression)
{
OrderBy = orderByExpression;
}
protected virtual void ApplyOrderByDescending(Expression<Func<T, object>> orderByDescendingExpression)
{
OrderByDescending = orderByDescendingExpression;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace PSManagement.SharedKernel.ValueObjects
{
public abstract class ValueObject : IEquatable<ValueObject>
{
public static bool operator ==(ValueObject one, ValueObject two)
{
return EqualOperator(one, two);
}
public static bool operator !=(ValueObject one, ValueObject two)
{
return !EqualOperator(one, two);
}
public bool Equals(ValueObject other)
{
return other is not null && ValuesAreEqual(other);
}
public override bool Equals(object obj)
{
if (obj is null || obj.GetType() != GetType())
{
return false;
}
return obj is ValueObject other && ValuesAreEqual(other);
}
public override int GetHashCode()
{
return GetEqualityComponents()
.Select(x => x != null ? x.GetHashCode() : 0)
.Aggregate((x, y) => x ^ y);
}
protected static bool EqualOperator(ValueObject left, ValueObject right)
{
if (left is null && right is null)
{
return true;
}
if (left is null || right is null)
{
return false;
}
return ReferenceEquals(left, right) || left.Equals(right);
}
protected abstract IEnumerable<object> GetEqualityComponents();
private bool ValuesAreEqual(ValueObject other)
{
return other is not null && GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment