From 26aef602054863bcec47b0f91dfd81cd07c17e08 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Tue, 13 Nov 2012 15:20:59 -0800 Subject: [PATCH] Refactoring Bags and moving to Orchard.Framework --HG-- branch : 1.x extra : rebase_source : 0f934cffae32b4477ee39be01bcc63d36982b6b8 --- .../Data/Bags}/SettingsTests.cs | 42 +++++++++---------- .../Orchard.Framework.Tests.csproj | 1 + .../Orchard.Projections.csproj | 12 ++---- .../Services/FormParametersHelper.cs | 4 +- .../Serialization/ISettingsSerializer.cs | 8 ---- .../Tests/Orchard.Projections.Tests.csproj | 1 - .../Settings => Orchard/Data/Bags}/SArray.cs | 4 +- .../Data/Bags}/SConvert.cs | 6 +-- .../Settings => Orchard/Data/Bags}/SItem.cs | 2 +- .../Settings => Orchard/Data/Bags}/SObject.cs | 10 ++--- .../Settings => Orchard/Data/Bags}/SValue.cs | 4 +- .../Data/Bags/Serialization/IBagSerializer.cs | 8 ++++ .../Serialization/XmlSettingsSerializer.cs | 24 +++++------ .../Data/Bags}/StateValueProvider.cs | 2 +- 14 files changed, 62 insertions(+), 66 deletions(-) rename src/{Orchard.Web/Modules/Orchard.Projections/Tests/Settings => Orchard.Tests/Data/Bags}/SettingsTests.cs (81%) delete mode 100644 src/Orchard.Web/Modules/Orchard.Projections/Settings/Serialization/ISettingsSerializer.cs rename src/{Orchard.Web/Modules/Orchard.Projections/Settings => Orchard/Data/Bags}/SArray.cs (92%) rename src/{Orchard.Web/Modules/Orchard.Projections/Settings => Orchard/Data/Bags}/SConvert.cs (95%) rename src/{Orchard.Web/Modules/Orchard.Projections/Settings => Orchard/Data/Bags}/SItem.cs (59%) rename src/{Orchard.Web/Modules/Orchard.Projections/Settings => Orchard/Data/Bags}/SObject.cs (89%) rename src/{Orchard.Web/Modules/Orchard.Projections/Settings => Orchard/Data/Bags}/SValue.cs (82%) create mode 100644 src/Orchard/Data/Bags/Serialization/IBagSerializer.cs rename src/{Orchard.Web/Modules/Orchard.Projections/Settings => Orchard/Data/Bags}/Serialization/XmlSettingsSerializer.cs (81%) rename src/{Orchard.Web/Modules/Orchard.Projections/Settings => Orchard/Data/Bags}/StateValueProvider.cs (87%) diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Tests/Settings/SettingsTests.cs b/src/Orchard.Tests/Data/Bags/SettingsTests.cs similarity index 81% rename from src/Orchard.Web/Modules/Orchard.Projections/Tests/Settings/SettingsTests.cs rename to src/Orchard.Tests/Data/Bags/SettingsTests.cs index a8cefbe2a..deac0f4c3 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Tests/Settings/SettingsTests.cs +++ b/src/Orchard.Tests/Data/Bags/SettingsTests.cs @@ -1,15 +1,15 @@ using NUnit.Framework; -using Orchard.Projections.Settings; +using Orchard.Data.Bags; +using Orchard.Data.Bags.Serialization; using System.Collections.Generic; using System.IO; -using Orchard.Projections.Settings.Serialization; -namespace Orchard.Projections.Tests.Settings { +namespace Orchard.Tests.Data.Bags { [TestFixture] - public class SettingsTests { + public class BagsTests { [Test] public void ShouldRemoveMember() { - dynamic e = new SObject(); + dynamic e = new Bag(); e.Foo = "Bar"; Assert.That(e, Is.Not.Empty); Assert.That(e.Foo, Is.EqualTo("Bar")); @@ -20,7 +20,7 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void ShouldSupportFactoryInvocation() { - var e = SObject.New(); + var e = Bag.New(); e.Foo = "Bar"; Assert.That(e["Foo"], Is.EqualTo("Bar")); @@ -29,7 +29,7 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void ShouldAddDynamicProperties() { - dynamic e = new SObject(); + dynamic e = new Bag(); e.Foo = "Bar"; Assert.That(e["Foo"], Is.EqualTo("Bar")); Assert.That(e.Foo, Is.EqualTo("Bar")); @@ -37,15 +37,15 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void UnknownPropertiesShouldBeNull() { - dynamic e = new SObject(); + dynamic e = new Bag(); Assert.That((object)e["Foo"], Is.EqualTo(null)); Assert.That((object)e.Foo, Is.EqualTo(null)); } [Test] public void ShouldAddDynamicObjects() { - dynamic e = new SObject(); - e.Address = new SObject(); + dynamic e = new Bag(); + e.Address = new Bag(); e.Address.Street = "One Microsoft Way"; Assert.That(e["Address"]["Street"], Is.EqualTo("One Microsoft Way")); @@ -53,7 +53,7 @@ namespace Orchard.Projections.Tests.Settings { } public void ShouldAddArraysOfAnonymousObject() { - dynamic e = new SObject(); + dynamic e = new Bag(); e.Foos = new[] { new { Foo1 = "Bar1", Foo2 = "Bar2" } }; Assert.That(e.Foos[0].Foo1, Is.EqualTo("Bar1")); @@ -61,7 +61,7 @@ namespace Orchard.Projections.Tests.Settings { } public void ShouldAddAnonymousObject() { - dynamic e = new SObject(); + dynamic e = new Bag(); e.Foos = new { Foo1 = "Bar1", Foo2 = "Bar2" }; Assert.That(e.Foos.Foo1, Is.EqualTo("Bar1")); @@ -70,7 +70,7 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void ShouldAddArrays() { - dynamic e = new SObject(); + dynamic e = new Bag(); e.Owners = new[] { "Steve", "Bill" }; Assert.That(e.Owners[0], Is.EqualTo("Steve")); Assert.That(e.Owners[1], Is.EqualTo("Bill")); @@ -78,8 +78,8 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void ShouldBeEnumerable() { - dynamic e = new SObject(); - e.Address = new SObject(); + dynamic e = new Bag(); + e.Address = new Bag(); e.Address.Street = "One Microsoft Way"; e.Foos = new[] { new { Foo1 = "Bar1", Foo2 = "Bar2" } }; @@ -93,11 +93,11 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void ShouldSerializeAndDeserialize() { - dynamic e = new SObject(); + dynamic e = new Bag(); e.Foo = "Bar"; - e.Address = new SObject(); + e.Address = new Bag(); e.Address.Street = "One Microsoft Way"; e.Owners = new[] { "Steve", "Bill" }; e.Foos = new[] { new { Foo1 = "Bar1", Foo2 = "Bar2" } }; @@ -128,11 +128,11 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void MergeShouldOverwriteExistingProperties() { - var o1 = SObject.New(); + var o1 = Bag.New(); o1.Foo = "Foo1"; o1.Bar = "Bar1"; - var o2 = SObject.New(); + var o2 = Bag.New(); o2.Foo = "Foo2"; o2.Baz = "Baz2"; @@ -145,10 +145,10 @@ namespace Orchard.Projections.Tests.Settings { [Test] public void MergeShouldConcatenateArrays() { - var o1 = SObject.New(); + var o1 = Bag.New(); o1.Foo = new[] { "a", "b" }; - var o2 = SObject.New(); + var o2 = Bag.New(); o2.Foo = new[] { "c", "d" }; var o3 = o1 & o2; diff --git a/src/Orchard.Tests/Orchard.Framework.Tests.csproj b/src/Orchard.Tests/Orchard.Framework.Tests.csproj index 1ff295d27..0e2f29486 100644 --- a/src/Orchard.Tests/Orchard.Framework.Tests.csproj +++ b/src/Orchard.Tests/Orchard.Framework.Tests.csproj @@ -217,6 +217,7 @@ + diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Orchard.Projections.csproj b/src/Orchard.Web/Modules/Orchard.Projections/Orchard.Projections.csproj index 397997902..fdc36195e 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Orchard.Projections.csproj +++ b/src/Orchard.Web/Modules/Orchard.Projections/Orchard.Projections.csproj @@ -21,6 +21,10 @@ false + + + + true @@ -217,16 +221,8 @@ - - - - - - - - diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Services/FormParametersHelper.cs b/src/Orchard.Web/Modules/Orchard.Projections/Services/FormParametersHelper.cs index 862164590..608466502 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Services/FormParametersHelper.cs +++ b/src/Orchard.Web/Modules/Orchard.Projections/Services/FormParametersHelper.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Xml; using System.Xml.Linq; -using Orchard.Projections.Settings; +using Orchard.Data.Bags; namespace Orchard.Projections.Services { public static class FormParametersHelper { @@ -43,7 +43,7 @@ namespace Orchard.Projections.Services { public static dynamic ToDynamic(string parameters) { - var result = SObject.New(); + var result = Bag.New(); if (String.IsNullOrEmpty(parameters)) { return result; diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/Serialization/ISettingsSerializer.cs b/src/Orchard.Web/Modules/Orchard.Projections/Settings/Serialization/ISettingsSerializer.cs deleted file mode 100644 index d7dcf3deb..000000000 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/Serialization/ISettingsSerializer.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System.IO; - -namespace Orchard.Projections.Settings.Serialization { - public interface ISettingsSerializer : IDependency { - void Serialize(TextWriter tw, SObject o); - SObject Deserialize(TextReader tr); - } -} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Tests/Orchard.Projections.Tests.csproj b/src/Orchard.Web/Modules/Orchard.Projections/Tests/Orchard.Projections.Tests.csproj index 6acee7922..3402825e3 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Tests/Orchard.Projections.Tests.csproj +++ b/src/Orchard.Web/Modules/Orchard.Projections/Tests/Orchard.Projections.Tests.csproj @@ -66,7 +66,6 @@ - diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SArray.cs b/src/Orchard/Data/Bags/SArray.cs similarity index 92% rename from src/Orchard.Web/Modules/Orchard.Projections/Settings/SArray.cs rename to src/Orchard/Data/Bags/SArray.cs index 1f740bcf3..5c356b1d2 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SArray.cs +++ b/src/Orchard/Data/Bags/SArray.cs @@ -3,7 +3,7 @@ using System.Dynamic; using System.Linq; using System.Reflection; -namespace Orchard.Projections.Settings { +namespace Orchard.Data.Bags { public class SArray : DynamicObject, ISItem { public ISItem[] Values { get; private set; } @@ -70,7 +70,7 @@ namespace Orchard.Projections.Settings { return new SArray(o1.Values.Union(new[] { o1 }).ToArray()); } - public static SObject operator &(SArray o1, SObject o2) { + public static Bag operator &(SArray o1, Bag o2) { return o2; } diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SConvert.cs b/src/Orchard/Data/Bags/SConvert.cs similarity index 95% rename from src/Orchard.Web/Modules/Orchard.Projections/Settings/SConvert.cs rename to src/Orchard/Data/Bags/SConvert.cs index c1044cc29..87ef9908c 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SConvert.cs +++ b/src/Orchard/Data/Bags/SConvert.cs @@ -4,14 +4,14 @@ using System.Reflection; using System.Globalization; using System.Xml; -namespace Orchard.Projections.Settings { +namespace Orchard.Data.Bags { public class SConvert { public static ISItem ToSettings(object o) { if (o is SValue) { return (ISItem)o; } - if (o is SObject) { + if (o is Bag) { return (ISItem)o; } @@ -24,7 +24,7 @@ namespace Orchard.Projections.Settings { } if (IsAnonymousType(o.GetType())) { - dynamic grappe = new SObject(); + dynamic grappe = new Bag(); foreach (var p in o.GetType().GetProperties()) { grappe[p.Name] = p.GetValue(o, null); diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SItem.cs b/src/Orchard/Data/Bags/SItem.cs similarity index 59% rename from src/Orchard.Web/Modules/Orchard.Projections/Settings/SItem.cs rename to src/Orchard/Data/Bags/SItem.cs index c817af367..2f98a37b8 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SItem.cs +++ b/src/Orchard/Data/Bags/SItem.cs @@ -1,6 +1,6 @@ using System; -namespace Orchard.Projections.Settings { +namespace Orchard.Data.Bags { public interface ISItem : ICloneable { } diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SObject.cs b/src/Orchard/Data/Bags/SObject.cs similarity index 89% rename from src/Orchard.Web/Modules/Orchard.Projections/Settings/SObject.cs rename to src/Orchard/Data/Bags/SObject.cs index 9e2ea16c2..028bd730d 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SObject.cs +++ b/src/Orchard/Data/Bags/SObject.cs @@ -1,12 +1,12 @@ using System.Collections.Generic; using System.Dynamic; -namespace Orchard.Projections.Settings { - public class SObject : DynamicObject, IEnumerable>, ISItem { +namespace Orchard.Data.Bags { + public class Bag : DynamicObject, IEnumerable>, ISItem { internal readonly Dictionary _properties = new Dictionary(); public static dynamic New() { - return new SObject(); + return new Bag(); } public override bool TrySetMember(SetMemberBinder binder, object value) { @@ -52,12 +52,12 @@ namespace Orchard.Projections.Settings { return GetMember(indexes[0].ToString(), out result); } - public static SObject operator &(SObject o1, SObject o2) { + public static Bag operator &(Bag o1, Bag o2) { if (o1 == null) { return o2; } - var clone = (SObject)o1.Clone(); + var clone = (Bag)o1.Clone(); dynamic dclone = clone; foreach (var pair in o2._properties) { diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SValue.cs b/src/Orchard/Data/Bags/SValue.cs similarity index 82% rename from src/Orchard.Web/Modules/Orchard.Projections/Settings/SValue.cs rename to src/Orchard/Data/Bags/SValue.cs index 772825670..4b35f5c9f 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/SValue.cs +++ b/src/Orchard/Data/Bags/SValue.cs @@ -1,6 +1,6 @@ using System.Linq; -namespace Orchard.Projections.Settings { +namespace Orchard.Data.Bags { public class SValue : ISItem { public object Value { get; set; } @@ -23,7 +23,7 @@ namespace Orchard.Projections.Settings { return new SArray(new[] { o1 }.Union(o2.Values).ToArray()); } - public static SObject operator &(SValue o1, SObject o2) { + public static Bag operator &(SValue o1, Bag o2) { return o2; } diff --git a/src/Orchard/Data/Bags/Serialization/IBagSerializer.cs b/src/Orchard/Data/Bags/Serialization/IBagSerializer.cs new file mode 100644 index 000000000..65bed3a92 --- /dev/null +++ b/src/Orchard/Data/Bags/Serialization/IBagSerializer.cs @@ -0,0 +1,8 @@ +using System.IO; + +namespace Orchard.Data.Bags.Serialization { + public interface IBagSerializer : IDependency { + void Serialize(TextWriter tw, Bag o); + Bag Deserialize(TextReader tr); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/Serialization/XmlSettingsSerializer.cs b/src/Orchard/Data/Bags/Serialization/XmlSettingsSerializer.cs similarity index 81% rename from src/Orchard.Web/Modules/Orchard.Projections/Settings/Serialization/XmlSettingsSerializer.cs rename to src/Orchard/Data/Bags/Serialization/XmlSettingsSerializer.cs index 650745ef4..5f58ba1e9 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/Serialization/XmlSettingsSerializer.cs +++ b/src/Orchard/Data/Bags/Serialization/XmlSettingsSerializer.cs @@ -3,11 +3,11 @@ using System.Collections.Generic; using System.IO; using System.Xml; -namespace Orchard.Projections.Settings.Serialization { - public class XmlSettingsSerializer : ISettingsSerializer { - private const string Root = "SObject"; +namespace Orchard.Data.Bags.Serialization { + public class XmlSettingsSerializer : IBagSerializer { + private const string Root = "Bag"; - public void Serialize(TextWriter tw, SObject o) { + public void Serialize(TextWriter tw, Bag o) { using (var writer = new XmlTextWriter(tw)) { writer.WriteStartDocument(); writer.WriteStartElement(Root); @@ -17,9 +17,9 @@ namespace Orchard.Projections.Settings.Serialization { } } - public SObject Deserialize(TextReader tr) { + public Bag Deserialize(TextReader tr) { var reader = new XmlTextReader(tr); - var result = new SObject(); + var result = new Bag(); // ignore root element while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == Root) { @@ -33,7 +33,7 @@ namespace Orchard.Projections.Settings.Serialization { return result; } - private void ReadElement(XmlReader reader, SObject parent) { + private void ReadElement(XmlReader reader, Bag parent) { var name = XmlConvert.DecodeName(reader.LocalName); var type = reader["type"]; @@ -51,7 +51,7 @@ namespace Orchard.Projections.Settings.Serialization { } } else { - var grappe = new SObject(); + var grappe = new Bag(); reader.Read(); parent.SetMember(name, grappe); while (reader.MoveToContent() == XmlNodeType.Element) { @@ -66,7 +66,7 @@ namespace Orchard.Projections.Settings.Serialization { var list = new List(); reader.Read(); while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "Item") { - dynamic o = new SObject(); + dynamic o = new Bag(); ReadElement(reader, o); list.Add(o.Item); } @@ -74,16 +74,16 @@ namespace Orchard.Projections.Settings.Serialization { return new SArray(list.ToArray()); } - private void WriteGrappe(XmlWriter writer, SObject grappe) { + private void WriteGrappe(XmlWriter writer, Bag grappe) { foreach (var pair in grappe._properties) { WriteAny(writer, pair.Key, pair.Value); } } private void WriteAny(XmlWriter writer, string name, object value) { - if (value is SObject) { + if (value is Bag) { writer.WriteStartElement(XmlConvert.EncodeLocalName(name)); - WriteGrappe(writer, (SObject)value); + WriteGrappe(writer, (Bag)value); writer.WriteEndElement(); } if (value is SArray) { diff --git a/src/Orchard.Web/Modules/Orchard.Projections/Settings/StateValueProvider.cs b/src/Orchard/Data/Bags/StateValueProvider.cs similarity index 87% rename from src/Orchard.Web/Modules/Orchard.Projections/Settings/StateValueProvider.cs rename to src/Orchard/Data/Bags/StateValueProvider.cs index 6d20eeca0..90193d5c4 100644 --- a/src/Orchard.Web/Modules/Orchard.Projections/Settings/StateValueProvider.cs +++ b/src/Orchard/Data/Bags/StateValueProvider.cs @@ -1,6 +1,6 @@ using System.Web.Mvc; -namespace Orchard.Projections.Settings { +namespace Orchard.Data.Bags { public class SettingsValueProvider : IValueProvider { private readonly dynamic _state;