/// <summary> /// Asynchronously loads multiple documents from Raven and returns them as optional values. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="ids"></param> /// <returns></returns> public static async Task <IEnumerable <T?> > LoadOptionalAsync <T>(this IAsyncLoaderWithInclude <T> session, IEnumerable <string> ids) where T : class { var result = await session.LoadAsync <T>(ids); return(result.Values); }
/// <summary> /// Asynchronously loads a document from Raven and stores it in an Option. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="id"></param> /// <returns></returns> public static async Task <T?> LoadOptionalAsync <T>(this IAsyncLoaderWithInclude <T> session, string id) where T : class { if (string.IsNullOrWhiteSpace(id)) { return(null); } return(await session.LoadAsync <T>(id)); }
/// <summary> /// Loads a document from the session and throws if it's null. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="id"></param> /// <returns></returns> public static async Task <T> LoadRequiredAsync <T>(this IAsyncLoaderWithInclude <T> session, string id) { var result = await session.LoadAsync <T>(id); if (result == null) { throw new ArgumentException($"Tried to load {id} but was null."); } return(result); }
/// <summary> /// Loads a document from the session and throws the specified exception if null. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="id"></param> /// <returns></returns> public static async Task <T> LoadRequiredAsync <T, TException>(this IAsyncLoaderWithInclude <T> session, string id, Func <TException> thrower) where TException : Exception { var result = await session.LoadAsync <T>(id); if (result == null) { throw thrower(); } return(result); }
/// <summary> /// Loads a document from the session and throws if it's null. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="ids"></param> /// <returns></returns> public static async Task <List <T> > LoadWithoutNulls <T>(this IAsyncLoaderWithInclude <T> session, IEnumerable <string> ids) { if (ids == null) { throw new ArgumentNullException(nameof(ids), "Tried to load required entity but passed in a null ID"); } var results = await session.LoadAsync <T>(ids); return(results.Values .Where(item => item != null) .ToList()); }
/// <summary> /// Loads a document from the session and throws the specified exception if null. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="TException"></typeparam> /// <param name="session"></param> /// <param name="id"></param> /// <param name="thrower"></param> /// <returns></returns> public static async Task <T> LoadRequiredAsync <T, TException>(this IAsyncLoaderWithInclude <T> session, string id, Func <TException> thrower) where TException : Exception { if (id == null) { throw new ArgumentNullException(nameof(id), "Tried to load required entity but passed in a null ID"); } var result = await session.LoadAsync <T>(id); if (result == null) { throw thrower(); } return(result); }
/// <summary> /// Loads a document from the session and throws if it's null. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="id"></param> /// <returns></returns> public static async Task <T> LoadRequiredAsync <T>(this IAsyncLoaderWithInclude <T> session, string id) { if (id == null) { throw new ArgumentNullException(nameof(id), "Tried to load required entity but passed in a null ID"); } var result = await session.LoadAsync <T>(id); if (result == null) { throw new ArgumentException("Tried to load " + id + " but was null."); } return(result); }
/// <summary> /// Asynchronously loads multiple documents from Raven and returns them as Options. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="ids"></param> /// <returns></returns> public static async Task <IEnumerable <Option <T> > > LoadOptionAsync <T>(this IAsyncLoaderWithInclude <T> session, IEnumerable <string> ids) { var result = await session.LoadAsync <T>(ids); return(result.Select(kv => kv.Value.SomeNotNull())); }
/// <summary> /// Asynchronously loads a document from Raven and stores it in an Option. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="session"></param> /// <param name="id"></param> /// <returns></returns> public static async Task <Option <T> > LoadOptionAsync <T>(this IAsyncLoaderWithInclude <T> session, string id) { var result = await session.LoadAsync <T>(id); return(result.SomeNotNull()); }