第六周NuGet软件包-具有粘土的动态,可延展,令人愉悦的Expando对象

Hey, have you implemented the NuGet Action Plan? Get on it, it'll take only 5 minutes: NuGet Action Plan - Upgrade to 1.2, Setup Automatic Updates, Get NuGet Package Explorer. NuGet 1.3 is out, so make sure you're set to automatically update!

嘿,您是否执行了NuGet行动计划 上车,仅需5分钟: NuGet行动计划-升级到1.2,设置自动更新,获取NuGet软件包资源管理器 NuGet 1.3已发布,因此请确保您设置为自动更新!

The Backstory: I was thinking since the NuGet .NET package management site is starting to fill up that I should start looking for gems (no pun intended) in there. You know, really useful stuff that folks might otherwise not find. I'll look for mostly open source projects, ones I think are really useful. I'll look at how they built their NuGet packages, if there's anything interesting about the way the designed the out of the box experience (and anything they could do to make it better) as well as what the package itself does.

背景:自从NuGet .NET程序包管理站点开始填充以来,我一直在思考,我应该开始在其中查找gem(无双关语)。 您知道的,否则人们可能找不到真正有用的东西。 我会寻找大多数开源项目,这些项目我认为真的很有用。 我将看看他们如何构建他们的NuGet软件包,如果关于开箱即用体验的设计方式(以及他们可以做得更好的任何方式)以及软件包本身的功能有什么有趣的事情。

This weeks Package of the Week is "Clay." It makes working with dynamic objects even more fun. It was written for the open source Orchard Project by Louis DeJardin with an assist from Bertrand LeRoy.

本周的“一周套餐”是“粘土”。 它使处理动态对象变得更加有趣。 它是由Louis DeJardin在Bertrand LeRoy的协助下为开放源代码Or​​chard Project编写的。

静态语言的愉悦动态 (Enjoyable Dynamics in a Static Language)

Here's a little copy/paste from a post two years ago I did on the dynamic keyword in C#. I thought it was good, so I'll include it again here.

这是两年前我在C#中对dynamic关键字所做的文章的一些复制/粘贴。 我认为这很好,所以我将在这里再次包括在内。

So I asked this guy, what's up with the dynamic keyword, and what type was it exactly? I mean, C# isn't dynamic, right? He says:

所以我问这个人, dynamic关键字是什么,它的确切类型是什么? 我的意思是,C#不是动态的,对不对? 他说:

"Oh, well it's statically-typed as a dynamic type."

“哦,它是静态类型的,是动态类型的。”

Then my brain exploded and began to leak out my ears. Honestly, though, it took a second. Here's a good example from some of Anders' slides:

然后我的大脑爆炸了,开始渗出我的耳朵。 老实说,不过,花了一秒钟。 这是一些Anders幻灯片中的一个很好的例子:

Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);

That's the creation of an object, invokation of a method, and the collection of a return value. This is the exact same code, as the "var" type is figured out at compile time.

那就是对象的创建,方法的调用以及返回值的集合。 这是完全相同的代码,因为在编译时可以找到“ var”类型。

var calc = GetCalculator();
int sum = calc.Add(10, 20);

If you wanted to do the exact same thing, except with Reflection (like if it were some other class, maybe old-COM interop, or something where the compiler didn't know a priori that Add() was available, etc) you'd do this:

如果您想做完全相同的事情,除了Reflection(例如,它是其他类,例如old-COM interop,或者编译器不知道先验的Add()是否可用等),您可以d执行以下操作:

object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
BindingFlags.InvokeMethod, null,
new object[] { 10, 20 });
int sum = Convert.ToInt32(res);

It's pretty horrible to look at, of course. If the object is some dynamic thing (from any number of sources), we can do this:

当然,这看起来非常可怕。 如果对象是动态对象(来自任何数量的源),我们可以这样做:

dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);

And get the dynamic method invocation and conversion of the return type. Basically it looks just like we're calling any other object.

并获得返回类型的动态方法调用和转换。 基本上看起来就像我们在调用其他任何对象。

My buddy Rob Conery and I love dynamic languages, but we also love the .NET CLR. If we had our way, there'd be a lot more support for the Dynamic Language Runtime and the Iron.NET languages. We wrote the http://thisdeveloperslife.com website using ASP.NET Web Pages largely because it uses the Razor template engine - which feels very dynamic - and we used dynamics throughout the code.

我的好友Rob Conery和我喜欢动态语言,但我们也喜欢.NET CLR。 如果我们有办法,那么将对动态语言运行时和Iron.NET语言提供更多支持。 我们使用ASP.NET网页编写了http://thisdeveloperslife.com网站,主要是因为它使用了Razor模板引擎(感觉非常动态),并且在整个代码中都使用了动态特性。

Some folks think that static languages have no business dipping their toes into the dynamic pool, but I disagree. Successful compilation is just the first unit test, as they say, and I like the ability to pick and choose between static and dyanamic.

有些人认为静态语言无济于事,但我不同意。 正如他们所说,成功的编译只是第一个单元测试,我喜欢在静态和动态之间进行选择的能力。

Expandos和动态 (Expandos and Dynamic)

In .NET, the Expando object is a dynamic type that lets you add and remove members to it, ahem, dynamically. It's great for dealing with dynamic data. You might do this:

在.NET中,Expando对象是一种动态类型,可让您动态添加和删除成员。 这对于处理动态数据非常有用。 您可以这样做:

dynamic myObject = new ExpandoObject();
myObject.WhateverMakesMeHappy = "Scott";

