和Keyle一起学StrangeIoc – Binding

StrangeIoC

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.

 

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

posted @ 2015-02-26 23:48  keyle_xiao  阅读(2190)  评论(0编辑  收藏  举报