Browse Source

Added repository interfaces and asynhelper.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent
commit
50f2eae288
  1. 77
      src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs
  2. 129
      src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs
  3. 7
      src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepositoryMarker.cs
  4. 43
      src/Volo.Abp/Volo/Abp/Threading/AsyncHelper.cs
  5. 3
      src/Volo.Abp/project.json
  6. 32
      test/Volo.Abp.Tests/Volo/Abp/Threading/AsyncHelper_Tests.cs

77
src/Volo.Abp/Volo/Abp/Domain/Repositories/IQueryableRepository.cs

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Domain.Repositories
{
public interface IQueryableRepository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
/// <summary>
/// Used to get a IQueryable that is used to retrieve entities from entire table.
/// </summary>
/// <returns>IQueryable to be used to select entities from database</returns>
IQueryable<TEntity> GetAll();
/// <summary>
/// Used to get all entities based on given <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A condition to filter entities</param>
/// <returns>List of all entities</returns>
List<TEntity> GetAllList(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Used to get all entities based on given <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A condition to filter entities</param>
/// <returns>List of all entities</returns>
Task<List<TEntity>> GetAllListAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Gets an entity with given given predicate or null if not found.
/// </summary>
/// <param name="predicate">Predicate to filter entities</param>
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Gets an entity with given given predicate or null if not found.
/// </summary>
/// <param name="predicate">Predicate to filter entities</param>
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Deletes many entities by function.
/// Notice that: All entities fits to given predicate are retrieved and deleted.
/// This may cause major performance problems if there are too many entities with
/// given predicate.
/// </summary>
/// <param name="predicate">A condition to filter entities</param>
void Delete(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Deletes many entities by function.
/// Notice that: All entities fits to given predicate are retrieved and deleted.
/// This may cause major performance problems if there are too many entities with
/// given predicate.
/// </summary>
/// <param name="predicate">A condition to filter entities</param>
Task DeleteAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Gets count of all entities in this repository based on given <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A method to filter count</param>
/// <returns>Count of entities</returns>
int Count(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Gets count of all entities in this repository based on given <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">A method to filter count</param>
/// <returns>Count of entities</returns>
Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate);
}
}

129
src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepository.cs

@ -0,0 +1,129 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
namespace Volo.Abp.Domain.Repositories
{
//TODO: Didn't get all members from ABP 1.x
public interface IRepository<TEntity, TPrimaryKey> : IRepositoryMarker where TEntity : class, IEntity<TPrimaryKey>
{
/// <summary>
/// Used to get all entities.
/// </summary>
/// <returns>List of all entities</returns>
List<TEntity> GetAllList();
/// <summary>
/// Used to get all entities.
/// </summary>
/// <returns>List of all entities</returns>
Task<List<TEntity>> GetAllListAsync();
/// <summary>
/// Gets an entity with given primary key.
/// </summary>
/// <param name="id">Primary key of the entity to get</param>
/// <returns>Entity</returns>
TEntity Get(TPrimaryKey id);
/// <summary>
/// Gets an entity with given primary key.
/// </summary>
/// <param name="id">Primary key of the entity to get</param>
/// <returns>Entity</returns>
Task<TEntity> GetAsync(TPrimaryKey id);
/// <summary>
/// Gets an entity with given primary key or null if not found.
/// </summary>
/// <param name="id">Primary key of the entity to get</param>
/// <returns>Entity or null</returns>
TEntity FirstOrDefault(TPrimaryKey id);
/// <summary>
/// Gets an entity with given primary key or null if not found.
/// </summary>
/// <param name="id">Primary key of the entity to get</param>
/// <returns>Entity or null</returns>
Task<TEntity> FirstOrDefaultAsync(TPrimaryKey id);
/// <summary>
/// Inserts a new entity.
/// </summary>
/// <param name="entity">Inserted entity</param>
TEntity Insert(TEntity entity);
/// <summary>
/// Inserts a new entity.
/// </summary>
/// <param name="entity">Inserted entity</param>
Task<TEntity> InsertAsync(TEntity entity);
/// <summary>
/// Inserts a new entity and gets it's Id.
/// It may require to save current unit of work
/// to be able to retrieve id.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns>Id of the entity</returns>
TPrimaryKey InsertAndGetId(TEntity entity);
/// <summary>
/// Inserts a new entity and gets it's Id.
/// It may require to save current unit of work
/// to be able to retrieve id.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns>Id of the entity</returns>
Task<TPrimaryKey> InsertAndGetIdAsync(TEntity entity);
/// <summary>
/// Updates an existing entity.
/// </summary>
/// <param name="entity">Entity</param>
TEntity Update(TEntity entity);
/// <summary>
/// Updates an existing entity.
/// </summary>
/// <param name="entity">Entity</param>
Task<TEntity> UpdateAsync(TEntity entity);
/// <summary>
/// Deletes an entity.
/// </summary>
/// <param name="entity">Entity to be deleted</param>
void Delete(TEntity entity);
/// <summary>
/// Deletes an entity.
/// </summary>
/// <param name="entity">Entity to be deleted</param>
Task DeleteAsync(TEntity entity);
/// <summary>
/// Deletes an entity by primary key.
/// </summary>
/// <param name="id">Primary key of the entity</param>
void Delete(TPrimaryKey id);
/// <summary>
/// Deletes an entity by primary key.
/// </summary>
/// <param name="id">Primary key of the entity</param>
Task DeleteAsync(TPrimaryKey id);
/// <summary>
/// Gets count of all entities in this repository.
/// </summary>
/// <returns>Count of entities</returns>
int Count();
/// <summary>
/// Gets count of all entities in this repository.
/// </summary>
/// <returns>Count of entities</returns>
Task<int> CountAsync();
}
}

7
src/Volo.Abp/Volo/Abp/Domain/Repositories/IRepositoryMarker.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.Domain.Repositories
{
public interface IRepositoryMarker
{
}
}

43
src/Volo.Abp/Volo/Abp/Threading/AsyncHelper.cs

@ -0,0 +1,43 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using Nito.AsyncEx;
namespace Volo.Abp.Threading
{
/// <summary>
/// Provides some helper methods to work with async methods.
/// </summary>
public static class AsyncHelper
{
/// <summary>
/// Checks if given method is an async method.
/// </summary>
/// <param name="method">A method to check</param>
public static bool IsAsyncMethod(MethodInfo method)
{
return method.ReturnType == typeof(Task) ||
(method.ReturnType.GetTypeInfo().IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>));
}
/// <summary>
/// Runs a async method synchronously.
/// </summary>
/// <param name="func">A function that returns a result</param>
/// <typeparam name="TResult">Result type</typeparam>
/// <returns>Result of the async operation</returns>
public static TResult RunSync<TResult>(Func<Task<TResult>> func)
{
return AsyncContext.Run(func);
}
/// <summary>
/// Runs a async method synchronously.
/// </summary>
/// <param name="action">An async action</param>
public static void RunSync(Func<Task> action)
{
AsyncContext.Run(action);
}
}
}

