Skip to content

Commit 787aaa2

Browse files
author
Claus
committed
updating comments for NestedContentValueConnector
1 parent 8e8d860 commit 787aaa2

File tree

2 files changed

+18
-35
lines changed

2 files changed

+18
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ src/Umbraco.Deploy.Contrib.Connectors/App_Plugins/*
3030
src/Umbraco.Deploy.Contrib.Connectors/Config/*
3131
src/Umbraco.Deploy.Contrib.Connectors/app.config
3232
/build/UmbracoDeploy.Contrib.*.nupkg
33+
/build/UmbracoDeploy.Contrib.*.zip

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/NestedContentValueConnector.cs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public class NestedContentValueConnector : IValueConnector
2121
private readonly Lazy<ValueConnectorCollection> _valueConnectorsLazy;
2222
private readonly ILogger _logger;
2323

24+
// Our.Umbraco.NestedContent is the original NestedContent package
25+
// Umbraco.NestedContent is Core NestedContent (introduced in v7.7)
26+
public virtual IEnumerable<string> PropertyEditorAliases => new[] { "Our.Umbraco.NestedContent", "Umbraco.NestedContent" };
27+
28+
// cannot inject ValueConnectorCollection else of course it creates a circular (recursive) dependency,
29+
// so we have to inject it lazily and use the lazy value when actually needing it
30+
private ValueConnectorCollection ValueConnectors => _valueConnectorsLazy.Value;
31+
2432
public NestedContentValueConnector(IContentTypeService contentTypeService, Lazy<ValueConnectorCollection> valueConnectors, ILogger logger)
2533
{
2634
if (contentTypeService == null) throw new ArgumentNullException(nameof(contentTypeService));
@@ -31,8 +39,6 @@ public NestedContentValueConnector(IContentTypeService contentTypeService, Lazy<
3139
_logger = logger;
3240
}
3341

34-
// Our.Umbraco.NestedContent is the original NestedContent package
35-
// Umbraco.NestedContent is Core NestedContent (introduced in v7.7)
3642
public string ToArtifact(object value, PropertyType propertyType, ICollection<ArtifactDependency> dependencies)
3743
{
3844
var svalue = value as string;
@@ -44,9 +50,13 @@ public string ToArtifact(object value, PropertyType propertyType, ICollection<Ar
4450

4551
var nestedContent = new List<NestedContentValue>();
4652
if (svalue.Trim().StartsWith("{"))
53+
{
4754
nestedContent.Add(JsonConvert.DeserializeObject<NestedContentValue>(svalue));
55+
}
4856
else
57+
{
4958
nestedContent.AddRange(JsonConvert.DeserializeObject<NestedContentValue[]>(svalue));
59+
}
5060

5161
if (nestedContent.All(x => x == null))
5262
return null;
@@ -86,13 +96,11 @@ public string ToArtifact(object value, PropertyType propertyType, ICollection<Ar
8696
continue;
8797
}
8898

99+
// fetch the right value connector from the collection of connectors, intended for use with this property type.
89100
// throws if not found - no need for a null check
90101
var propValueConnector = ValueConnectors.Get(propType);
91102

92-
// this should be enough for all other value connectors to work with
93-
// as all they should need is the value, and the property type infos
94-
//var mockProperty = new Property(propType);
95-
//var mockProperty = new Property(propType, row.PropertyValues[key]);
103+
// pass the value, property type and the dependencies collection to the connector to get a "artifact" value
96104
var val = row.PropertyValues[key];
97105
object parsedValue = propValueConnector.ToArtifact(val, propType, dependencies);
98106

@@ -145,25 +153,10 @@ public object FromArtifact(string value, PropertyType propertyType, object curre
145153
throw new InvalidOperationException($"Could not resolve these content types for the Nested Content property: {string.Join(",", allContentTypes.Where(x => x.Value == null).Select(x => x.Key))}");
146154
}
147155

148-
var mocks = new Dictionary<IContentType, IContent>();
149-
150156
foreach (var row in nestedContent)
151157
{
152158
var contentType = allContentTypes[row.ContentTypeAlias];
153159

154-
// note
155-
// the way we do it here, doing content.SetValue() several time on the same content, reduces
156-
// allocations and should be ok because SetValue does not care about the previous value - would
157-
// be different for the overloads that manage eg files for uploads (not sure how NestedContent
158-
// deals with them really)
159-
160-
// we need a fake content instance to pass in to the value connector, since the value connector
161-
// wants to SetValue on an object - then we can extract the value back from that object to set
162-
// it correctly on the real instance
163-
IContent mockContent;
164-
if (!mocks.TryGetValue(contentType, out mockContent))
165-
mockContent = mocks[contentType] = new Content("NC_" + Guid.NewGuid(), -1, contentType);
166-
167160
foreach (var key in row.PropertyValues.Keys.ToArray())
168161
{
169162
// key is a system property that is added by NestedContent in Core v7.7
@@ -179,13 +172,15 @@ public object FromArtifact(string value, PropertyType propertyType, object curre
179172
continue;
180173
}
181174

175+
// fetch the right value connector from the collection of connectors, intended for use with this property type.
182176
// throws if not found - no need for a null check
183177
var propValueConnector = ValueConnectors.Get(innerPropertyType);
184178

185179
var rowValue = row.PropertyValues[key];
186180

187181
if (rowValue != null)
188182
{
183+
// pass the artifact value and property type to the connector to get a real value from the artifact
189184
var convertedValue = propValueConnector.FromArtifact(rowValue.ToString(), innerPropertyType, null);
190185
if (convertedValue == null)
191186
{
@@ -200,7 +195,7 @@ public object FromArtifact(string value, PropertyType propertyType, object curre
200195
{
201196
// test if the value is a json object (thus could be a nested complex editor)
202197
// if that's the case we'll need to add it as a json object instead of string to avoid it being escaped
203-
JToken jtokenValue = convertedValue.ToString().DetectIsJson() ? JToken.Parse(convertedValue.ToString()) : null;
198+
var jtokenValue = convertedValue.ToString().DetectIsJson() ? JToken.Parse(convertedValue.ToString()) : null;
204199
if (jtokenValue != null)
205200
{
206201
row.PropertyValues[key] = jtokenValue;
@@ -223,12 +218,6 @@ public object FromArtifact(string value, PropertyType propertyType, object curre
223218
return value;
224219
}
225220

226-
public virtual IEnumerable<string> PropertyEditorAliases => new[] { "Our.Umbraco.NestedContent", "Umbraco.NestedContent" };
227-
228-
// cannot inject ValueConnectorCollection else of course it creates a circular (recursive) dependency,
229-
// so we have to inject it lazily and use the lazy value when actually needing it
230-
private ValueConnectorCollection ValueConnectors => _valueConnectorsLazy.Value;
231-
232221
/// <summary>
233222
/// The typed value stored for Nested Content
234223
/// </summary>
@@ -249,13 +238,6 @@ public class NestedContentValue
249238
[JsonProperty("ncContentTypeAlias")]
250239
public string ContentTypeAlias { get; set; }
251240

252-
// starting with v7.7, Core's NestedContent implement "key" as a system property
253-
// but since we are supporting pre-v7.7 including the NestedContent package, we
254-
// cannot do it this way - it's all managed "manually" when dealing with
255-
// PropertyValues.
256-
//[JsonProperty("key")]
257-
//public Guid Key { get; set; }
258-
259241
/// <summary>
260242
/// The remaining properties will be serialized to a dictionary
261243
/// </summary>

0 commit comments

Comments
 (0)