Ejemplo n.º 1
0
        /// <summary>
        /// Lazily loads a document from the session. When the value is accessed, an exception will be thrown if the document is null.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sessionOps"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Lazy <Task <T> > LoadRequiredAsync <T>(this IAsyncLazySessionOperations sessionOps, string id)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id), "Tried to load required entity but passed in a null ID");
            }

            var loadTask    = sessionOps.LoadAsync <T>(id);
            var wrappedLazy = new Lazy <Task <T> >(() =>
            {
                // When unwrapping the Lazy, return a task that does the actually loading.
                var result = loadTask.Value;
                return(result.ContinueWith(t =>
                {
                    // When unwrapping the Task, return the result of the task. But if it's null, throw an execption.
                    if (t.Result == null)
                    {
                        throw new ArgumentException($"Tried to lazily load {id}, but it wasn't found in the database.");
                    }

                    return t.Result;
                }, TaskContinuationOptions.OnlyOnRanToCompletion));
            }, isThreadSafe: false);

            return(wrappedLazy);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Lazily loads an entity from the Raven. The result is wrapped as an optional value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lazyOps"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Lazy <Task <Option <T> > > LoadOptionAsync <T>(this IAsyncLazySessionOperations lazyOps, string id)
        {
            var loadTask   = lazyOps.LoadAsync <T>(id);
            var optionTask = new Lazy <Task <Option <T> > >(() =>
            {
                var result = loadTask.Value;
                return(result.ContinueWith(t => t.Result.SomeNotNull(), TaskContinuationOptions.OnlyOnRanToCompletion));
            }, isThreadSafe: false);

            return(optionTask);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Lazily loads a document from the session. When the value is accessed, an exception will be thrown if the document is null.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="session"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Lazy <Task <T> > LoadRequiredAsync <T>(this IAsyncLazySessionOperations sessionOps, string id)
        {
            var loadTask    = sessionOps.LoadAsync <T>(id);
            var wrappedLazy = new Lazy <Task <T> >(() =>
            {
                var result = loadTask.Value;
                return(result.ContinueWith(t =>
                {
                    if (t.Result == null)
                    {
                        throw new ArgumentException($"Tried to lazily load {id}, but it wasn't found in the database.");
                    }

                    return t.Result;
                }, TaskContinuationOptions.OnlyOnRanToCompletion));
            }, isThreadSafe: false);

            return(wrappedLazy);
        }