Trim

Nov 4, 2010 at 2:35 PM

Hi,

Can I perform a Trim to all string and string? types using LoopValueInjection.

By the moment I have this functionality inherithing from ValueInjection class.

Thanks in advance,

Kings.

Coordinator
Nov 5, 2010 at 7:09 AM

Hi,

yes, of course you can, here are some solutions:

    public class TrimTests
    {
        [Test]
        public void TrimFromAnonymousToFoo()
        {
            var a = new {Aa = " hello ", Bb = "  yo "};
            var foo = new Foo();
            foo.InjectFrom<TrimStrings>(a);
            Assert.AreEqual("hello", foo.Aa);
            Assert.AreEqual("yo", foo.Bb);
        }

        [Test]
        public void TrimFooToFoo()
        {
            var foo = new Foo {Aa = " a ", Bb = " x "};
            foo.InjectFrom<TrimStrings>(foo);
            Assert.AreEqual("a", foo.Aa);
            Assert.AreEqual("x", foo.Bb);
        }

        [Test]
        public void SelfTrim()
        {
            var foo = new Foo {Aa = " a ", Bb = " b "};
            foo.InjectFrom<TrimSelf>();
            Assert.AreEqual("a", foo.Aa);
            Assert.AreEqual("b", foo.Bb);
        }

        public class TrimStrings : LoopValueInjection<string,string>
        {
            protected override string SetValue(string sourcePropertyValue)
            {
                return sourcePropertyValue.Trim();
            }
        }

        public class TrimSelf : NoSourceValueInjection
        {
            protected override void Inject(object target)
            {
                var props = target.GetProps();
                for (var i = 0; i < props.Count; i++)
                {
                    if (props[i].PropertyType != typeof(string)) continue;
                    var value = props[i].GetValue(target);

                    if(value != null) props[i].SetValue(target, value.ToString().Trim());
                }
            }
        }

        public class Foo
        {
            public string Aa { get; set; }
            public string Bb { get; set; }
        }
    }

kthnxbye,

Omu

Nov 5, 2010 at 8:18 AM
Edited Nov 5, 2010 at 8:19 AM

Thank you very much, Omu.

For the performance point of view, do you consider one solution better than the other, or more or less are all the same?

Thanks again,

Kings.

Coordinator
Nov 5, 2010 at 8:32 AM

the last one could be a nano bit faster, so just use the one that it looks better for you

Nov 5, 2010 at 9:15 AM
Edited Nov 5, 2010 at 9:18 AM

Thank you very much again.

But when I try to use the second class , I'm having this error:

'object' does not contain a definition for 'GetProps' and no extension method 'GetProps' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

I'm using framework 4.0 and ValueInjector 2.0. 

Am I missing some reference?

Kings.

Coordinator
Nov 5, 2010 at 9:27 AM

using Omu.ValueInjecter; and reference to Omu.ValueInjecter.dll should be enough.

using the latest version  is better (2.1)