This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
CompositeBindingSource.cs
116 lines (102 loc) · 4.11 KB
/
CompositeBindingSource.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Abstractions;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
/// <summary>
/// A <see cref="BindingSources"/> which can represent multiple value-provider data sources.
/// </summary>
public class CompositeBindingSource : BindingSource
{
/// <summary>
/// Creates a new <see cref="CompositeBindingSource"/>.
/// </summary>
/// <param name="bindingSources">
/// The set of <see cref="BindingSource"/> entries.
/// Must be value-provider sources and user input.
/// </param>
/// <param name="displayName">The display name for the composite source.</param>
/// <returns>A <see cref="CompositeBindingSource"/>.</returns>
public static CompositeBindingSource Create(
IEnumerable<BindingSource> bindingSources,
string displayName)
{
if (bindingSources == null)
{
throw new ArgumentNullException(nameof(bindingSources));
}
foreach (var bindingSource in bindingSources)
{
if (bindingSource.IsGreedy)
{
var message = Resources.FormatBindingSource_CannotBeGreedy(
bindingSource.DisplayName,
nameof(CompositeBindingSource));
throw new ArgumentException(message, nameof(bindingSources));
}
if (!bindingSource.IsFromRequest)
{
var message = Resources.FormatBindingSource_MustBeFromRequest(
bindingSource.DisplayName,
nameof(CompositeBindingSource));
throw new ArgumentException(message, nameof(bindingSources));
}
if (bindingSource is CompositeBindingSource)
{
var message = Resources.FormatBindingSource_CannotBeComposite(
bindingSource.DisplayName,
nameof(CompositeBindingSource));
throw new ArgumentException(message, nameof(bindingSources));
}
}
var id = string.Join("&", bindingSources.Select(s => s.Id).OrderBy(s => s, StringComparer.Ordinal));
return new CompositeBindingSource(id, displayName, bindingSources);
}
private CompositeBindingSource(
string id,
string displayName,
IEnumerable<BindingSource> bindingSources)
: base(id, displayName, isGreedy: false, isFromRequest: true)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
if (bindingSources == null)
{
throw new ArgumentNullException(nameof(bindingSources));
}
BindingSources = bindingSources;
}
/// <summary>
/// Gets the set of <see cref="BindingSource"/> entries.
/// </summary>
public IEnumerable<BindingSource> BindingSources { get; }
/// <inheritdoc />
public override bool CanAcceptDataFrom(BindingSource bindingSource)
{
if (bindingSource == null)
{
throw new ArgumentNullException(nameof(bindingSource));
}
if (bindingSource is CompositeBindingSource)
{
var message = Resources.FormatBindingSource_CannotBeComposite(
bindingSource.DisplayName,
nameof(CanAcceptDataFrom));
throw new ArgumentException(message, nameof(bindingSource));
}
foreach (var source in BindingSources)
{
if (source.CanAcceptDataFrom(bindingSource))
{
return true;
}
}
return false;
}
}
}