# OO Mapper 实践（上篇）

上一篇 介绍了OO Mapper的作用以及支持的功能。这一篇将以上一篇为提纲进行实践。

准备工作

• 下载NLite
•  编译NLite 项目
•  创建一个NUnit单元测试项目
•  添加NLite.dll 引用
•   添加 : using NLite;

实践：

1. Any -> String

Assert.AreEqual("5", Mapper.Map<int, string>(5));


2. primitive->Enum,Enum->primitive

   [TestFixture]
public class EnumTests
{
public enum En1 : byte
{
a = 1,
b = 2,
c = 3
}

public enum En2 : long
{
a = 1,
b = 2,
c = 3
}

public enum En3 : int
{
b = 2,
c = 3,
a = 1
}

public class A
{
public En1 en1 { get; set; }
public En2 en2;
public En3 en3 { get; set; }
public decimal en4;
public string en5;
public En1? en6;
public En3 en7;
public En3? en8;
public En3? en9 = En3.c;
}

public class B
{
public decimal en1 = 3;
public En1 en2 { get; set; }
public string en3 = "c";
public En2 en4 = En2.b;
public En3 en5 = En3.a;
public En2 en6 = En2.c;
public En1? en7 = En1.c;
public En1? en8 = En1.c;
public En2? en9 = null;

public B()
{
en2 = En1.c;
}
}
[Test]
public void EnumTests1()
{
Mapper.CreateMapper<B, A>();

var a = Mapper.Map<B,A>(new B());
Assert.IsTrue(a.en1 == En1.c);
Assert.IsTrue(a.en2 == En2.c);
Assert.IsTrue(a.en3 == En3.c);
Assert.IsTrue(a.en4 == 2);
Assert.IsTrue(a.en6 == En1.c);
Assert.IsTrue(a.en7 == En3.c);
Assert.IsTrue(a.en8 == En3.c);
Assert.IsNull(a.en9);
}
}


3. Nullable -> Primitive,

            Assert.AreEqual(true, Mapper.Map<bool?, bool>(true));
Assert.AreEqual(false, Mapper.Map<bool?, bool>(false));
Assert.AreEqual(false, Mapper.Map<bool?, bool>(null));


4. Primitive -> Nullable

            Assert.AreEqual(true, Mapper.Map<bool, bool?>(true));
Assert.AreEqual(false, Mapper.Map<bool, bool?>(false));


5. Nullable -> Nullable

            Assert.IsFalse(Mapper.Map<sbyte?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Char?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Byte?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Int16?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<UInt16?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Int32?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<UInt32?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Int64?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<UInt64?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Single?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Double?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Decimal?, Boolean?>(null).HasValue);
Assert.IsFalse(Mapper.Map<DateTime?, Boolean?>(null).HasValue);

Assert.IsFalse(Mapper.Map<Boolean?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<sbyte?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Byte?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Int16?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<UInt16?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Int32?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<UInt32?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Int64?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<UInt64?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Single?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Double?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<Decimal?, Char?>(null).HasValue);
Assert.IsFalse(Mapper.Map<DateTime?, Char?>(null).HasValue);

。。。

6.  Null -> Primitive

            object d = null;
object t = Mapper.Map(d, null, typeof(Int32));
Assert.IsInstanceOf<int>(t);


未完待续。

posted @ 2011-04-08 12:50 风云 阅读(...) 评论(...) 编辑 收藏