This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
CurrentContent attribute and value-resolver #79
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
54dad11
Added unit-test for a complex nested property.
leekelleher edfe8fc
Added attribute and value-resolver for `CurrentContent` (IPublishedCo…
leekelleher dcb0271
Renamed the model for the unit-test.
leekelleher ad661f2
Added XML docs
leekelleher 24611cd
Extended `GetTypedValue` to check if the property-value is an `IPubli…
leekelleher 0775e66
Separated out the unit-test for CurrentContent.
leekelleher 2e3c5a4
Merged
mattbrailsford 3bb663a
Added IEnumerable<IPublishedContent> check (needs unit test)
mattbrailsford 3331e29
Fixed broken unit test due to old test not being removed during merge
mattbrailsford a1c9405
Fixed non-passing unit tests
mattbrailsford 1821658
Renamed CurrentContentAttribute to CurrentContentAsAttribute to be mo…
mattbrailsford 362327c
Merge branch 'develop' into feature/current-content-resolver
mattbrailsford 2975191
Fixed issues after merge
mattbrailsford 4d2a7e7
Added unit tests for recursive .As usage
mattbrailsford File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/Our.Umbraco.Ditto/Attributes/CurrentContentAsAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Umbraco.Core.Models; | ||
|
||
namespace Our.Umbraco.Ditto | ||
{ | ||
using System; | ||
|
||
/// <summary> | ||
/// The current content attribute. | ||
/// Used for providing Ditto with the current <see cref="IPublishedContent"/> object from Umbraco. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | ||
public class CurrentContentAsAttribute : DittoValueResolverAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CurrentContentAsAttribute"/> class. | ||
/// </summary> | ||
public CurrentContentAsAttribute() | ||
: base(typeof(CurrentContentAsValueResolver)) | ||
{ } | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Our.Umbraco.Ditto/ComponentModel/ValueResolvers/CurrentContentAsValueResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace Our.Umbraco.Ditto | ||
{ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
|
||
using global::Umbraco.Core.Models; | ||
|
||
/// <summary> | ||
/// The current content value resolver. | ||
/// </summary> | ||
public class CurrentContentAsValueResolver : DittoValueResolver<CurrentContentAsAttribute> | ||
{ | ||
/// <summary> | ||
/// Resolves the value. | ||
/// Gets the current <see cref="IPublishedContent"/> from Umbraco. | ||
/// </summary> | ||
/// <param name="context"> | ||
/// An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context. | ||
/// </param> | ||
/// <param name="attribute"> | ||
/// The <see cref="CurrentContentAsAttribute"/> containing additional information | ||
/// indicating how to resolve the property. | ||
/// </param> | ||
/// <param name="culture">The <see cref="T:System.Globalization.CultureInfo" /> to use as the current culture.</param> | ||
/// <returns> | ||
/// The <see cref="object"/> representing the raw value. | ||
/// </returns> | ||
public override object ResolveValue(ITypeDescriptorContext context, CurrentContentAsAttribute attribute, CultureInfo culture) | ||
{ | ||
// NOTE: [LK] In order to prevent an infinite loop / stack-overflow, we check if the | ||
// property's type matches the containing model's type, then we throw an exception. | ||
if (context.PropertyDescriptor.PropertyType == context.PropertyDescriptor.ComponentType) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format("Unable to process property type '{0}', it is the same as the containing model type.", | ||
context.PropertyDescriptor.PropertyType.Name)); | ||
} | ||
|
||
return ((IPublishedContent)context.Instance).As(context.PropertyDescriptor.PropertyType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
namespace Our.Umbraco.Ditto.Tests | ||
{ | ||
using System; | ||
using NUnit.Framework; | ||
using Our.Umbraco.Ditto.Tests.Mocks; | ||
|
||
[TestFixture] | ||
public class CurrentContentTests | ||
{ | ||
public class MyModel | ||
{ | ||
[CurrentContentAs] | ||
public MyMetaDataModel MetaData1 { get; set; } | ||
|
||
public MyMetaDataModel MetaData2 { get; set; } | ||
} | ||
|
||
public class MyMetaDataModel | ||
{ | ||
public string MetaTitle { get; set; } | ||
|
||
public string MetaDescription { get; set; } | ||
|
||
public string MetaKeywords { get; set; } | ||
} | ||
|
||
public class MyCircularReferenceModel | ||
{ | ||
[CurrentContentAs] | ||
public MyCircularReferenceModel MyCircularReferenceProperty { get; set; } | ||
} | ||
|
||
[Test] | ||
public void CurrentContent_Property_Mapped() | ||
{ | ||
var metaTitle = new PublishedContentPropertyMock | ||
{ | ||
Alias = "metaTitle", | ||
Value = "This is the meta title" | ||
}; | ||
var metaDescription = new PublishedContentPropertyMock | ||
{ | ||
Alias = "metaDescription", | ||
Value = "This is the meta description" | ||
}; | ||
var metaKeywords = new PublishedContentPropertyMock | ||
{ | ||
Alias = "metaKeywords", | ||
Value = "these,are,meta,keywords" | ||
}; | ||
|
||
var content = new PublishedContentMock | ||
{ | ||
Properties = new[] { metaTitle, metaDescription, metaKeywords } | ||
}; | ||
|
||
var model = content.As<MyModel>(); | ||
|
||
Assert.That(model, Is.Not.Null); | ||
Assert.That(model.MetaData1, Is.Not.Null, "We expect the property to be populated."); | ||
Assert.That(model.MetaData1, Is.TypeOf<MyMetaDataModel>()); | ||
Assert.That(model.MetaData2, Is.Null, "We expect the property not to be populated."); | ||
} | ||
|
||
[Test] | ||
public void CurrentContent_InfineLoop_Check() | ||
{ | ||
var content = new PublishedContentMock(); | ||
|
||
TestDelegate code = () => { content.As<MyCircularReferenceModel>(); }; | ||
|
||
Assert.Throws<InvalidOperationException>(code); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
|
||
namespace Our.Umbraco.Ditto.Tests.Models | ||
{ | ||
public class InnerContentModel | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public InnerContentModel2 ContentProp { get; set; } | ||
|
||
public IEnumerable<InnerContentModel2> ContentListProp { get; set; } | ||
} | ||
|
||
public class InnerContentModel2 | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public string InnerProp { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to add additional check for IEnumerable< IPublishedContent> where to result type is IEnumerable< Entity> and do ditto cast there too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this as simple as checking for
propertyValue is IEnumerable<IPublishedContent>
? or do we have to get funky with all the.IsGenericType
checks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, I think might be able to do IsAssignableTo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
propertyValue.IsEnumerableOfType(typeof(IPublishedContent))
Honeymoon is next week 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @JimBobSquarePants!
(See you next week - does it sound weird that we'll see you on your honeymoon?)