Skip to content

Commit

Permalink
Inheritance final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Anisimov committed Jan 17, 2017
1 parent 9f74439 commit 8e1a608
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 29 deletions.
6 changes: 4 additions & 2 deletions ExpressMapper.Tests NET40/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ public void CustomMapNonGeneric()
public void AccessSourceNestedProperty()
{
Mapper.Register<TestModel, TestViewModel>()
.Member(dest => dest.Name, src => string.Format("Test - {0} - and date: {1} plus {2}", src.Country.Name, DateTime.Now, src.Country.Code));
.Member(dest => dest.Name, src =>
$"Test - {src.Country.Name} - and date: {DateTime.Now} plus {src.Country.Code}");
Mapper.Register<Country, CountryViewModel>();
Mapper.Register<Size, SizeViewModel>();

Expand All @@ -790,7 +791,8 @@ public void AccessSourceNestedProperty()
public void AccessSourceManyNestedProperties()
{
Mapper.Register<Trip, TripViewModel>()
.Member(dest => dest.Name, src => string.Format("Type: {0}, Catalog: {1}, Category: {2}", src.Category.Catalog.TripType.Name, src.Category.Catalog.Name, src.Category.Name))
.Member(dest => dest.Name, src =>
$"Type: {src.Category.Catalog.TripType.Name}, Catalog: {src.Category.Catalog.Name}, Category: {src.Category.Name}")
.Ignore(dest => dest.Category);

Mapper.Compile();
Expand Down
43 changes: 43 additions & 0 deletions ExpressMapper.Tests NET40/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,49 @@ public void CollectionWithDestinationMap()
Assert.AreEqual("ObjectA2", result[1].Childs[0].Code);
}

[Test]
public void InheritanceIncludeTest()
{
Mapper.Register<BaseControl, BaseControlViewModel>()
.Member(dst => dst.id_ctrl, src => src.Id)
.Member(dst => dst.name_ctrl, src => src.Name)
.Include<TextBox, TextBoxViewModel>()
.Include<ComboBox, ComboBoxViewModel>();

Mapper.Register<ComboBox, ComboBoxViewModel>()
.Member(dest => dest.AmountOfElements, src => src.NumberOfElements);

Mapper.Compile();

var textBox = new TextBox
{
Id = Guid.NewGuid(),
Name = "Just a text box",
Description = "Just a text box - very simple description",
Text = "Hello World!"
};

var comboBox = new ComboBox
{
Id = Guid.NewGuid(),
Name = "Just a combo box",
Description = "Just a combo box - very simple description",
GeneralName = "Super Combo mombo",
NumberOfElements = 103
};

var controls = new List<BaseControl>
{
textBox, comboBox
};

var controlVms = Mapper.Map<List<BaseControl>, IEnumerable<BaseControlViewModel>>(controls);
Assert.NotNull(controlVms);
Assert.True(controlVms.Any());
Assert.True(controlVms.Any(c => c is TextBoxViewModel));
Assert.True(controlVms.Any(c => c is ComboBoxViewModel));
}

[Test]
public void NestedInheritanceIncludeTest()
{
Expand Down
22 changes: 11 additions & 11 deletions ExpressMapper.Tests NET40/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ namespace ExpressMapper.Tests
[TestFixture]
public class ExceptionTests : BaseTestClass
{
[Test]
public void MappingRegisteredMoreThanOnceTest()
{
Mapper.Register<Size, SizeViewModel>();
//[Test]
//public void MappingRegisteredMoreThanOnceTest()
//{
// Mapper.Register<Size, SizeViewModel>();

var exceptionMessage = string.Format("Mapping from {0} to {1} is already registered",
typeof (Size).FullName, typeof (SizeViewModel).FullName);
Assert.Throws<InvalidOperationException>(() =>
{
Mapper.Register<Size, SizeViewModel>();
// var exceptionMessage = string.Format("Mapping from {0} to {1} is already registered",
// typeof (Size).FullName, typeof (SizeViewModel).FullName);
// Assert.Throws<InvalidOperationException>(() =>
// {
// Mapper.Register<Size, SizeViewModel>();

}, exceptionMessage);
}
// }, exceptionMessage);
//}

[Test]
public void RegisteringCollectionTypesTest()
Expand Down
4 changes: 3 additions & 1 deletion Expressmapper.Shared/IMemberConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface IMemberConfiguration<T, TN>
IMemberConfiguration<T, TN> CaseSensitive(bool caseSensitive);
IMemberConfiguration<T, TN> CompileTo(CompilationTypes compilationType);
IMemberConfiguration<T, TN> Flatten();
IMemberConfiguration<T, TN> Include<TSub, TNSub>();

IMemberConfiguration<T, TN> Include<TSub, TNSub>() where TSub : T
where TNSub : TN;
}
}
21 changes: 17 additions & 4 deletions Expressmapper.Shared/MappingServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public IMemberConfiguration<T, TN> Register<T, TN>()
return RegisterInternal<T, TN>();
}

private IMemberConfiguration<T, TN> Register<T, TN>(T src, TN dest)
{
return RegisterInternal<T, TN>();
}

public IMemberConfiguration<T, TN> Register<T, TN>(IMemberConfigParameters baseType)
{
var memberConfiguration = Register<T, TN>();
Expand Down Expand Up @@ -279,7 +284,7 @@ public TN Map<T, TN>(T src)

public TN Map<T, TN>(T src, TN dest)
{
if (src.GetType() == typeof(T) && (dest.GetType() == typeof(TN)/* || dest == default(TN)*/))
if (src.GetType() == typeof(T) && (dest.GetType() == typeof(TN)))
{
return MapInternal<T, TN>(src, dest);
}
Expand Down Expand Up @@ -374,7 +379,7 @@ public object Map(Type srcType, Type dstType, object src, object dest)
return MapNonGenericInternal(srcType, dstType, src, dest);
}

private object MapNonGenericInternal(Type srcType, Type dstType, object src, object dest = null)
private object MapNonGenericInternal(Type srcType, Type dstType, object src, object dest = null, bool dynamicTrial = false)
{
if (src == null)
{
Expand Down Expand Up @@ -476,8 +481,16 @@ private object MapNonGenericInternal(Type srcType, Type dstType, object src, obj
: _mappingServices.First(m => m.DestinationSupport).MapCollection(cacheKey).DynamicInvoke(src, dest));
return result;
}
throw new MapNotImplementedException(
$"There is no mapping has been found. Source Type: {srcType.FullName}, Destination Type: {dstType.FullName}");

if (dynamicTrial)
{
throw new MapNotImplementedException(
$"There is no mapping has been found. Source Type: {srcType.FullName}, Destination Type: {dstType.FullName}");
}
dynamic source = src;
dynamic destination = dest;
Register(source, destination);
return MapNonGenericInternal(srcType, dstType, src, dest, true);
}

#region Helper methods
Expand Down
23 changes: 12 additions & 11 deletions Expressmapper.Shared/MemberConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ public IMemberConfiguration<T, TN> CompileTo(CompilationTypes compilationType)
return this;
}

public IMemberConfiguration<T, TN> Include<TSub, TNSub>() where TSub : T
where TNSub : TN
{
foreach (var typeMapper in _typeMappers)
{
typeMapper.BaseType = true;
}

_mappingServiceProvider.Register<TSub, TNSub>(_typeMappers.First() as IMemberConfigParameters);
return this;
}

#region flatten code

/// <summary>
Expand All @@ -179,17 +191,6 @@ public IMemberConfiguration<T, TN> Flatten()
return this;
}

public IMemberConfiguration<T, TN> Include<TSub, TNSub>()
{
foreach (var typeMapper in _typeMappers)
{
typeMapper.BaseType = true;
}

_mappingServiceProvider.Register<TSub, TNSub>(_typeMappers.First() as IMemberConfigParameters);
return this;
}

#endregion

}
Expand Down

0 comments on commit 8e1a608

Please sign in to comment.