Commit 085df9d9 authored by 阿星Plus's avatar 阿星Plus

Services Dto

parent 9d3188ea
using Plus.Service; using Plus.Services;
using System; using System;
namespace Plus.Aspects namespace Plus.Aspects
......
using Plus.Services.Dto;
using System;
using System.Linq;
using System.Linq.Expressions;
namespace Plus.Linq.Extensions
{
/// <summary>
/// Some useful extension methods for <see cref="IQueryable{T}"/>.
/// </summary>
public static class QueryableExtensions
{
/// <summary>
/// Used for paging. Can be used as an alternative to Skip(...).Take(...) chaining.
/// </summary>
public static IQueryable<T> PageBy<T>(this IQueryable<T> query, int skipCount, int maxResultCount)
{
if (query == null)
{
throw new ArgumentNullException("query");
}
return query.Skip(skipCount).Take(maxResultCount);
}
/// <summary>
/// Used for paging with an <see cref="IPagedResultRequest"/> object.
/// </summary>
/// <param name="query">Queryable to apply paging</param>
/// <param name="pagedResultRequest">An object implements <see cref="IPagedResultRequest"/> interface</param>
public static IQueryable<T> PageBy<T>(this IQueryable<T> query, IPagedResultRequest pagedResultRequest)
{
return query.PageBy(pagedResultRequest.SkipCount, pagedResultRequest.MaxResultCount);
}
/// <summary>
/// Filters a <see cref="IQueryable{T}"/> by given predicate if given condition is true.
/// </summary>
/// <param name="query">Queryable to apply filtering</param>
/// <param name="condition">A boolean value</param>
/// <param name="predicate">Predicate to filter the query</param>
/// <returns>Filtered or not filtered query based on <paramref name="condition"/></returns>
public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
{
return condition
? query.Where(predicate)
: query;
}
/// <summary>
/// Filters a <see cref="IQueryable{T}"/> by given predicate if given condition is true.
/// </summary>
/// <param name="query">Queryable to apply filtering</param>
/// <param name="condition">A boolean value</param>
/// <param name="predicate">Predicate to filter the query</param>
/// <returns>Filtered or not filtered query based on <paramref name="condition"/></returns>
public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, int, bool>> predicate)
{
return condition
? query.Where(predicate)
: query;
}
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Plus.Linq
{
/// <summary>
/// IAsyncQueryableExecuter
/// </summary>
public interface IAsyncQueryableExecuter
{
Task<int> CountAsync<T>(IQueryable<T> queryable);
Task<List<T>> ToListAsync<T>(IQueryable<T> queryable);
Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable);
}
}
\ No newline at end of file
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Plus.Linq
{
/// <summary>
/// NullAsyncQueryableExecuter
/// </summary>
public class NullAsyncQueryableExecuter : IAsyncQueryableExecuter
{
public static NullAsyncQueryableExecuter Instance { get; } = new NullAsyncQueryableExecuter();
public Task<int> CountAsync<T>(IQueryable<T> queryable)
{
return Task.FromResult(queryable.Count());
}
public Task<List<T>> ToListAsync<T>(IQueryable<T> queryable)
{
return Task.FromResult(queryable.ToList());
}
public Task<T> FirstOrDefaultAsync<T>(IQueryable<T> queryable)
{
return Task.FromResult(queryable.FirstOrDefault());
}
}
}
\ No newline at end of file
using System;
namespace Plus
{
/// <summary>
/// Can be used to store Name/Value (or Key/Value) pairs.
/// </summary>
[Serializable]
public class NameValue : NameValue<string>
{
/// <summary>
/// Creates a new <see cref="NameValue"/>.
/// </summary>
public NameValue()
{
}
/// <summary>
/// Creates a new <see cref="NameValue"/>.
/// </summary>
public NameValue(string name, string value)
{
Name = name;
Value = value;
}
}
/// <summary>
/// Can be used to store Name/Value (or Key/Value) pairs.
/// </summary>
[Serializable]
public class NameValue<T>
{
/// <summary>
/// Name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Value.
/// </summary>
public T Value { get; set; }
/// <summary>
/// Creates a new <see cref="NameValue"/>.
/// </summary>
public NameValue()
{
}
/// <summary>
/// Creates a new <see cref="NameValue"/>.
/// </summary>
public NameValue(string name, T value)
{
Name = name;
Value = value;
}
}
}
\ No newline at end of file
using Castle.Core; using Castle.Core;
using Castle.MicroKernel; using Castle.MicroKernel;
using Plus.Dependency; using Plus.Dependency;
using Plus.Service; using Plus.Services;
using System.Reflection; using System.Reflection;
namespace Plus.Runtime.Validation.Interception namespace Plus.Runtime.Validation.Interception
......
using System;
namespace Plus.Services.Dto
{
/// <summary>
/// A shortcut of <see cref="EntityDto{TPrimaryKey}"/> for most used primary key type (<see cref="int"/>).
/// </summary>
[Serializable]
public class EntityDto : EntityDto<int>, IEntityDto
{
/// <summary>
/// Creates a new <see cref="EntityDto"/> object.
/// </summary>
public EntityDto()
{
}
/// <summary>
/// Creates a new <see cref="EntityDto"/> object.
/// </summary>
/// <param name="id">Id of the entity</param>
public EntityDto(int id)
: base(id)
{
}
}
/// <summary>
/// Implements common properties for entity based DTOs.
/// </summary>
/// <typeparam name="TPrimaryKey">Type of the primary key</typeparam>
[Serializable]
public class EntityDto<TPrimaryKey> : IEntityDto<TPrimaryKey>
{
/// <summary>
/// Id of the entity.
/// </summary>
public TPrimaryKey Id { get; set; }
/// <summary>
/// Creates a new <see cref="EntityDto{TPrimaryKey}"/> object.
/// </summary>
public EntityDto()
{
}
/// <summary>
/// Creates a new <see cref="EntityDto{TPrimaryKey}"/> object.
/// </summary>
/// <param name="id">Id of the entity</param>
public EntityDto(TPrimaryKey id)
{
Id = id;
}
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// A shortcut of <see cref="IEntityDto{TPrimaryKey}"/> for most used primary key type (<see cref="int"/>).
/// </summary>
public interface IEntityDto : IEntityDto<int>
{
}
/// <summary>
/// Defines common properties for entity based DTOs.
/// </summary>
/// <typeparam name="TPrimaryKey"></typeparam>
public interface IEntityDto<TPrimaryKey>
{
/// <summary>
/// Id of the entity.
/// </summary>
TPrimaryKey Id { get; set; }
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to set "Total Count of Items" to a DTO for long type.
/// </summary>
public interface IHasLongTotalCount
{
/// <summary>
/// Total count of Items.
/// </summary>
long TotalCount { get; set; }
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to set "Total Count of Items" to a DTO.
/// </summary>
public interface IHasTotalCount
{
/// <summary>
/// Total count of Items.
/// </summary>
int TotalCount { get; set; }
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to request a limited result.
/// </summary>
public interface ILimitedResultRequest
{
/// <summary>
/// Max expected result count.
/// </summary>
int MaxResultCount { get; set; }
}
}
\ No newline at end of file
using System.Collections.Generic;
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to return a list of items to clients.
/// </summary>
/// <typeparam name="T">Type of the items in the <see cref="Items"/> list</typeparam>
public interface IListResult<T>
{
/// <summary>
/// List of items.
/// </summary>
IReadOnlyList<T> Items { get; set; }
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to request a paged and sorted result.
/// </summary>
public interface IPagedAndSortedResultRequest : IPagedResultRequest, ISortedResultRequest
{
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to return a page of items to clients.
/// </summary>
/// <typeparam name="T">Type of the items in the <see cref="IListResult{T}.Items"/> list</typeparam>
public interface IPagedResult<T> : IListResult<T>, IHasTotalCount
{
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to request a paged result.
/// </summary>
public interface IPagedResultRequest : ILimitedResultRequest
{
/// <summary>
/// Skip count (beginning of the page).
/// </summary>
int SkipCount { get; set; }
}
}
\ No newline at end of file
namespace Plus.Services.Dto
{
/// <summary>
/// This interface is defined to standardize to request a sorted result.
/// </summary>
public interface ISortedResultRequest
{
/// <summary>
/// Sorting information.
/// Should include sorting field and optionally a direction (ASC or DESC)
/// Can contain more than one field separated by comma (,).
/// </summary>
/// <example>
/// Examples:
/// "Name"
/// "Name DESC"
/// "Name ASC, Age DESC"
/// </example>
string Sorting { get; set; }
}
}
\ No newline at end of file
using System.ComponentModel.DataAnnotations;
namespace Plus.Services.Dto
{
/// <summary>
/// Simply implements <see cref="ILimitedResultRequest"/>.
/// </summary>
public class LimitedResultRequestDto : ILimitedResultRequest
{
[Range(1, int.MaxValue)]
public virtual int MaxResultCount { get; set; } = 10;
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
namespace Plus.Services.Dto
{
/// <summary>
/// Implements <see cref="IListResult{T}"/>.
/// </summary>
/// <typeparam name="T">Type of the items in the <see cref="Items"/> list</typeparam>
[Serializable]
public class ListResultDto<T> : IListResult<T>
{
/// <summary>
/// List of items.
/// </summary>
public IReadOnlyList<T> Items
{
get { return _items ?? (_items = new List<T>()); }
set { _items = value; }
}
private IReadOnlyList<T> _items;
/// <summary>
/// Creates a new <see cref="ListResultDto{T}"/> object.
/// </summary>
public ListResultDto()
{
}
/// <summary>
/// Creates a new <see cref="ListResultDto{T}"/> object.
/// </summary>
/// <param name="items">List of items</param>
public ListResultDto(IReadOnlyList<T> items)
{
Items = items;
}
}
}
\ No newline at end of file
using System;
namespace Plus.Services.Dto
{
/// <summary>
/// Can be used to send/receive Name/Value (or Key/Value) pairs.
/// </summary>
[Serializable]
public class NameValueDto : NameValueDto<string>
{
/// <summary>
/// Creates a new <see cref="NameValueDto"/>.
/// </summary>
public NameValueDto()
{
}
/// <summary>
/// Creates a new <see cref="NameValueDto"/>.
/// </summary>
public NameValueDto(string name, string value)
: base(name, value)
{
}
/// <summary>
/// Creates a new <see cref="NameValueDto"/>.
/// </summary>
/// <param name="nameValue">A <see cref="NameValue"/> object to get it's name and value</param>
public NameValueDto(NameValue nameValue)
: this(nameValue.Name, nameValue.Value)
{
}
}
/// <summary>
/// Can be used to send/receive Name/Value (or Key/Value) pairs.
/// </summary>
[Serializable]
public class NameValueDto<T> : NameValue<T>
{
/// <summary>
/// Creates a new <see cref="NameValueDto"/>.
/// </summary>
public NameValueDto()
{
}
/// <summary>
/// Creates a new <see cref="NameValueDto"/>.
/// </summary>
public NameValueDto(string name, T value)
: base(name, value)
{
}
/// <summary>
/// Creates a new <see cref="NameValueDto"/>.
/// </summary>
/// <param name="nameValue">A <see cref="NameValue"/> object to get it's name and value</param>
public NameValueDto(NameValue<T> nameValue)
: this(nameValue.Name, nameValue.Value)
{
}
}
}
\ No newline at end of file
using System;
namespace Plus.Services.Dto
{
/// <summary>
/// This DTO can be directly used (or inherited)
/// to pass an nullable Id value to an application service method.
/// </summary>
/// <typeparam name="TId">Type of the Id</typeparam>
[Serializable]
public class NullableIdDto<TId>
where TId : struct
{
public TId? Id { get; set; }
public NullableIdDto()
{
}
public NullableIdDto(TId? id)
{
Id = id;
}
}
/// <summary>
/// A shortcut of <see cref="NullableIdDto{TId}"/> for <see cref="int"/>.
/// </summary>
[Serializable]
public class NullableIdDto : NullableIdDto<int>
{
public NullableIdDto()
{
}
public NullableIdDto(int? id)
: base(id)
{
}
}
}
\ No newline at end of file
using System;
namespace Plus.Services.Dto
{
/// <summary>
/// Simply implements <see cref="IPagedAndSortedResultRequest"/>.
/// </summary>
[Serializable]
public class PagedAndSortedResultRequestDto : PagedResultRequestDto, IPagedAndSortedResultRequest
{
public virtual string Sorting { get; set; }
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
namespace Plus.Services.Dto
{
/// <summary>
/// Implements <see cref="IPagedResult{T}"/>.
/// </summary>
/// <typeparam name="T">Type of the items in the <see cref="ListResultDto{T}.Items"/> list</typeparam>
[Serializable]
public class PagedResultDto<T> : ListResultDto<T>, IPagedResult<T>
{
/// <summary>
/// Total count of Items.
/// </summary>
public int TotalCount { get; set; }
/// <summary>
/// Creates a new <see cref="PagedResultDto{T}"/> object.
/// </summary>
public PagedResultDto()
{
}
/// <summary>
/// Creates a new <see cref="PagedResultDto{T}"/> object.
/// </summary>
/// <param name="totalCount">Total count of Items</param>
/// <param name="items">List of items in current page</param>
public PagedResultDto(int totalCount, IReadOnlyList<T> items)
: base(items)
{
TotalCount = totalCount;
}
}
}
\ No newline at end of file
using System;
using System.ComponentModel.DataAnnotations;
namespace Plus.Services.Dto
{
/// <summary>
/// Simply implements <see cref="IPagedResultRequest"/>.
/// </summary>
[Serializable]
public class PagedResultRequestDto : LimitedResultRequestDto, IPagedResultRequest
{
[Range(0, int.MaxValue)]
public virtual int SkipCount { get; set; }
}
}
\ No newline at end of file
using Plus.Dependency; using Plus.Dependency;
namespace Plus.Service namespace Plus.Services
{ {
/// <summary> /// <summary>
/// IApplicationService /// IApplicationService
......
using System.Collections.Generic; using System.Collections.Generic;
namespace Plus.Service namespace Plus.Services
{ {
/// <summary> /// <summary>
/// IAvoidDuplicateCrossCuttingConcerns /// IAvoidDuplicateCrossCuttingConcerns
......
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