与调用定制的精致小巧的构造? [英] Call custom constructor with Dapper?

查看:135
本文介绍了与调用定制的精致小巧的构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用短小精悍的ASP.NET SQL成员资格提供表的接口。我裹着的SqlMembershipProvider类,并增加了额外的方法来让我给与我有一些自定义表的某些标准MembershipUsers。



在查询与短小精悍,它的数据看起来精致小巧首先实例化一个带参数的构造函数的类,然后映射返回列到对象的属性。



不过,UserName属性上在类的MembershipUser有没有setter。在小巧玲珑的SqlMapper.cs从大约1417线来看,该方法 GetSettableProps()只得到可设置的属性。



我试图做一个Multimap之查询,调用构造函数,但是,问题在于传递到查询的对象是已经丢失的用户名。



我猜我可以修改 GetSettableProps()方法,但我不知道这会的工作,或者如果它会影响我现有的代码。



反正我调用了的MembershipUser类具有自定义构造函数?



还是有合理的变化,我可以作出小巧精致,支持我的情况?



** 更新 **



马克的回答使用非通用/动态查询()方法是正确的,但对于后人,这是方法我指的是里面小巧玲珑的:

 静态列表< PropInfo> GetSettableProps(T型)
{
返回牛逼
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic可| BindingFlags.Instance)
。选择(P =>新建PropInfo $ B ?$ b {
名称= p.Name,
二传手= p.DeclaringType ==牛逼p.GetSetMethod(真):p.DeclaringType.GetProperty(p.Name).GetSetMethod(真),
键入= p.PropertyType
})
。凡(资讯=>!info.Setter = NULL)
.ToList();
}


解决方案

我会用非这里的通用查询API ...

 返回connection.Query(SQL,参数)。选择(行=> 
新ANYTYPE((串)row.Foo,(INT)row.Bar){
=其他(浮动)row.Other})了ToList()。



使用这个,你可以同时使用非默认的构造函数和属性分配,没有任何的变化,通过使用动态作为中间步骤。


I'm trying to use Dapper to interface with the ASP.NET SQL Membership Provider tables. I wrapped the SqlMembershipProvider class and added an additional method to get me the MembershipUsers given a certain criteria relating to some custom tables I have.

When querying the data with Dapper, it appears that Dapper first instantiates the class with a parameter-less constructor, and then "maps" the returned columns into the properties on the object.

However, the UserName property on the MembershipUser class has a no setter. Judging from around line 1417 in the Dapper SqlMapper.cs, the method GetSettableProps() only gets settable properties.

I tried to do a MultiMap query to invoke the constructor, but the problem with that is the objects passed into the query are already missing the UserName.

I'm guessing I could modify the GetSettableProps() method, but I'm not sure if that will work, or if it will affect my existing code.

Is there anyway for me to invoke the custom constructor that the MembershipUser class has?

Or is there a reasonable change that I could make to Dapper to support my situation?

** UPDATE **

Marc's answer to use the non-generic/dynamic Query() method was correct, but for posterity, this is the method I was referring to inside Dapper:

static List<PropInfo> GetSettableProps(Type t)
{
    return t
          .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
          .Select(p => new PropInfo
          {
              Name = p.Name,
              Setter = p.DeclaringType == t ? p.GetSetMethod(true) : p.DeclaringType.GetProperty(p.Name).GetSetMethod(true),
              Type = p.PropertyType
          })
          .Where(info => info.Setter != null)
          .ToList();  
}

解决方案

I would use the non-generic Query API here...

 return connection.Query(sql, args).Select(row =>
     new AnyType((string)row.Foo, (int)row.Bar) {
         Other = (float)row.Other }).ToList();

Using this you can use both non-default constructors and property assignments, without any changes, by using "dynamic" as an intermediate step.

这篇关于与调用定制的精致小巧的构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