1- using System ;
2- using System . Collections . Generic ;
1+ // Copyright (C) 2019-2020 Xtensive LLC.
2+ // This code is distributed under MIT license terms.
3+ // See the License.txt file in the project root for more information.
4+
5+ using System ;
36using System . Diagnostics . Contracts ;
47using System . Linq ;
58using System . Linq . Expressions ;
6- using System . Reflection ;
7- using Xtensive . Orm ;
9+ using System . Threading ;
10+ using System . Threading . Tasks ;
811
912namespace Xtensive . Orm . BulkOperations
1013{
@@ -19,10 +22,22 @@ public static class BulkExtensions
1922 /// <typeparam name="T">Type of the entity.</typeparam>
2023 /// <param name="query">The query.</param>
2124 /// <returns>Number of the deleted entities.</returns>
22- public static int Delete < T > ( this IQueryable < T > query ) where T : class , IEntity
23- {
24- return new BulkDeleteOperation < T > ( query ) . Execute ( ) ;
25- }
25+ public static int Delete < T > ( this IQueryable < T > query ) where T : class , IEntity =>
26+ new BulkDeleteOperation < T > ( query ) . Execute ( ) ;
27+
28+ /// <summary>
29+ /// Asynchronously executes bulk delete of entities specified by the query.
30+ /// </summary>
31+ /// <remarks>Multiple active operations in the same session instance are not supported. Use
32+ /// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
33+ /// another method in this session.</remarks>
34+ /// <typeparam name="T">Type of the entity.</typeparam>
35+ /// <param name="query">The query.</param>
36+ /// <param name="token">The cancellation token to terminate execution if needed.</param>
37+ /// <returns>Number of the deleted entities.</returns>
38+ public static Task < int > DeleteAsync < T > ( this IQueryable < T > query , CancellationToken token = default )
39+ where T : class , IEntity =>
40+ new BulkDeleteOperation < T > ( query ) . ExecuteAsync ( token ) ;
2641
2742 /// <summary>
2843 /// Executes bulk update of entities specified by the query.
@@ -35,10 +50,8 @@ public static int Delete<T>(this IQueryable<T> query) where T : class, IEntity
3550 /// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
3651 [ Pure ]
3752 public static IUpdatable < T > Set < T , TResult > ( this IQueryable < T > query , Expression < Func < T , TResult > > field ,
38- Expression < Func < T , TResult > > update ) where T : IEntity
39- {
40- return new Updatable < T > ( query , field , update ) ;
41- }
53+ Expression < Func < T , TResult > > update ) where T : IEntity =>
54+ new Updatable < T > ( query , field , update ) ;
4255
4356 /// <summary>
4457 /// Executes bulk update of entities specified by the query.
@@ -51,10 +64,8 @@ public static IUpdatable<T> Set<T, TResult>(this IQueryable<T> query, Expression
5164 /// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
5265 [ Pure ]
5366 public static IUpdatable < T > Set < T , TResult > ( this IUpdatable < T > query , Expression < Func < T , TResult > > field ,
54- Expression < Func < T , TResult > > update ) where T : IEntity
55- {
56- return new Updatable < T > ( ( Updatable < T > ) query , field , update ) ;
57- }
67+ Expression < Func < T , TResult > > update ) where T : IEntity =>
68+ new Updatable < T > ( ( Updatable < T > ) query , field , update ) ;
5869
5970 /// <summary>
6071 /// Executes bulk update of entities specified by the query.
@@ -67,10 +78,8 @@ public static IUpdatable<T> Set<T, TResult>(this IUpdatable<T> query, Expression
6778 /// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
6879 [ Pure ]
6980 public static IUpdatable < T > Set < T , TResult > ( this IQueryable < T > query , Expression < Func < T , TResult > > field ,
70- TResult value ) where T : IEntity
71- {
72- return Set ( query , field , a => value ) ;
73- }
81+ TResult value ) where T : IEntity =>
82+ Set ( query , field , a => value ) ;
7483
7584 /// <summary>
7685 /// Executes bulk update of entities specified by the query.
@@ -83,10 +92,8 @@ public static IUpdatable<T> Set<T, TResult>(this IQueryable<T> query, Expression
8392 /// <returns>Instance of <see cref=" IUpdatable<T>"/>.</returns>
8493 [ Pure ]
8594 public static IUpdatable < T > Set < T , TResult > ( this IUpdatable < T > query , Expression < Func < T , TResult > > field ,
86- TResult value ) where T : IEntity
87- {
88- return Set ( query , field , a => value ) ;
89- }
95+ TResult value ) where T : IEntity =>
96+ Set ( query , field , a => value ) ;
9097
9198 /// <summary>
9299 /// Executes the UPDATE operation.
@@ -95,21 +102,46 @@ public static IUpdatable<T> Set<T, TResult>(this IUpdatable<T> query, Expression
95102 /// <param name="query">The query.</param>
96103 /// <param name="evaluator">The expression, that specify new values. Constructor parameters are ignored.</param>
97104 /// <returns>Number of updated entities.</returns>
98- public static int Update < T > ( this IQueryable < T > query , Expression < Func < T , T > > evaluator ) where T : class , IEntity
99- {
100- return new BulkUpdateOperation < T > ( query , evaluator ) . Execute ( ) ;
101- }
105+ public static int Update < T > ( this IQueryable < T > query , Expression < Func < T , T > > evaluator ) where T : class , IEntity =>
106+ new BulkUpdateOperation < T > ( query , evaluator ) . Execute ( ) ;
107+
108+ /// <summary>
109+ /// Asynchronously executes the UPDATE operation.
110+ /// </summary>
111+ /// <remarks>Multiple active operations in the same session instance are not supported. Use
112+ /// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
113+ /// another method in this session.</remarks>
114+ /// <typeparam name="T">Type of the entity.</typeparam>
115+ /// <param name="query">The query.</param>
116+ /// <param name="evaluator">The expression, that specify new values. Constructor parameters are ignored.</param>
117+ /// <param name="token">The cancellation token to terminate execution if necessary.</param>
118+ /// <returns>Number of updated entities.</returns>
119+ public static Task < int > UpdateAsync < T > ( this IQueryable < T > query , Expression < Func < T , T > > evaluator ,
120+ CancellationToken token = default ) where T : class , IEntity =>
121+ new BulkUpdateOperation < T > ( query , evaluator ) . ExecuteAsync ( token ) ;
102122
103123 /// <summary>
104124 /// Executes the UPDATE operation.
105125 /// </summary>
106126 /// <typeparam name="T">Type of the entity.</typeparam>
107127 /// <param name="query">The query.</param>
108128 /// <returns>Number of updated entities.</returns>
109- public static int Update < T > ( this IUpdatable < T > query ) where T : class , IEntity
110- {
111- return new BulkUpdateOperation < T > ( query ) . Execute ( ) ;
112- }
129+ public static int Update < T > ( this IUpdatable < T > query ) where T : class , IEntity =>
130+ new BulkUpdateOperation < T > ( query ) . Execute ( ) ;
131+
132+ /// <summary>
133+ /// Asynchronously executes the UPDATE operation.
134+ /// </summary>
135+ /// <remarks>Multiple active operations in the same session instance are not supported. Use
136+ /// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
137+ /// another method in this session.</remarks>
138+ /// <typeparam name="T">Type of the entity.</typeparam>
139+ /// <param name="query">The query.</param>
140+ /// <param name="token">The cancellation token to terminate execution if necessary.</param>
141+ /// <returns>Number of updated entities.</returns>
142+ public static Task < int > UpdateAsync < T > ( this IUpdatable < T > query , CancellationToken token = default )
143+ where T : class , IEntity =>
144+ new BulkUpdateOperation < T > ( query ) . ExecuteAsync ( token ) ;
113145
114146 /// <summary>
115147 /// Executes INSERT operation.
@@ -125,9 +157,23 @@ public static Key Insert<T>(this QueryEndpoint queryEndpoint, Expression<Func<T>
125157 return operation . Key ;
126158 }
127159
128- #region Non-public methods
129-
130-
131- #endregion
160+ /// <summary>
161+ /// Asynchronously executes INSERT operation.
162+ /// </summary>
163+ /// <remarks>Multiple active operations in the same session instance are not supported. Use
164+ /// <see langword="await"/> to ensure that all asynchronous operations have completed before calling
165+ /// another method in this session.</remarks>
166+ /// <typeparam name="T">Type of the entity.</typeparam>
167+ /// <param name="queryEndpoint">The query endpoint.</param>
168+ /// <param name="evaluator">The expression, tha specify new values.</param>
169+ /// <param name="token">The cancellation token to terminate execution if necessary.</param>
170+ /// <returns>Key of the created entity.</returns>
171+ public static async Task < Key > InsertAsync < T > ( this QueryEndpoint queryEndpoint , Expression < Func < T > > evaluator ,
172+ CancellationToken token = default ) where T : Entity
173+ {
174+ var operation = new InsertOperation < T > ( queryEndpoint . Provider , evaluator ) ;
175+ await operation . ExecuteAsync ( token ) . ConfigureAwait ( false ) ;
176+ return operation . Key ;
177+ }
132178 }
133179}
0 commit comments