/
AsyncMultiLoaderWithInclude.cs
130 lines (119 loc) · 3.94 KB
/
AsyncMultiLoaderWithInclude.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#if !NET_3_5
//-----------------------------------------------------------------------
// <copyright file="MultiLoaderWithInclude.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Raven.Abstractions.Extensions;
using Raven.Client.Document.Async;
namespace Raven.Client.Document
{
/// <summary>
/// Fluent implementation for specifying include paths
/// for loading documents
/// </summary>
public class AsyncMultiLoaderWithInclude<T> : IAsyncLoaderWithInclude<T>
{
private readonly AsyncDocumentSession session;
private readonly List<string> includes = new List<string>();
/// <summary>
/// Includes the specified path.
/// </summary>
/// <param name="path">The path.</param>
public AsyncMultiLoaderWithInclude<T> Include(string path)
{
includes.Add(path);
return this;
}
/// <summary>
/// Includes the specified path.
/// </summary>
/// <param name="path">The path.</param>
public AsyncMultiLoaderWithInclude<T> Include(Expression<Func<T, object>> path)
{
return Include(path.ToPropertyPath());
}
/// <summary>
/// Loads the specified ids.
/// </summary>
/// <param name="ids">The ids.</param>
public Task<T[]> LoadAsync(params string[] ids)
{
return session.LoadAsyncInternal<T>(ids, includes.ToArray());
}
/// <summary>
/// Loads the specified id.
/// </summary>
/// <param name="id">The id.</param>
public Task<T> Load(string id)
{
return session.LoadAsyncInternal<T>(new[] {id}, includes.ToArray()).ContinueWith(x => x.Result.FirstOrDefault());
}
/// <summary>
/// Loads the specified entities with the specified id after applying
/// conventions on the provided id to get the real document id.
/// </summary>
/// <remarks>
/// This method allows you to call:
/// Load{Post}(1)
/// And that call will internally be translated to
/// Load{Post}("posts/1");
///
/// Or whatever your conventions specify.
/// </remarks>
public Task<T> Load(ValueType id)
{
var idAsStr = session.Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof (T), false);
return Load(idAsStr);
}
/// <summary>
/// Initializes a new instance of the <see cref="AsyncMultiLoaderWithInclude{T}"/> class.
/// </summary>
/// <param name="session">The session.</param>
public AsyncMultiLoaderWithInclude(AsyncDocumentSession session)
{
this.session = session;
}
/// <summary>
/// Loads the specified ids.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="ids">The ids.</param>
public Task<TResult[]> Load<TResult>(params string[] ids)
{
return session.LoadAsyncInternal<TResult>(ids, includes.ToArray());
}
/// <summary>
/// Loads the specified id.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="id">The id.</param>
public Task<TResult> Load<TResult>(string id)
{
return Load<TResult>(new[] { id }).ContinueWith(x => x.Result.FirstOrDefault());
}
/// <summary>
/// Loads the specified entities with the specified id after applying
/// conventions on the provided id to get the real document id.
/// </summary>
/// <remarks>
/// This method allows you to call:
/// Load{Post}(1)
/// And that call will internally be translated to
/// Load{Post}("posts/1");
///
/// Or whatever your conventions specify.
/// </remarks>
public Task<TResult> Load<TResult>(ValueType id)
{
var idAsStr = session.Conventions.FindFullDocumentKeyFromNonStringIdentifier(id, typeof(T), false);
return Load<TResult>(new[] { idAsStr }).ContinueWith(x => x.Result.FirstOrDefault());
}
}
}
#endif