Skip to content
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

Feature - Allow specifying the constructor to use for activition #3

Closed
michael-wolfenden opened this issue Feb 20, 2018 · 2 comments
Closed

Comments

@michael-wolfenden
Copy link

Remute assumes that the type has a single constructor. however we often have types with multiple constructors (usually one was private and only used for serialization).

It would be nice to have a way to specify the specific constructor to use if there are multiple by providing a Func<IEnumerable<ConstructorInfo>, ConstructorInfo> or via some other mechanism.

PS - Thanks for the library, it's been super useful to us

@ababik
Copy link
Owner

ababik commented Feb 23, 2018

@michael-wolfenden thank you for your inquiry. Your feedback is appreciated.

Now you can configure Remute to work with type with multiple constructors. You can do that providing ActivationConfiguration object to Remute constructor.

Here is an example.

    public class User
    {
        public int Id { get; }
        public string FirstName { get; }
        public string LastName { get; }

        public User(int id, string firstName, string lastName)
        {
            Id = id;
            FirstName = firstName;
            LastName = lastName;
        }

        public User(string firstNameNotMatchingPropertyName, string lastName)
        {
            FirstName = firstNameNotMatchingPropertyName;
            LastName = lastName;
        }
    }
var user = new User(1, "Joe", "Doe");
var constructor = user.GetType().GetConstructor(new[] { typeof(int), typeof(string), typeof(string) });

var config = new ActivationConfiguration().Configure(constructor);
var remute = new Remute(config);

Also using ActivationConfiguration you can solve the problem when constructor parameter and property names are not matching.

var user = new User(1, "Joe", "Doe");
var type = user.GetType();
var constructor = type.GetConstructor(new[] { typeof(string), typeof(string) });

var firstNameParameter = constructor.GetParameters().Single(x => x.Name == "firstNameNotMatchingPropertyName");
var firstNameProperty = type.GetProperty("FirstName");

var config = new ActivationConfiguration()
    .Configure(constructor, new Dictionary<ParameterInfo, PropertyInfo>() { [firstNameParameter] = firstNameProperty });

var remute = new Remute(config);

So Configure method is to specify which constructor to use and how to map object properties to constructor parameters.

Last but not least, you can avoid reflection using the overloaded method. I think that would be the most preferable method because it's refactoring friendlier.

var config = new ActivationConfiguration()
    .Configure<User>(x => new User(x.FirstName, x.LastName))
    .Configure<Foo>(x => ...)
    .Configure<Bar>(x => ...);

var remute = new Remute(config);

This expression is not actually executed. This is to point which constructor to use and how to map parameters.

Please let me know if you have any questions.

Repository owner deleted a comment from ababik-frozenmountain Feb 23, 2018
@ababik ababik closed this as completed Feb 24, 2018
@kofifus
Copy link

kofifus commented Feb 8, 2019

Remute can also search for a matching constructors from all those available ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants