11// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
22
33using CoreEx . Entities ;
4+ using CoreEx . RefData ;
45using CoreEx . Results ;
56
67namespace CoreEx . EntityFrameworkCore
@@ -234,5 +235,195 @@ public static class EfDbExtensions
234235 => efDb . DeleteWithResultAsync < T , TModel > ( new EfDbArgs ( efDb . DbArgs ) , key , cancellationToken ) ;
235236
236237 #endregion
238+
239+ #region With
240+
241+ /// <summary>
242+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
243+ /// </summary>
244+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
245+ /// <param name="with">The value with which to verify.</param>
246+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
247+ public static void With ( this IEfDb efDb , string ? with , Action < string > action ) => efDb . With ( with , ( ) => action ( with ! ) ) ;
248+
249+ /// <summary>
250+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
251+ /// </summary>
252+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
253+ /// <param name="with">The value with which to verify.</param>
254+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
255+ public static void With ( this IEfDb efDb , int ? with , Action < int > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
256+
257+ /// <summary>
258+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
259+ /// </summary>
260+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
261+ /// <param name="with">The value with which to verify.</param>
262+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
263+ public static void With ( this IEfDb efDb , int with , Action < int > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
264+
265+ /// <summary>
266+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
267+ /// </summary>
268+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
269+ /// <param name="with">The value with which to verify.</param>
270+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
271+ public static void With ( this IEfDb efDb , short ? with , Action < short > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
272+
273+ /// <summary>
274+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
275+ /// </summary>
276+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
277+ /// <param name="with">The value with which to verify.</param>
278+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
279+ public static void With ( this IEfDb efDb , short with , Action < short > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
280+
281+ /// <summary>
282+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
283+ /// </summary>
284+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
285+ /// <param name="with">The value with which to verify.</param>
286+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
287+ public static void With ( this IEfDb efDb , long ? with , Action < long > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
288+
289+ /// <summary>
290+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
291+ /// </summary>
292+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
293+ /// <param name="with">The value with which to verify.</param>
294+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
295+ public static void With ( this IEfDb efDb , long with , Action < long > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
296+
297+ /// <summary>
298+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
299+ /// </summary>
300+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
301+ /// <param name="with">The value with which to verify.</param>
302+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
303+ public static void With ( this IEfDb efDb , decimal ? with , Action < decimal > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
304+
305+ /// <summary>
306+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
307+ /// </summary>
308+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
309+ /// <param name="with">The value with which to verify.</param>
310+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
311+ public static void With ( this IEfDb efDb , decimal with , Action < decimal > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
312+
313+ /// <summary>
314+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
315+ /// </summary>
316+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
317+ /// <param name="with">The value with which to verify.</param>
318+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
319+ public static void With ( this IEfDb efDb , float ? with , Action < float > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
320+
321+ /// <summary>
322+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
323+ /// </summary>
324+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
325+ /// <param name="with">The value with which to verify.</param>
326+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
327+ public static void With ( this IEfDb efDb , float with , Action < float > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
328+
329+ /// <summary>
330+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
331+ /// </summary>
332+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
333+ /// <param name="with">The value with which to verify.</param>
334+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
335+ public static void With ( this IEfDb efDb , double ? with , Action < double > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
336+
337+ /// <summary>
338+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
339+ /// </summary>
340+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
341+ /// <param name="with">The value with which to verify.</param>
342+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
343+ public static void With ( this IEfDb efDb , double with , Action < double > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
344+
345+ /// <summary>
346+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
347+ /// </summary>
348+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
349+ /// <param name="with">The value with which to verify.</param>
350+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
351+ public static void With ( this IEfDb efDb , DateTime ? with , Action < DateTime > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
352+
353+ /// <summary>
354+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
355+ /// </summary>
356+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
357+ /// <param name="with">The value with which to verify.</param>
358+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
359+ public static void With ( this IEfDb efDb , DateTime with , Action < DateTime > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
360+
361+ /// <summary>
362+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
363+ /// </summary>
364+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
365+ /// <param name="with">The value with which to verify.</param>
366+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
367+ public static void With ( this IEfDb efDb , TimeSpan ? with , Action < TimeSpan > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
368+
369+ /// <summary>
370+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
371+ /// </summary>
372+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
373+ /// <param name="with">The value with which to verify.</param>
374+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
375+ public static void With ( this IEfDb efDb , TimeSpan with , Action < TimeSpan > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
376+
377+ /// <summary>
378+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
379+ /// </summary>
380+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
381+ /// <param name="with">The value with which to verify.</param>
382+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
383+ public static void With ( this IEfDb efDb , bool ? with , Action < bool > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
384+
385+ /// <summary>
386+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
387+ /// </summary>
388+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
389+ /// <param name="with">The value with which to verify.</param>
390+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
391+ public static void With ( this IEfDb efDb , bool with , Action < bool > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
392+
393+ /// <summary>
394+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
395+ /// </summary>
396+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
397+ /// <param name="with">The value with which to verify.</param>
398+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
399+ public static void With ( this IEfDb efDb , char ? with , Action < char > action ) => efDb . With ( with , ( ) => action ( with ! . Value ) ) ;
400+
401+ /// <summary>
402+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>default</c>.
403+ /// </summary>
404+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
405+ /// <param name="with">The value with which to verify.</param>
406+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
407+ public static void With ( this IEfDb efDb , char with , Action < char > action ) => efDb . With ( with , ( ) => action ( with ) ) ;
408+
409+ /// <summary>
410+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
411+ /// </summary>
412+ /// <typeparam name="TRef">The <see cref="IReferenceData"/> <see cref="Type"/>.</typeparam>
413+ /// <param name="efDb"></param>
414+ /// <param name="with"></param>
415+ /// <param name="action"></param>
416+ public static void With < TRef > ( this IEfDb efDb , TRef ? with , Action < TRef > action ) where TRef : IReferenceData => efDb . With ( with , ( ) => action ( with ! ) ) ;
417+
418+ /// <summary>
419+ /// Invokes the <paramref name="action"/> when the <paramref name="with"/> is not <c>null</c>.
420+ /// </summary>
421+ /// <typeparam name="TRef">The <see cref="IReferenceData"/> <see cref="Type"/>.</typeparam>
422+ /// <param name="efDb">The <see cref="IEfDb"/>.</param>
423+ /// <param name="with">The value with which to verify.</param>
424+ /// <param name="action">The <see cref="Action"/> to invoke when there is a valid <paramref name="with"/> value.</param>
425+ public static void With < TRef > ( this IEfDb efDb , ReferenceDataCodeList < TRef > ? with , Action < ReferenceDataCodeList < TRef > > action ) where TRef : class , IReferenceData , new ( ) => efDb . With ( with , ( ) => action ( with ! ) ) ;
426+
427+ #endregion
237428 }
238429}
0 commit comments