3
src/Volo.Abp/project.json

@ -6,7 +6,8 @@
"Volo.DependencyInjection": "1.0.0-*", "Volo.DependencyInjection": "1.0.0-*",
"System.Collections.Immutable": "1.3.0", "System.Collections.Immutable": "1.3.0",
"Volo.ExtensionMethods": "1.0.0-*", "Volo.ExtensionMethods": "1.0.0-*",
"Newtonsoft.Json": "9.0.1" "Newtonsoft.Json": "9.0.1",
"Nito.AsyncEx.Context": "1.1.0"
}, },
"frameworks": { "frameworks": {

32
test/Volo.Abp.Tests/Volo/Abp/Threading/AsyncHelper_Tests.cs

@ -0,0 +1,32 @@
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.Abp.Threading
{
public class AsyncHelper_Tests
{
[Fact]
public void RunSync_Test_Without_Return_Value()
{
AsyncHelper.RunSync(MyTaskWithoutReturnValue);
}
[Fact]
public void RunSync_Test_With_Return_Value()
{
AsyncHelper.RunSync(() => MyTaskWithReturnValue(42)).ShouldBe(42);
}
private static async Task MyTaskWithoutReturnValue()
{
await Task.Delay(1);
}
private static async Task<int> MyTaskWithReturnValue(int aNumber)
{
await Task.Delay(1);
return aNumber;
}
}
}
Loading…
Cancel
Save