2015-02-06 162 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選中。

相關問題