Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dapper needs a parameterless constructor?

I have a class:

public class NListingsData : ListingData, IListingData
    {
        private readonly IMetaDictionaryRepository _metaDictionary;

        //Constructor.
        public NListingsData(IMetaDictionaryRepository metaDictionary)
        {
            _metaDictionary = metaDictionary;
        }

        //Overridden function from abstract base class
        public override List<Images> Photos
        {
            get
            {
                var urlFormat = _metaDictionary.GetDictionary(CommonConstants.ImagesUrlFormat, this.Key);
                var imgs = new List<Images>();

                for (var i = 0; i < PhotosCount; i++)
                {
                    imgs.Add(new Images
                    {
                        Url = string.Format(urlFormat, this.MNumber, i)

                    });
                }
                return imgs;
            }
            set { }
        }
    }

The metaDictionary is injected by Autofac.

I am executing a query with Dapper and I try to materialize NListingsData. This is what I am using:

string sqlQuery = GetQuery(predicates); //Select count(*) from LView; select * from lView;
//Use multiple queries
            using (var multi = _db.QueryMultipleAsync(sqlQuery,
              new
              {
                  //The parameter names below have to be same as column names and same as the fields names in the function: GetListingsSqlFilterCriteria()
                  @BedroomsTotal = predicates.GetBedrooms(),
                  @BathroomsTotalInteger = predicates.GetBathrooms()
              }).Result)
            {
                //Get count of results that match the query
                totalResultsCount = multi.ReadAsync<int>().Result.Single();
                //Retrieve only the pagesize number of results

                    var dblistings = multi.ReadAsync<NListingsData>().Result; // Error is here
            }
            return dblistings;

I get the error

A parameterless default constructor or one matching signature (System.Guid ListingId, System.String MLSNumber, System.Int32 BedroomsTotal, System.Double BathroomsTotalInteger) is required for CTP.ApiModels.NListingsData materialization

Does my class that I use to materialize with dapper must always be parameterless?

Now, I could create a simple DataModel then map to my ViewModel. But is that the only way to do it?

like image 385
jaxxbo Avatar asked Oct 19 '25 04:10

jaxxbo


1 Answers

Just add an additional private, parameterless, constructor. This will get picked by Dapper.

like image 127
Siewers Avatar answered Oct 20 '25 18:10

Siewers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!