Closed phoenix172 closed 5 years ago
Great idea, I like it. I will try to implement it.
Here is my first attempt of implementing refactoring that generate clone method. Would you mind to test it and give me some feedback about the results? Version with this experimental feature is available here
@phoenix172 are you still interested in this feature?
Yeah, sorry been away for a while. Gonna test it when I get to work.
Ok, I tested the new update. It seems good with a few notes:
Something like this:
The name of the method need not be constrained to clone. It was just an example.
Something like this:
But instead of generating "return parent;", it would generate code for cloning the object. I don't think anyone would need a refactoring that generates a method that just returns its parameter. The above suggestion is far more useful, and I think it would be what the user would expect to happen.
I hope the above feedback helps. Sorry for the late reply and cheers for the feature. :) 👍
Great feedback, thanks a lot!!! I will try to implement your suggestions.
@phoenix172
According to pt 3 How the complex properties should be mapped: rewriting property by property even if the object implement ICloneable
interface
public User Clone(User u)
{
return new User()
{
FirstName = u.FirstName,
LastName = u.LastName,
Age = u.Age,
ComplexProperty = new ComplexObject{
Prop1 = u.ComplexProperty.Prop1,
Prop2 = u.ComplexProperty.Prop2
}
};
}
or call Clone()
method if it's accessible?
public User Clone(User u)
{
return new User()
{
FirstName = u.FirstName,
LastName = u.LastName,
Age = u.Age,
ComplexProperty = u.ComplexProperty.Clone(),
};
}
I think utilizing the Clone method if ICloneable is implemented sounds reasonable. After all you are cloning the object and the object is offering a way to clone itself. With that said though, something like an "to explicit mapping" refactoring will be nice to have on the Clone method call.
Here's the new version that implements all your suggestions https://ci.appveyor.com/api/buildjobs/q2luj7o5s5jstwsw/artifacts/MappingGenerator%2FMappingGenerator%2FMappingGenerator.Vsix%2Fbin%2FRelease%2FMappingGenerator.vsix
Any feedback is welcome as always ;)
@phoenix172 sorry for bothering you, but I'm not sure you got the notification about last update. I would like to release this feature in few days.
Yeah, sorry for the delay. I tested the feature and everything looks great.
Thanks for information and your time. I'm really appreciate.
It would be nice if there was a straightforward way to create a clone. I know that currently that can be accomplished by creating a mapping that takes and returns the same object, and after that alt-enter -> explicit mapping, but I was wondering if it wouldnt be better to directly generate explicit mapping in the above scenario. Writing a mapping method that takes and returns the same type doesn't really make sense in any other case, unless you want to clone (and maybe modify slightly) the passed object.
P.S: Love the tool. Saves me from writing boring code.
P.P.S: Sorry for overwhelming you with issues :D