Use file scoped namepace
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
namespace GerstITS.Examples.Logic.Example
|
||||
{
|
||||
public class Example
|
||||
{
|
||||
#region Properties
|
||||
namespace GerstITS.Examples.Logic.Example;
|
||||
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Description { get; set; }
|
||||
public class Example
|
||||
{
|
||||
#region Properties
|
||||
|
||||
public string FirstName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Description { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
namespace GerstITS.Examples.Logic.Example
|
||||
namespace GerstITS.Examples.Logic.Example;
|
||||
|
||||
public interface IExampleProvider
|
||||
{
|
||||
public interface IExampleProvider
|
||||
{
|
||||
Example GetById(int id);
|
||||
Example GetById_v1_1(int id);
|
||||
}
|
||||
}
|
||||
Example GetById(int id);
|
||||
Example GetById_v1_1(int id);
|
||||
}
|
||||
@@ -1,51 +1,50 @@
|
||||
using AutoMapper;
|
||||
using GerstITS.Data;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Example
|
||||
namespace GerstITS.Examples.Logic.Example;
|
||||
|
||||
internal sealed class ExampleProvider : IExampleProvider
|
||||
{
|
||||
internal sealed class ExampleProvider : IExampleProvider
|
||||
#region Fields
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public ExampleProvider(IMapper mapper)
|
||||
{
|
||||
#region Fields
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public ExampleProvider(IMapper mapper)
|
||||
{
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IExampleProvider
|
||||
|
||||
public Example GetById(int id)
|
||||
{
|
||||
ThrowsAnExceptionIfEntityIsNotFound(id);
|
||||
|
||||
return _mapper.Map<Example>(id);
|
||||
}
|
||||
|
||||
public Example GetById_v1_1(int id)
|
||||
{
|
||||
ThrowsAnExceptionIfEntityIsNotFound(id);
|
||||
|
||||
return _mapper.Map<Example>(id);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static void ThrowsAnExceptionIfEntityIsNotFound(int id)
|
||||
{
|
||||
if (id % 2 == 0)
|
||||
throw new EntityNotFoundException($"Example mit der Id '{id}' nicht gefunden.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IExampleProvider
|
||||
|
||||
public Example GetById(int id)
|
||||
{
|
||||
ThrowsAnExceptionIfEntityIsNotFound(id);
|
||||
|
||||
return _mapper.Map<Example>(id);
|
||||
}
|
||||
|
||||
public Example GetById_v1_1(int id)
|
||||
{
|
||||
ThrowsAnExceptionIfEntityIsNotFound(id);
|
||||
|
||||
return _mapper.Map<Example>(id);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static void ThrowsAnExceptionIfEntityIsNotFound(int id)
|
||||
{
|
||||
if (id % 2 == 0)
|
||||
throw new EntityNotFoundException($"Example mit der Id '{id}' nicht gefunden.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,19 +1,18 @@
|
||||
using AutoMapper;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Example
|
||||
namespace GerstITS.Examples.Logic.Example;
|
||||
|
||||
internal sealed class IntegerMapping : Profile
|
||||
{
|
||||
internal sealed class IntegerMapping : Profile
|
||||
#region Construtcors
|
||||
|
||||
public IntegerMapping()
|
||||
{
|
||||
#region Construtcors
|
||||
|
||||
public IntegerMapping()
|
||||
{
|
||||
CreateMap<int, Example>()
|
||||
.ForMember(x => x.FirstName, m => m.MapFrom((s, _) => $"First Name {s}"))
|
||||
.ForMember(x => x.LastName, m => m.MapFrom((s, _) => $"Last Name {s}"))
|
||||
.ForMember(x => x.Description, m => m.MapFrom((s, _) => $"Useful description for id '{s}'"));
|
||||
}
|
||||
|
||||
#endregion
|
||||
CreateMap<int, Example>()
|
||||
.ForMember(x => x.FirstName, m => m.MapFrom((s, _) => $"First Name {s}"))
|
||||
.ForMember(x => x.LastName, m => m.MapFrom((s, _) => $"Last Name {s}"))
|
||||
.ForMember(x => x.Description, m => m.MapFrom((s, _) => $"Useful description for id '{s}'"));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -2,19 +2,18 @@
|
||||
using GerstITS.Mapping.AutoMapper;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace GerstITS.Examples.Logic
|
||||
namespace GerstITS.Examples.Logic;
|
||||
|
||||
public sealed partial class Module
|
||||
{
|
||||
public sealed partial class Module
|
||||
#region Methods
|
||||
|
||||
private static void RegisterExample(IServiceCollection container)
|
||||
{
|
||||
#region Methods
|
||||
container.RegisterMappingsOf<IExampleProvider>();
|
||||
|
||||
private static void RegisterExample(IServiceCollection container)
|
||||
{
|
||||
container.RegisterMappingsOf<IExampleProvider>();
|
||||
container.AddScoped<IExampleProvider, ExampleProvider>();
|
||||
}
|
||||
|
||||
container.AddScoped<IExampleProvider, ExampleProvider>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -4,19 +4,18 @@ using GerstITS.Search;
|
||||
using GerstITS.Validation;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace GerstITS.Examples.Logic
|
||||
namespace GerstITS.Examples.Logic;
|
||||
|
||||
public sealed partial class Module
|
||||
{
|
||||
public sealed partial class Module
|
||||
#region Methods
|
||||
|
||||
private static void RegisterShared(IServiceCollection container)
|
||||
{
|
||||
#region Methods
|
||||
container.RegisterValidationRulesOf<IdValidationRule>();
|
||||
|
||||
private static void RegisterShared(IServiceCollection container)
|
||||
{
|
||||
container.RegisterValidationRulesOf<IdValidationRule>();
|
||||
container.AddScoped<ISortingProvider, SortingProvider>();
|
||||
}
|
||||
|
||||
container.AddScoped<ISortingProvider, SortingProvider>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
using GerstITS.IoC;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace GerstITS.Examples.Logic
|
||||
{
|
||||
public sealed partial class Module : IIoCModule<IServiceCollection>
|
||||
{
|
||||
#region IIoCModule
|
||||
namespace GerstITS.Examples.Logic;
|
||||
|
||||
public void RegisterComponents(IServiceCollection container)
|
||||
{
|
||||
RegisterExample(container);
|
||||
RegisterShared(container);
|
||||
}
|
||||
public sealed partial class Module : IIoCModule<IServiceCollection>
|
||||
{
|
||||
#region IIoCModule
|
||||
|
||||
public void RegisterComponents(IServiceCollection container)
|
||||
{
|
||||
RegisterExample(container);
|
||||
RegisterShared(container);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -4,28 +4,27 @@ using System.Linq.Expressions;
|
||||
using GerstITS.Common;
|
||||
using GerstITS.Data;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Shared
|
||||
namespace GerstITS.Examples.Logic.Shared;
|
||||
|
||||
internal static class ISearchEngineExtensions
|
||||
{
|
||||
internal static class ISearchEngineExtensions
|
||||
#region Methods
|
||||
|
||||
public static TEntity QueryBy<TEntity>(this IReadOnlyRepository repository, Expression<Func<TEntity, bool>> condition)
|
||||
where TEntity : IEntity
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public static TEntity QueryBy<TEntity>(this IReadOnlyRepository repository, Expression<Func<TEntity, bool>> condition)
|
||||
where TEntity : IEntity
|
||||
{
|
||||
return repository.Query<TEntity>()
|
||||
.Where(condition)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public static TModel ThrowsAnExceptionIfNotFound<TModel, TKey>(this TModel model, TKey id)
|
||||
{
|
||||
if (model.IsNull())
|
||||
throw new EntityNotFoundException($"{typeof(TModel).Name} with id '{id}' not found.");
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
#endregion
|
||||
return repository.Query<TEntity>()
|
||||
.Where(condition)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public static TModel ThrowsAnExceptionIfNotFound<TModel, TKey>(this TModel model, TKey id)
|
||||
{
|
||||
if (model.IsNull())
|
||||
throw new EntityNotFoundException($"{typeof(TModel).Name} with id '{id}' not found.");
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
namespace GerstITS.Examples.Logic.Shared.Search.Sorting
|
||||
namespace GerstITS.Examples.Logic.Shared.Search.Sorting;
|
||||
|
||||
internal interface ISortable
|
||||
{
|
||||
internal interface ISortable
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,46 +5,45 @@ using System.Reflection;
|
||||
using GerstITS.Common;
|
||||
using GerstITS.Search;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Shared.Search.Sorting
|
||||
namespace GerstITS.Examples.Logic.Shared.Search.Sorting;
|
||||
|
||||
internal class SortingProvider : ISortingProvider
|
||||
{
|
||||
internal class SortingProvider : ISortingProvider
|
||||
#region ISortingProvider
|
||||
|
||||
public bool CanSort<TItem>(IQueryable<TItem> query)
|
||||
{
|
||||
#region ISortingProvider
|
||||
|
||||
public bool CanSort<TItem>(IQueryable<TItem> query)
|
||||
{
|
||||
return typeof(TItem).IsAssignableTo(typeof(ISortable));
|
||||
}
|
||||
|
||||
public IQueryable<TItem> Sort<TItem>(IQueryable<TItem> query, ISorting sorting)
|
||||
{
|
||||
var propertyInfo = typeof(TItem).GetProperties()
|
||||
.SingleOrDefault(i => string.Equals(i.Name, sorting.SortBy, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
if (!CanSort(query) || propertyInfo.IsNull())
|
||||
return query;
|
||||
|
||||
var sortExpression = CreateSortExpression<TItem>(propertyInfo);
|
||||
|
||||
return (sorting.SortDirection == SortingDirections.Ascending
|
||||
? query.OrderBy(sortExpression)
|
||||
: query.OrderByDescending(sortExpression))
|
||||
.AsQueryable();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static Expression<Func<TItem, object>> CreateSortExpression<TItem>(MemberInfo propertyInfo)
|
||||
{
|
||||
var parameterExpression = Expression.Parameter(typeof(TItem), "x");
|
||||
var propertyExpression = Expression.Property(parameterExpression, propertyInfo.Name);
|
||||
var convertExpression = Expression.Convert(propertyExpression, typeof(object));
|
||||
|
||||
return Expression.Lambda<Func<TItem, object>>(convertExpression, parameterExpression);
|
||||
}
|
||||
|
||||
#endregion
|
||||
return typeof(TItem).IsAssignableTo(typeof(ISortable));
|
||||
}
|
||||
}
|
||||
|
||||
public IQueryable<TItem> Sort<TItem>(IQueryable<TItem> query, ISorting sorting)
|
||||
{
|
||||
var propertyInfo = typeof(TItem).GetProperties()
|
||||
.SingleOrDefault(i => string.Equals(i.Name, sorting.SortBy, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
if (!CanSort(query) || propertyInfo.IsNull())
|
||||
return query;
|
||||
|
||||
var sortExpression = CreateSortExpression<TItem>(propertyInfo);
|
||||
|
||||
return (sorting.SortDirection == SortingDirections.Ascending
|
||||
? query.OrderBy(sortExpression)
|
||||
: query.OrderByDescending(sortExpression))
|
||||
.AsQueryable();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
private static Expression<Func<TItem, object>> CreateSortExpression<TItem>(MemberInfo propertyInfo)
|
||||
{
|
||||
var parameterExpression = Expression.Parameter(typeof(TItem), "x");
|
||||
var propertyExpression = Expression.Property(parameterExpression, propertyInfo.Name);
|
||||
var convertExpression = Expression.Convert(propertyExpression, typeof(object));
|
||||
|
||||
return Expression.Lambda<Func<TItem, object>>(convertExpression, parameterExpression);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,19 +1,18 @@
|
||||
using FluentValidation;
|
||||
using GerstITS.Validation;
|
||||
|
||||
namespace GerstITS.Examples.Logic.Shared.Validation
|
||||
{
|
||||
internal sealed class IdValidationRule : ValidationRuleBase<int>
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public IdValidationRule()
|
||||
{
|
||||
RuleFor(x => x)
|
||||
.GreaterThan(0)
|
||||
.OverridePropertyName("id");
|
||||
}
|
||||
namespace GerstITS.Examples.Logic.Shared.Validation;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
internal sealed class IdValidationRule : ValidationRuleBase<int>
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public IdValidationRule()
|
||||
{
|
||||
RuleFor(x => x)
|
||||
.GreaterThan(0)
|
||||
.OverridePropertyName("id");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user