-
Notifications
You must be signed in to change notification settings - Fork 334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Derived to Derived type mapping #96
Comments
Doing the following would work, however I still think that there should be a way to configure how derived types should be mapped. Let me know if this is just 'out of scope'. Thanks public Contracts.Models.FakeRestaurant.Choice ChoiceChooser(Contracts.Models.FakeOrder.Choice from)
{
if(from == null)
{
return null;
}
switch(from.GetType().Name)
{
case "Choice":
return new Contracts.Models.FakeRestaurant.Choice();
case "ComplexChoice":
return new Contracts.Models.FakeRestaurant.ComplexChoice();
default:
throw new ArgumentException(nameof(from) + " is not of a valid type");
}
}
[TestMethod]
public void ValidateMapsterWithChildren()
{
TypeAdapterConfig<Contracts.Models.FakeOrder.Choice, Contracts.Models.FakeRestaurant.Choice>.NewConfig()
.ConstructUsing(src => ChoiceChooser(src));
... |
Hi, this is interesting feature, however we already capture in #52 and it still open, sorry for this. It is nice that you can find workaround. This will be my to do list (or any PR is welcome). Thank you, |
In my case, I'm doing code generation to bridge two code bases, so I just have to do some trickery to auto-add ConstructUsing. However, if I wasn't, manually generating 100 methods to manage custom construction would be problematic (yeah... it's a large code base) I can perhaps look into a PR, might be easier to do then the code generation :) |
Turns out that with the current code base, it really looks like everything is tied to a generic which will auto-downgrade to the actual instead of the dynamic derived type. In the case of something simple like TestRequest I will have to auto-generate the following methods using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Testing.ExpectedResults
{
public class RegisterEcpTesting2EcpInternalTesting
{
public InternalTesting.TestRequest RegisterFromTestRequest(Testing.TestRequest from)
{
if(from == null)
{
return null;
}
switch(from.GetType().Name)
{
case "TestRequest":
return new InternalTesting.TestRequest();
case "TestRequestChild":
return new InternalTesting.TestRequestChild();
case "TestRequestGrandChild":
return new InternalTesting.TestRequestGrandChild();
default:
return new InternalTesting.TestRequest();
}
}
public Testing.TestRequest RegisterToTestRequest(InternalTesting.TestRequest from)
{
if (from == null)
{
return null;
}
switch (from.GetType().Name)
{
case "TestRequest":
return new Testing.TestRequest();
case "TestRequestChild":
return new Testing.TestRequestChild();
case "TestRequestGrandChild":
return new Testing.TestRequestGrandChild();
default:
return new Testing.TestRequest();
}
}
public InternalTesting.TestRequestChild RegisterFromTestRequestChild(Testing.TestRequestChild from)
{
if (from == null)
{
return null;
}
switch (from.GetType().Name)
{
case "TestRequestChild":
return new InternalTesting.TestRequestChild();
case "TestRequestGrandChild":
return new InternalTesting.TestRequestGrandChild();
default:
return new InternalTesting.TestRequestChild();
}
}
public Testing.TestRequestChild RegisterToTestRequestChild(InternalTesting.TestRequestChild from)
{
if (from == null)
{
return null;
}
switch (from.GetType().Name)
{
case "TestRequestChild":
return new Testing.TestRequestChild();
case "TestRequestGrandChild":
return new Testing.TestRequestGrandChild();
default:
return new Testing.TestRequestChild();
}
}
public void Register()
{
TypeAdapterConfig<Testing.TestRequest, InternalTesting.TestRequest>.NewConfig()
.ConstructUsing(src => RegisterFromTestRequest(src));
TypeAdapterConfig<InternalTesting.TestRequest, Testing.TestRequest>.NewConfig()
.ConstructUsing(src => RegisterToTestRequest(src));
TypeAdapterConfig<Testing.TestRequestChild, InternalTesting.TestRequestChild>.NewConfig()
.ConstructUsing(src => RegisterFromTestRequestChild(src));
TypeAdapterConfig<InternalTesting.TestRequestChild, Testing.TestRequestChild>.NewConfig()
.ConstructUsing(src => RegisterToTestRequestChild(src));
}
}
} |
Hi, I added |
I'm mapping classes from one namespace to another, however it looks like that mapster will mapped a derived class to the baseclass instead of mapping it to a derived class. For example, I have a class named Choice and ComplexChoice in both namespaces, if I pass a ComplexChoice as a property to a class, mapping it should also result in a ComplexChoice, the following code can be used to test this
If you could help, it would be greatly appreciated.
The text was updated successfully, but these errors were encountered: