This project is read-only.

Ignore interface properties in generic method

Mar 26, 2010 at 2:24 PM

 

The last test causes error "Must be a property or valid method". The first test executes OK.
using System.Runtime.Serialization;
using Glue;
using NUnit.Framework;
namespace eBOSS.Domain.Shared.Tests.Base
{
    [TestFixture]
    public class GlueMappingTest
    {
        private class ServiceDtoType : IExtensibleDataObject
        {
            public string SomeProperty {get;set;}
            public ExtensionDataObject ExtensionData { get; set; }
        }
        private class SharedDtoType
        {
            public string SomeProperty { get; set; }
        }
        private void IgnoreExtensionDataOnTarget<TOther, TExtensible>(Mapping<TOther, TExtensible> mapping)
            where TExtensible : IExtensibleDataObject
        {
            mapping.IgnoreTowards<TExtensible>(x => x.ExtensionData);
        }
        private SharedDtoType sharedDto;
        private ServiceDtoType serviceDto;
        [SetUp]
        public void SetUp()
        {
            sharedDto = new SharedDtoType();
            serviceDto = new ServiceDtoType();
        }
        [Test]
        public void Mapping_should_ignore_ExtensionData()
        {
            var mapping = new Mapping<SharedDtoType, ServiceDtoType>();
            mapping.AutoRelateEqualNames();
            mapping.IgnoreTowards<ServiceDtoType>(x => x.ExtensionData);
        }
        [Test]
        public void Mapping_should_ignore_ExtensionData_in_generic()
        {
            var mapping = new Mapping<SharedDtoType, ServiceDtoType>();
            mapping.AutoRelateEqualNames();
            IgnoreExtensionDataOnTarget(mapping);
        }
    }
}
Mapping.IgnoreTowards<T> is called with this value in propertyToIgnore:
{x => Convert(x).ExtensionData}
Tried this fix in LambdaDecomposer which seems to work:
private void HandlePropertyOrField(Expression expression)
{
var memberExpression = (MemberExpression) expression;
var exp = memberExpression.Expression;
if (exp.NodeType == ExpressionType.Convert)
{
exp = ((UnaryExpression)memberExpression.Expression).Operand;
}
if (exp.NodeType != ExpressionType.Parameter)
{
getNestedParentObject = new CompiledExpression(exp, lambda.Parameters.ToArray());
}
var member = memberExpression.Member;
propertyInfo = member as PropertyInfo;
fieldinfo = member as FieldInfo;
}
The last test causes error "Must be a property or valid method". The first test executes OK.
using System.Runtime.Serialization;
using Glue;
using NUnit.Framework;
namespace eBOSS.Domain.Shared.Tests.Base
{
    [TestFixture]
    public class GlueMappingTest
    {
        private class ServiceDtoType : IExtensibleDataObject
        {
            public string SomeProperty {get;set;}
            public ExtensionDataObject ExtensionData { get; set; }
        }
        private class SharedDtoType
        {
            public string SomeProperty { get; set; }
        }
        private void IgnoreExtensionDataOnTarget<TOther, TExtensible>(Mapping<TOther, TExtensible> mapping)
            where TExtensible : IExtensibleDataObject
        {
            mapping.IgnoreTowards<TExtensible>(x => x.ExtensionData);
        }
        private SharedDtoType sharedDto;
        private ServiceDtoType serviceDto;
        [SetUp]
        public void SetUp()
        {
            sharedDto = new SharedDtoType();
            serviceDto = new ServiceDtoType();
        }
        [Test]
        public void Mapping_should_ignore_ExtensionData()
        {
            var mapping = new Mapping<SharedDtoType, ServiceDtoType>();
            mapping.AutoRelateEqualNames();
            mapping.IgnoreTowards<ServiceDtoType>(x => x.ExtensionData);
        }
        [Test]
        public void Mapping_should_ignore_ExtensionData_in_generic()
        {
            var mapping = new Mapping<SharedDtoType, ServiceDtoType>();
            mapping.AutoRelateEqualNames();
            IgnoreExtensionDataOnTarget(mapping);
        }
    }
}
Mapping.IgnoreTowards<T> is called with this value in propertyToIgnore:
{x => Convert(x).ExtensionData}
Tried this fix in LambdaDecomposer which seems to work:
private void HandlePropertyOrField(Expression expression)
{
var memberExpression = (MemberExpression) expression;
var exp = memberExpression.Expression;
if (exp.NodeType == ExpressionType.Convert)
{
exp = ((UnaryExpression)memberExpression.Expression).Operand;
}
if (exp.NodeType != ExpressionType.Parameter)
{
getNestedParentObject = new CompiledExpression(exp, lambda.Parameters.ToArray());
}
var member = memberExpression.Member;
propertyInfo = member as PropertyInfo;
fieldinfo = member as FieldInfo;
}
Hi,
I am using Glue to map partially defined Service Data Transfer Objects (DTOs) to fully defined Shared DTOs.
As part of that I need to ignore the ExtensionData property that all the Service DTOs implement.
The test Mapping_should_ignore_ExtensionData  executes OK.
The test Mapping_should_ignore_ExtensionData_in_generic causes the error "Must be a property or valid method".
using System.Runtime.Serialization;
using Glue;
using NUnit.Framework;
namespace eBOSS.Domain.Shared.Tests.Base
{
    [TestFixture]
    public class GlueMappingTest
    {
        private class ServiceDtoType : IExtensibleDataObject
        {
            public string SomeProperty {get;set;}
            public ExtensionDataObject ExtensionData { get; set; }
        }
        private class SharedDtoType
        {
            public string SomeProperty { get; set; }
        }
        private void IgnoreExtensionDataOnTarget<TOther, TExtensible>(Mapping<TOther, TExtensible> mapping)
            where TExtensible : IExtensibleDataObject
        {
            mapping.IgnoreTowards<TExtensible>(x => x.ExtensionData);
        }
        private SharedDtoType sharedDto;
        private ServiceDtoType serviceDto;
        [SetUp]
        public void SetUp()
        {
            sharedDto = new SharedDtoType();
            serviceDto = new ServiceDtoType();
        }
        [Test]
        public void Mapping_should_ignore_ExtensionData()
        {
            var mapping = new Mapping<SharedDtoType, ServiceDtoType>();
            mapping.AutoRelateEqualNames();
            mapping.IgnoreTowards<ServiceDtoType>(x => x.ExtensionData);
        }
        [Test]
        public void Mapping_should_ignore_ExtensionData_in_generic()
        {
            var mapping = new Mapping<SharedDtoType, ServiceDtoType>();
            mapping.AutoRelateEqualNames();
            IgnoreExtensionDataOnTarget(mapping);
        }
    }
}
Mapping.IgnoreTowards<T> is called with this value in propertyToIgnore:
{x => Convert(x).ExtensionData}
Tried this fix in LambdaDecomposer which seems to work:
private void HandlePropertyOrField(Expression expression)
{
var memberExpression = (MemberExpression) expression;
var exp = memberExpression.Expression;
if (exp.NodeType == ExpressionType.Convert)
{
exp = ((UnaryExpression)memberExpression.Expression).Operand;
}
if (exp.NodeType != ExpressionType.Parameter)
{
getNestedParentObject = new CompiledExpression(exp, lambda.Parameters.ToArray());
}
var member = memberExpression.Member;
propertyInfo = member as PropertyInfo;
fieldinfo = member as FieldInfo;
}

 

Mar 27, 2010 at 2:00 PM

Thanks for patch!

I've added the fix in changeset #41176. I also wrote a few tests for it. You'll find them under the LamdaDecomposerTests-folder.

- Tore Vestues