And boom, I've got a new property. You can even "cast" Expandos as other types and start using them like that type. It's crazy. Play with it.

和繁荣,我有一个新财产。 您甚至可以像其他类型一样“投射” Expandos,然后开始像该类型一样使用它们。 这很疯狂。 玩吧。

Anonymous objects via object initalizers are nice, but once you've made one, it's stuck that way. For example, from Bertrand Le Roy's blog

通过对象初始化器实现的匿名对象很好,但是一旦创建了一个对象,它就会被卡住。 例如,来自Bertrand Le Roy的博客

Html.TextBoxFor(m => m.CurrentUser, new {
title = "User Name",
style = "float:left;"
})

See the object intializer? It makes an anonymous object, but it'll have that shape with title and style, forever.

看到对象初始化器了吗? 它制作了一个匿名对象,但是它将永远带有标题和样式。

为什么需要粘土? (Why is Clay needed?)

In Bertrand's words:

用贝特朗的话来说:

In terms of API usability [ExpandoObject is] not very daring and in particular it does not do much to help you build deep dynamic object graphs. Its behavior is also fixed and can’t be extended.

就API的可用性而言,[ExpandoObject]不太大胆,特别是它并不能帮助您构建深层的动态对象图。 它的行为也是固定的,无法扩展。

Clay on the other hand is highly extensible and focuses on creation and consumption of deep graphs.

另一方面,黏土是高度可扩展的,并且专注于深图的创建和使用。

Clay has a clever naming convention (although you may hate it. Relax, it's a convention) where you name the ClayFactory instance "New." Yes, capital-N "New." *brain explodes again*

Clay有一个聪明的命名约定(尽管您可能讨厌它。放宽,这是一个约定),您将ClayFactory实例命名为“ New”。 是的,大写字母N为“新”。 *大脑再次爆炸*

You can do the usual stuff with Clay that you can also do with Expando, of course. But, you can use several different techniques depending on the situation you're in, and that's where it gets interesting. Here's some examples from Bertrand and Lou, starting with the ClayFactory creation:

当然,您可以使用Clay进行常规操作,也可以使用Expando进行处理。 但是,您可以根据所处的情况使用多种不同的技术,这才是有趣的地方。 这是Bertrand和Lou的一些示例,从ClayFactory创建开始:

dynamic New = new ClayFactory();

Now this “New” object will help us create new Clay objects, as the name implies (although this name is just a convention). Then:

现在,顾名思义,这个“ New”对象将帮助我们创建新的Clay对象(尽管该名称只是一个约定)。 然后:

var person = New.Person();
person.FirstName = "Louis";
person.LastName = "Dejardin";

For instance in Clay, indexer syntax and property accessors are equivalent, just as they are in JavaScript. This is very useful when you are writing code that accesses a property by name without knowing that name at compile-time:

例如,在Clay中,索引器语法和属性访问器是等效的,就像在JavaScript中一样。 当您编写通过名称访问属性而在编译时不知道该名称的代码时,这非常有用:

var person = New.Person();
person["FirstName"] = "Louis";
person["LastName"] = "Dejardin";

You can also use properties as chainable setters, jQuery-style:

您还可以将属性用作jQuery样式的可链接设置器:

var person = New.Person()
    .FirstName("Louis")
    .LastName("Dejardin");

Or you can pass an anonymous object and it will become a Clay object:

或者,您可以传递一个匿名对象,它将成为一个Clay对象:

var person = New.Person(new {
    FirstName = "Louis",
    LastName = "Dejardin"
});

Even better, Clay also understands named arguments, which enables us to write this:

更好的是,Clay还了解命名参数,这使我们可以编写以下代码:

var person = New.Person(
    FirstName: "Louis",
    LastName: "Dejardin"
);

Or even this as an array:

甚至将此作为数组:

var people = New.Array(
New.Person().FirstName("Louis").LastName("Dejardin"),
New.Person().FirstName("Bertrand").LastName("Le Roy")
);

All of this also means that these are all equivalent:

所有这些还意味着这些都是等效的:

person.FirstName
person["FirstName"]
person.FirstName()

To get started, rather than using NuGet to "install-package Clay," I'd recommend you install Clay.Sample. This is a common convention for open source projects to include sample packages that have a dependency on the project itself. Install the sample and you'll get both packages.

首先,建议您安装Clay.Sample,而不是使用NuGet来“安装Clay”。 这是开源项目的通用约定,其中包括依赖于项目本身的示例包。 安装示例,您将获得两个软件包。

Here's some other cool samples that really give you an idea of how you can move like clay between the dynamic and static worlds:

这里还有一些其他很酷的示例,它们确实使您了解如何在动态世界和静态世界之间像粘土一样移动:

public interface IPerson {
string FirstName { get; set; }
string LastName { get; set; }
}

public static void CastToCLRInterface() {
dynamic New = new ClayFactory();

var person = New.Person();
person.FirstName = "Louis";
person.LastName = "Dejardin";

// Concrete interface implementation gets magically created!
IPerson lou = person;

// You get intellisense and compile time check here
Console.WriteLine("{0} {1}", lou.FirstName, lou.LastName);
}

I'd like the see folks in power *cough* Anders *cough* check out things like Clay and make them built in. Yum.

我希望看到当权者*咳嗽*安德斯*咳嗽*看看Clay之类的东西,然后内置它们。

相关链接 (Related Links)

翻译自: https://www.hanselman.com/blog/nuget-package-of-the-week-6-dynamic-malleable-enjoyable-expando-objects-with-clay

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值