小巧玲珑的示例代码的澄清 [英] Clarification of Dapper Example Code

查看:143
本文介绍了小巧玲珑的示例代码的澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图神交小巧玲珑,似乎失去了一些东西很基本的,可有人解释从谷歌代码短小精悍主页采取下列代码,并解释为什么没有FROM子句,第二个参数的查询方法(动态)传递一个匿名类型,我猜这是某种建立一个命令对象,但想在凡人术语的解释。



感谢您,
斯蒂芬

 公共类犬{
公众诠释?年龄{搞定;组; }
公众的Guid标识{搞定;组; }
公共字符串名称{;组; }
公众持股量?重量{搞定;组; }
公众诠释IgnoredProperty {
{返回1; }
}
}

VAR GUID = Guid.NewGuid();
变种狗= connection.Query<狗和GT(选择年龄= @Age,ID = @Id,新时代{=(int)的空,ID = GUID}?);

dog.Count()IsEqualTo(1);
dog.First()Age.IsNull();
dog.First()Id.IsEqualTo(GUID)。


解决方案

前两个例子只是没有做任何真正的数据访问,大概是为了让他们简单。结果
是的,有一个用于连接( connection.Query(...)) ,但只是因为这是调用短小精悍的方法(因为它们扩展IDbConnection接口)的唯一途径。



这样的事情是完全有效的SQL代码:

 选择'富',1 

......它只是生成的动态结果,实际上并没有选择从表中任何事情。



与参数和匿名的例子类型:



VAR狗= connection.Query<狗>(选择年龄= @Age,ID = @Id,新的{年龄= (INT?)空,ID = GUID});



...只是表明精致小巧的能力的在一个匿名类型的形式再次提交的SQL参数。结果
,查询实际上不会从表中选择任何内容,大概是为了保持它的简单。


I'm trying to grok Dapper and seem to be missing something very fundamental, can someone explain the following code taken from the Dapper home page on Google code and explain why there is no From clause, and the second param to the Query method (dynamic) is passed an anonymous type, I gather this is somehow setting up a command object, but would like an explanation in mere mortal terminology.

Thank you, Stephen

public class Dog {    
    public int? Age { get; set; }    
    public Guid Id { get; set; }    
    public string Name { get; set; }    
    public float? Weight { get; set; }    
    public int IgnoredProperty {
        get { return 1; }
    }
}

var guid = Guid.NewGuid();
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });            

dog.Count().IsEqualTo(1);
dog.First().Age.IsNull();
dog.First().Id.IsEqualTo(guid);

解决方案

The first two examples just don't do any "real" data access, probably in order to keep them simple.
Yes, there is a connection used (connection.Query(...)), but only because that's the only way to call Dapper's methods (because they extend the IDbConnection interface).

Something like this is perfectly valid SQL code:

select 'foo', 1

...it just does "generate" its result on the fly, without actually selecting anything from a table.

The example with the parameters and the anonymous type:

var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });)

...just shows Dapper's ability to submit SQL parameters in the form of an anonymous type.
Again, the query does not actually select anything from a table, probably in order to keep it simple.

这篇关于小巧玲珑的示例代码的澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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