Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is mapping an object PK breaking ExpressMapper?

Trying to implement a generic repository based on an article by Chris Pratt that uses object as the Id (PK).

All is good, until I tried to .Map()one of these objects with ExpressMapper.

More code below. But essentially, when I try and do this:

var dataModel = postedModel.Map(new ExampleDataModel());

It gives me:

No parameterless constructor defined for this object.

When I change the Id property of ExampleDataModel to a string or int it works fine.

I've googled around. I don't see anything obvious, but I am a bit out of my depth. I'd like to know:

  • What is causing this?

  • Can I work round it somehow?

I'd like to continue using the object PK and ExpressMapper if possible because they're both suiting my project just fine.


public interface IDataModel
{
    object Id { get; }  
}

public interface IDataModel<PKT> : IDataModel
{
    new PKT Id { get; set; }
}

public abstract class DataModel<PKT> : IDataModel<PKT>
{
    public PKT Id { get; set; }
    object IDataModel.Id
    {
        get { return this.Id; }
    }
}



public class ExampleDataModel : 
    DataModel<string>, 
    IDataModel<string>
{
     public virtual string SomeProperty{ get; set; }

    // etc.
}
like image 240
Martin Hansen Lennox Avatar asked May 24 '17 18:05

Martin Hansen Lennox


1 Answers

when you are doing new ExampleDataModel(), You are trying to initalize an instance of ExampleDataModel with parameterless constructor, but looking at your class, you don't have one, that's why you are getting

No parameterless constructor defined for this object.

You should add one parameterless constructor, which looks like this

ExampleDataModel()
{
//init object 
}

though I am not sure how you want to init your object so I am not sure how your constructor should be.

like image 142
Munzer Avatar answered Oct 12 '22 15:10

Munzer