Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Jan 19, 2020
2 parents 6f88aa6 + c03bbe7 commit 6cf5e15
Show file tree
Hide file tree
Showing 12 changed files with 717 additions and 27 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ using (MyDbContext context = new MyDbContext())
- Map to constructor

### Why Mapster?
#### Performance
Don't let other libraries slow you down, Mapster performance is at least 2.5 times faster!
And 3.x times faster with [FastExpressionCompiler](https://github.com/MapsterMapper/Mapster/wiki/FastExpressionCompiler) and [Mapster CodeGen](https://github.com/MapsterMapper/Mapster/wiki/CodeGen)!

![image](https://user-images.githubusercontent.com/5763993/46615061-8cc78980-cb41-11e8-8bea-b04d9fcabccd.png)
#### Performance & Memory efficient
Mapster was designed to be efficient on both speed and memory. You could gain 5x faster while using only 1/3 of memory.
And you could gain 13x faster with [Mapster CodeGen](https://github.com/MapsterMapper/Mapster/wiki/CodeGen)!

| Method | Mean | StdDev | Error | Gen 0 | Gen 1 | Gen 2 | Allocated |
|-------------------------- |---------------:|-------------:|-------------:|------------:|------:|------:|-----------:|
| 'Mapster 4.1.1' | 1,388,744.4 us | 3,987.51 us | 6,028.54 us | 312000.0000 | - | - | 1251.22 MB |
| 'Mapster 4.1.1 (Codegen)' | 505,727.6 us | 2,525.21 us | 3,817.75 us | 312000.0000 | - | - | 1251.22 MB |
| 'ExpressMapper 1.9.1' | 3,122,200.4 us | 11,701.40 us | 19,663.45 us | 604000.0000 | - | - | 2418.52 MB |
| 'AutoMapper 9.0.0' | 6,883,546.7 us | 28,159.65 us | 42,573.37 us | 911000.0000 | - | - | 3646.85 MB |

#### Step into debugging

Expand Down
131 changes: 131 additions & 0 deletions src/Mapster.Tests/DynamicTypeGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using Mapster.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;

namespace Mapster.Tests
{
[TestClass]
public class DynamicTypeGeneratorTests
{
private interface INotVisibleInterface
{
int Id { get; set; }
string Name { get; set; }
}

public interface ISimpleInterface
{
int Id { get; set; }
string Name { get; set; }
}

public interface IInheritedInterface : ISimpleInterface
{
int Value { get; set; }
}

public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}

public interface IComplexBaseInterface
{
int Id { get; set; }
Foo Foo { get; set; }
void BaseMethod();
}

public interface IComplexInterface : IComplexBaseInterface
{
string Name { get; set; }
int? NullableInt { get; set; }
int ReadOnlyProp { get; }
int WriteOnlyProp { set; }
IEnumerable<int> Ints { get; set; }
IEnumerable<Foo> Foos { get; set; }
int[] IntArray { get; set; }
Foo[] FooArray { get; set; }
void SimpleMethod();
int ComplexMethod(byte b, ref int i, out string s);
}

[TestMethod]
public void ThrowExceptionWhenInterfaceIsNotVisible()
{
void action() => DynamicTypeGenerator.GetTypeForInterface(typeof(INotVisibleInterface));

var ex = Should.Throw<InvalidOperationException>(action);
ex.Message.ShouldContain("not accessible");
}

[TestMethod]
public void CreateTypeForSimpleInterface()
{
Type iClass = DynamicTypeGenerator.GetTypeForInterface(typeof(ISimpleInterface));

ISimpleInterface instance = (ISimpleInterface)Activator.CreateInstance(iClass);

instance.ShouldNotBeNull();

instance.Id = 42;
instance.Name = "Lorem ipsum";

instance.Id.ShouldBe(42);
instance.Name.ShouldBe("Lorem ipsum");
}

[TestMethod]
public void CreateTypeForInheritedInterface()
{
Type iClass = DynamicTypeGenerator.GetTypeForInterface(typeof(IInheritedInterface));

IInheritedInterface instance = (IInheritedInterface)Activator.CreateInstance(iClass);

instance.ShouldNotBeNull();

instance.Id = 42;
instance.Name = "Lorem ipsum";
instance.Value = 24;

instance.Id.ShouldBe(42);
instance.Name.ShouldBe("Lorem ipsum");
instance.Value.ShouldBe(24);
}

[TestMethod]
public void CreateTypeForComplexInterface()
{
Type iClass = DynamicTypeGenerator.GetTypeForInterface(typeof(IComplexInterface));

IComplexInterface instance = (IComplexInterface)Activator.CreateInstance(iClass);

instance.ShouldNotBeNull();

instance.Id = 42;
instance.Foo = new Foo();
instance.NullableInt = 123;
instance.Name = "Lorem ipsum";
instance.WriteOnlyProp = 24;
instance.Ints = new List<int>();
instance.IntArray = new int[2];
instance.Foos = new List<Foo>();
instance.FooArray = new Foo[2];

instance.Id.ShouldBe(42);
instance.Foo.ShouldNotBeNull();
instance.NullableInt.ShouldBe(123);
instance.Name.ShouldBe("Lorem ipsum");
instance.ReadOnlyProp.ShouldBe(0);

int i = 0;
string s = null;
Should.Throw<NotImplementedException>(() => instance.BaseMethod(), "Call BaseMethod.");
Should.Throw<NotImplementedException>(() => instance.SimpleMethod(), "Call SimpleMethod.");
Should.Throw<NotImplementedException>(() => instance.ComplexMethod(123, ref i, out s), "Call ComplexMethod.");
}
}
}
Loading

0 comments on commit 6cf5e15

Please sign in to comment.