和Keyle一起学StrangeIoc – Binding

Strange: the IoC framework for Unity

Binding

StrangeIoc的核心是一个非常简单的绑定包,它可以创建间接绑定,减轻代码对程序其他部分的依赖。

The core of Strange is a very simple package for binding. This means, essentially, that we can bind (connect) one or more of anything to one or more of anything else. Tie an interface to a class that implements that interface. Or tie an event to a handler. Or tie two classes such that when one comes into being, the other one is automatically created. Why would we do this? So glad you asked! It turns out that much of what we do when programming comes down to binding. If you’ve ever dispatched an event (or a SendMessage in Unity), if you’ve ever indicated that one class uses another class, if you’ve ever written so much as an “if...else” statement, you’ve engaged in some form of binding. That is, you’ve tied something to something else.

But binding things directly is problematic, because it results in code that’s hard to change (rigid) and easy to break (brittle). For example, I’m sure you’ve programmed something in which you’ve expressed the idea “this is a Thing. And a Thing contains these other SubThings.” For example, a Spaceship class, which contains both a Gun and a keyboard control. You write this — and all’s well-and-good, until your boss says he wants mouse control instead of keyboard. So now you go back and re-write the Spaceship class. But wait a second. Your Spaceship class didn’t really change any. It’s the control that changed. So why are you re-writing Spaceship?

Instead of writing the controls right into the Spaceship class, you could create a MouseControl Class and use that. But if Spaceship includes a reference to the MouseControl class, you’restill directly binding. In order to change from KeyboardControl to MouseControl (and back again, when your boss changes his mind), you have to change the reference inside Spaceship.

Strange’s binders make it possible to create indirect bindings that relieve your code’s reliance on other parts of the program. This is a fundamental (but often misunderstood) tenet of Object-Oriented Programming. Your code isn’t really Object-Oriented until the objects themselves can function without reliance on other concrete classes. Using binders can give your code lots more freedom and flexibility.

The structure of a binding

绑定的结构,Strangeioc有两个必须的模块和一个可选的模块,这个必须的模块是一个Key,和一个Value,用这个Key触发这个Value,于是将事件作为Key触发回调函数,

Let’s look quickly at the structure of a single binding. This structure is repeated throughout Strange and all its extensions, so you’ll want to understand the pattern.

A Strange binding is made up of two required parts and one optional part. The required parts are a key and a value. The key triggers the value; thus an event can be the key that triggers a callback. Or the instantiation of one class can be the key that leads to the instantiation of another class. The optional part is a name. Under some circumstances, it is useful to qualify two bindings with identical keys. Under these circumstances, the name serves as a discriminator.

几种不同的绑定方式 既可以用泛型,类型,也提供了基本数据类型的支持

All three of these parts can be structured in one of two ways, either as a value or as a type using C# generics. Using generics, for example we might say:

Bind<Spaceship>().To<Liberator>();

The “Bind” is the key, the “To” is the value. We might express the binding as a value:

Bind(“MeaningOfLife”).To(42);

A binder fed with the input “MeaningOfLife” would react with the output 42.

There are times when these two styles get mixed:

Bind<Spaceship>().To(“Enterprise”);

When this binder is fed with the Type Spaceship, it outputs the string value “Enterprise”.

When naming is called for, the binding looks much the same:

Bind<IComputer>().To<SuperComputer>().ToName(“DeepThought”);Finally, note that the following things are all the same:

Bind<IDrive>().To<WarpDrive>();

Bind(typeof(IDrive)).To(typeof(WarpDrive));

IBinding binding = Bind<IDrive>();
binding.To<WarpDrive>();

作者也说差异化只是语法糖 所以先看看整体接口层 再细看实现

The differences are nothing more than syntactical sugar.

There are countless forms of binding, and Strange gives you access to a few really useful ones. What’s more, the binding framework is so simple that you can extend it yourself to create new binder components. We go into each of the included binders in the following section.

前一段时间本屌处在过年状态 完全找不着北了,今天开始正式回复满血工作状态 ,另外如果本屌文章被你读到我深感荣幸,有一点我想说明的是不要觉得我翻译的东西少,我基本上是全文通读只是写出非翻译不可的精华部分,其余的部分你要自己读原文也可其实也没多大意义。

时间: 2024-08-07 03:06:31

和Keyle一起学StrangeIoc – Binding的相关文章

和Keyle一起学StrangeIoc &ndash; Introduction

Strange: the IoC framework for Unity Strange attractors create predictable patterns, often in chaotic systems. 在混乱的系统中创造出一个可以预测的模式 . Introduction StrangeIoc主要用于C#与Unity3d,我们已经在IOS,Web,Android项目中成功运用,他包含了如下功能,其中大部分功能都是可选的 Strange is a super-lightweigh

和Keyle一起学StrangeIoc &ndash; Extensions

Strange: the IoC framework for Unity Extensions You may have heard that Strange is a Dependency Injection framework. I'm a little uncomfortable with that description. Sure, Strange offers DI and it's a great use, but the core of the framework - as I'

和Keyle一起学StrangeIoc &ndash; MVCSContext

MVCSContext: the big picture 本篇基本上就是介绍Strange框架的基本方法使用与部署Unity3d项目,另外所有框架的思路都是一致的,让项目变得易于维护.现在让我们一起开始吧. This section is basically a recipe for building a Strange app with MVCSContext. In the last section I described all the parts; in this one I'll ex

和Keyle一起学StrangeIoc &ndash; 教程目录

推荐你先快速阅读下项目概览Overview 翻译地址 http://www.cnblogs.com/Keyle/p/4289314.html 1. Binding  对应翻译 http://www.cnblogs.com/Keyle/p/4302442.html The structure of a binding 2. Extensions  对应翻译 http://www.cnblogs.com/Keyle/p/4304580.html The injection extension Ins

和Keyle一起学ShaderForge &ndash; Custom Blinn-Phong

用了两天时间精心准备了这篇教程,快来和Keyle一起学ShaderForge,玩起来~ 本章目录 1.什么是Blinn-Phong光照模型 2.如何使用自定义光照模型 2.1 ShaderForge内置光照模型 3.如何使用自定义类似光照实现Blinn-Phong的光照模型 4.完善我们的自定义Shader 4.1 我们先模拟出一个Diffuse的算法        4.2合并(通过加法),得到我们想要的结果        4.3 优化 去除重复声明的变量 如同Normal Direction 

和Keyle一起学StrangeIoc- 视频教程

StrangeIoC: The Good, The Bad and the Strange (Part 1 of 2)https://www.youtube.com/watch?v=4ebReOBH15Q StrangeIoC: The Good, The Bad and the Strange (Part 2 of 2)https://www.youtube.com/watch?v=k4Y7WgoYkD8&feature=iv&src_vid=4ebReOBH15Q&annota

和Keyle一起学ShaderForge &ndash; Create Base Shader

1.本篇让我们一起动手试试使用ShaderForge创建一个基本的Shader 2.介绍Shader文件Main函数中公开的节点 1.使用ShaderForge创建一个基本的Shader 效果如下左1为 node_1311 Color效果为纯白下的 ,左2为 node_1311 Color效果为红色RGB(255,0,0) 生成代码如下,在Properties 属性中增加了_keyleTexture 贴图,_TextureNormal 法线贴图 ,_node_1311 Color ,在两个对应的

Swift学习手札

? Keyle以前学过1个礼拜的OC,但是OC基础还是无限趋近于零,原因是OC的语法实在是sang高xin大bing上kuang,现在恰好工作之余还有那么一些零散的时间可以看看Swift便在此记下一些学习心得,缓慢更新中 -? ? 收获第一个报错 第一个HelloWorld就遇到问题了,我对一个变量进行如下声明结果报错了...报错了... String name = "keyle" println(name) 直接定义基本类型会报错,但是定义为常量或者var类型则不会 let name

什么时候该选用Xamarin?

现在开发app也好,桌面程序也好,有很多不同的技术可以选择.那么,什么时候应该选用Xamarin呢? 5 questions to consider before choosing Xamarin 1 是否需要跨平台? 根据公司情况.团队大小.如果每个平台有足够的开发人员,那么就不需要跨平台了.如果一共也就那么一两个人,需要开发安卓.iOS app,那么跨平台可能是一个比较好的选择.如果还要开发uwp程序,Xamarin就又有优势了. 如果不需要跨平台,就不必选用Xamarin了. 2 是否同时