Generic tears don’t mean you’re cryin’ over nuthin’

If you’ve been keeping up, you’ll know the main focus of feature development in Strange these past few weeks has been around pooling. Yesterday my pooling implementation hit a serious snag so now you get to hear me sing the blues.

Image

My thinking around the implementation of pooling has shifted around a lot as I’ve worked it over. Remember that it needs to function as an internal mechanism for Strange, but also as a useable tool for other developers (i.e., you). My latest iteration felt really simple, which I usually take to be a sign that I’m on the right track. It looks (from outside) like this:

//A pool is:
Pool<TypeOfInstancePooled>
//Bound in the Context like this:
injectionBinder.Bind<IPool<ISomeInterface>>().To<Pool<ImplementingClass>>();
//And Injected like this:
[Inject]
public IPool<ISomeInterface> myPool { get; set; }

Notwithstanding all the brackets, this is really nice to me: I’ve declared my dependency but kept it as broad as possible: something that implements IPool; it generically handles instances which implement ISomeInterface. The concrete dependencies are left (as they usually are in Strange) to the binding Context.

But there’s a flaw in the plan. Old hands at Unity/C# (well, older than mine, anyway) will see it I expect. This elegant flow relies on something called covariance. Covariance is a high-falutin’ way to describe the property of broadly-defined types (such as interfaces and abstract classes) to be assignable from narrower types (the classes that implement/extend the broad types. This, of course, is how Strange does its most important job, dependency injection, by allowing you to declare a dependency on ISomeInterface and fulfill that dependency with SomeImplementer.

Well, boys and girls, it turns out that this simple idea is a great big pile of fail because, you see, covariance wasn’t implemented for generics in C# until 4.0. And as you probably know, Unity uses something that ain’t quite 4.0, so no-covariance-in-generics-go-to-bed-without-any-supper-for-you.

I am Le Sad.

My current plan (not wanting to make the Perfect the enemy of the Good) is to carry on as I am, with the obvious, frustrating limitation that all Pool bindings will need to be concrete. While this kinda blows, I think the larger value of pooling outweighs the sin of a handful of concrete dependencies.

What do you think? Is there a better way to rethink the problem? Can you salve my conscience with something fine and smoky, perhaps from Islay? Constructive input welcome!

Advertisements

1 thought on “Generic tears don’t mean you’re cryin’ over nuthin’

  1. Pingback: Pools at last… | StrangeIoC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s