Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

saving interfaces

  • Loading branch information...
commit 8fdd0920a46ac3282e523c62db14aa567c235730 1 parent 3fa0f09
@mikeedwards83 authored
View
16 Source/Glass.Mapper/Context.cs
@@ -161,10 +161,20 @@ public AbstractTypeConfiguration GetTypeConfiguration(object obj)
{
var type = obj.GetType();
var config = TypeConfigurations.ContainsKey(type) ? TypeConfigurations[type] : null;
-
+
+ if (config != null) return config;
+
//check base type encase of proxy
- if (config == null)
- config = TypeConfigurations.ContainsKey(type.BaseType) ? TypeConfigurations[type.BaseType] : null;
+ config = TypeConfigurations.ContainsKey(type.BaseType) ? TypeConfigurations[type.BaseType] : null;
+
+ if (config != null) return config;
+
+ //check interfaces encase this is an interface proxy
+ string name = type.Name;
+ var interfaceType = type.GetInterfaces().FirstOrDefault(x => name.Contains(x.Name));
+
+ if (interfaceType != null)
+ config = TypeConfigurations.ContainsKey(interfaceType) ? TypeConfigurations[interfaceType] : null;
return config;
}
View
43 ...Mapper/Pipelines/ObjectConstruction/Tasks/CreateInterface/InterfacePropertyInterceptor.cs
@@ -8,12 +8,53 @@ namespace Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateInterface
{
public class InterfacePropertyInterceptor : IInterceptor
{
+ private readonly ObjectConstructionArgs _args;
+ Dictionary<string, object> _values;
+ bool _isLoaded = false;
+
public InterfacePropertyInterceptor(ObjectConstructionArgs args)
{
+ _args = args;
}
+
public void Intercept(IInvocation invocation)
{
- throw new NotImplementedException();
+ //do initial gets
+ if (!_isLoaded)
+ {
+ _values = new Dictionary<string, object>();
+ var config = _args.Configuration;
+ var mappingContext = _args.Service.CreateDataMappingContext(_args.AbstractTypeCreationContext, null);
+
+ foreach (var property in config.Properties)
+ {
+ var result = property.Mapper.MapToProperty(mappingContext);
+ _values[property.PropertyInfo.Name] = result;
+ }
+ _isLoaded = true;
+ }
+
+ if (invocation.Method.IsSpecialName)
+ {
+ if (invocation.Method.Name.StartsWith("get_") || invocation.Method.Name.StartsWith("set_"))
+ {
+
+ string method = invocation.Method.Name.Substring(0, 4);
+ string name = invocation.Method.Name.Substring(4);
+
+ if (method == "get_")
+ {
+ var result = _values[name];
+ invocation.ReturnValue = result;
+ }
+ else if (method == "set_")
+ {
+ _values[name] = invocation.Arguments[0];
+ }
+ else
+ throw new MapperException("Method with name {0}{1} on type {2} not supported.".Formatted(method, name, _args.Configuration.Type.FullName));
+ }
+ }
}
}
}
View
56 Tests/Integration Tests/Sitecore/Glass.Mapper.Sc.Integration/SitecoreServiceFixture.cs
@@ -135,6 +135,52 @@ public void Save_ItemDisplayNameChangedUsingProxy_SavesDisplayName()
}
}
+
+ [Test]
+ public void Save_ItemDisplayNameChangedUsingProxyUsingInterface_SavesDisplayName()
+ {
+ //Assign
+ var itemPath = "/sitecore/content/Tests/SitecoreService/Save/EmptyItem";
+ string expected = "new name";
+
+ var db = Sitecore.Configuration.Factory.GetDatabase("master");
+ var context = Context.Create(new GlassConfig());
+ context.Load(new SitecoreAttributeConfigurationLoader("Glass.Mapper.Sc.Integration"));
+ var currentItem = db.GetItem(itemPath);
+ var service = new SitecoreService(db, context);
+
+
+ //setup item
+ using (new SecurityDisabler())
+ {
+ currentItem.Editing.BeginEdit();
+ currentItem[Global.Fields.DisplayName] = "old name";
+ currentItem.Editing.EndEdit();
+ }
+
+ var cls = service.GetItem<IStubSaving>(itemPath);
+
+
+
+ using (new SecurityDisabler())
+ {
+ //Act
+ cls.Name = expected;
+ service.Save(cls);
+
+ //Assert
+ var newItem = db.GetItem(itemPath);
+
+ Assert.AreEqual(expected, newItem[Global.Fields.DisplayName]);
+
+ Assert.IsTrue(cls is IStubSaving);
+ Assert.AreNotEqual(typeof(IStubSaving), cls.GetType());
+ }
+
+ }
+
+
+
#endregion
#region Method - CreateClasses
@@ -448,6 +494,16 @@ public class StubSaving
}
[SitecoreType]
+ public interface IStubSaving
+ {
+ [SitecoreId]
+ Guid Id { get; set; }
+
+ [SitecoreInfo(Type = SitecoreInfoType.DisplayName)]
+ string Name { get; set; }
+ }
+
+ [SitecoreType]
public class StubClass{
public DateTime Param3 { get; set; }
public bool Param4 { get; set; }
Please sign in to comment.
Something went wrong with that request. Please try again.