2015-02-06 117 views
2

我有一个类:Dapper需要一个无参数构造函数?

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 { } 
     } 
    } 

的metaDictionary由Autofac注入。

我正在用Dapper执行查询,并尝试实现NListingsData。这是我在用的:

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; 

我得到的错误

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 

难道我的类,我用短小精悍的兑现必须始终参?

现在,我可以创建一个简单的DataModel,然后映射到我的ViewModel。但是,这是做到这一点的唯一方法吗?

+3

错误消息似乎很明确。不,你不需要*无参数的构造函数。你需要一个无参数的构造函数或一个匹配给定的签名。 – Servy 2015-02-06 21:03:04

+0

我的课程实际上有大约40个公共财产,并且可能会增长。有40个字段的构造函数?否则我不会发布。 – jaxxbo 2015-02-07 06:16:15

+4

Dapper旨在将数据库查询结果映射到简单的POCO,而不是具有依赖关系的全功能域模型。你可以为Dapper创建一个DTO,并使用它来填充'NListingsData'模型。 – 2015-02-07 16:15:41

回答

-2

只需添加一个额外的私人无参数构造函数。这将被Dapper选中。

相关问题