DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. A Tiny Portable Inversion of Control Container for Multiple Windows Platforms

A Tiny Portable Inversion of Control Container for Multiple Windows Platforms

Jeremy Likness user avatar by
Jeremy Likness
·
Oct. 06, 12 · Interview
Like (0)
Save
Tweet
Share
3.31K Views

Join the DZone community and get the full member experience.

Join For Free

as part of the cross-platform example in my book , i built a tiny ioc container mainly to avoid having to reference and explain mef, unity, or any of the other choices. this is not a full-fledged container with tons of services like injection. it is simply an easy way to create things that depend on other things and manage their lifetime.

the "portable" part is evident in the targets. you can reference the exact same dll from any windows phone, .net framework 4.x, windows store (windows 8), or silverlight 4+ project to use the container. that's it - completely portable, no recompiling necessary, and you now get to keep the same api on your view models when you're wiring dependencies (yes, i'm looking at providing a simple portable mvvm implementation too, but a portable sterling will probably be my next focus since i get so many requests for it on windows 8).

if you are dubious about what is possible across platforms, download the chapter 9 source code from windows8applications.codeplex.com (it's free, regardless of whether or not you get the book) and look at the wintellog example. this is a full blown rss feed reader implemented for both wpf and windows store (windows 8) that uses most of the same code (even part of the networking stack). it includes a very basic version of the container that i used in the book.

the container is less than 200 lines of code. many of the lines are there for design-by-contract checks and some thread-safety mechanisms to ensure unregistering and resolving from separate threads doesn't clobber the container.

it has a few features:

  • uses a simple lambda expression to describe how to create implementations for types
  • passes itself to the instance delegates so they can recursively resolve other references
  • provides labels so you can segregate different definitions (or consider everything under a "label" as a different "container"
  • allows you to unregister a definition (and register it as something different, though i can't imagine why you'd do that)
  • allows you receive a shared or a non-shared instance (lifetime management
  • allows you to destroy the shared instance and generate a new one
  • provides a tryresolve to test and resolve in one operation

the use of it is fairly straightforward and can be inferred from the tests:

[testinitialize]
public void testinitialize()
{
    _target = new portableioc();
}

register and resolve:

[testmethod]
public void giventyperegisteredwhenrequestedthenshouldreturninstance()
{
    var expected = new simplebar();
    _target.register<ibar>(ioc => expected);
    var actual = _target.resolve<ibar>();
    assert.aresame(expected, actual, "test failed: same instance was not returned.");
}

test that you can resolve it:

[testmethod]
public void giventyperegisteredwhencanresolvecalledthenshouldreturntrue()
{
    _target.register<ibar>(ioc => new simplebar());
    assert.istrue(_target.canresolve<ibar>(),
                    "test failed: can resolve should return true when the type is registered.");
}

try to resolve it:

[testmethod]
public void giventypeisnotregisteredwhentryresolvecalledthenshouldreturnfalse()
{
    ibar barinstance;
    var result = _target.tryresolve(out barinstance);
    assert.isnull(barinstance, "test failed: bar instance should be null when type is not registered");
    assert.isfalse(result, "test failed: result should be false when type is not registered.");
}

generate a non-shared instance:

[testmethod]
public void giventypeisregisteredwhennewinstanceisrequestedthenshouldreturnnewinstance()
{
    _target.register<ibar>(ioc => new simplebar());
    var actual1 = _target.resolve<ibar>();
    var actual2 = _target.resolve<ibar>(true);
    assert.arenotsame(actual1, actual2, "test failed: create new should not return the same shared instance.");
}

use constructor injection:

target.register<ibar>(ioc => new simplebar());            
_target.register<ifoo>(ioc => new simplefoo(ioc.resolve<ibar>()));

or property injection:

target.register<ibar>(ioc => new simplebar());            
_target.register<ifoo>(ioc => new simplefoo { bar = ioc.resolve<ibar>() });

that's about it - you can grab the source and/or binary (sorry, no nuget package yet) over at portableioc.codeplex.com . now let's get some work done on sterling ...

jeremy likness

(c) 2011-2012 jeremy likness.
Container Inversion of control

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Internal Components of Apache ZooKeeper and Their Importance
  • DevOps Roadmap for 2022
  • Debugging Streams and Collections
  • Building Angular Library and Publishing in npmjs Registry

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: