There's nothing built in and lots of people have created their own solutions. Including, but not limited to,: Funq, MicroIoc, TinyIoc, this or this. But none of them work the way I want to.
I want:
- Automatic constructor parameter injection
- Explicit indication of where a dependency exists
- Explicit configuration of how dependencies should be resolved
- Fluent configuration
I don't want:
- Parameter injection
- Automatic resolving of dependencies
- An aribtrary ServiceLocator
I don't care about:
- Letting the container manage singletons
- Nested/chained dependency resolution
There are two factors which influence my requirements:
1. My past experience with Castle Windsor - Chosen after evaluating what was available at the time (about 4 years or so ago.)
2. My experience with working in development teams where there would always be at least one person who wasn't interested in learning anything new, best practices or, seemingly, code quality. I've learnt that if you're working with such people, or anyone new to the industry, that you'll save yourself a lot of work (in fixing up their errors, mistakes and repeatedly explaining things to them) if everything is explicit and clear and not "magic" because if they can't understand it they can't update it or fix it if there's a problem.
So I've written something myself: http://github.com/mrlacey/NanoIoC
As pointed out in the comments, this doesn't provide automatic constructor injection. Unfortunately the platform just doesn't support a way of doing that. This is my next best thing. - Hope that makes it clearer.
Yes, the name is a tounge-in-cheek reference to MicroIoc & TinyIoC but hints that mine has much less code. (It's less than 90 LOC.)
Yes, I know it's more of a DI framework than an IOC one but the terms are used fairly interchangable out in the real world so I'm happy with this.
How to use it:
For the class that has some external dependencies, we declare this by marking them up with an interface "ISupportInjectionOf<T>". As an example, if we wanted to indicate a page had a dependency up an "IRepository" and an "IDateTimeHelper" we'd do this:
public partial class MainPage : PhoneApplicationPage, ISupportInjectionOf<IRepository>, ISupportInjectionOf<IDateTimeHelper> {
Then within our constructor we'd resolve these dependencies:
private IRepository repository; private IDateTimeHelper dateTime; public MainPage() { InitializeComponent(); repository = this.GetDependency<IRepository>(); dateTime = this.GetDependency<IDateTimeHelper>(); }
Yes, we could resolve the dependencies at any time, but in my code it'll always be in the constructors.
This is important for maintaining testability and maintainability. (I'm establishing a convention.)
In the above example I'd also include a constructor overload for directly injecting the dependencies during testing:
#if TEST public MainPage(IRepository repo, IDateTimeHelper dateTimeHelper) { InitializeComponent(); this.repository = repo; this.dateTime = dateTimeHelper; } #endif
Simple!
Configuration:
Configuration is simple and done at app level. We can declare how each dependency should be resolved separately:
NanoIocConfig.RegisterDependency<IRepository>(new ViewModelRepository()); NanoIocConfig.RegisterDependency<IDateTimeHelper>(new DateTimeHelper());
Or via a fluent interface:
NanoIocConfig.RegisterDependency<IRepository>(new ViewModelRepository()) .And<IDateTimeHelper>(new DateTimeHelper());
Obviously these examples all use interfaces. But we don't have to. Assuming that we didn't want to hide "DateTimeHelper" behind an interface, we can just do this (note the type is inferred):
NanoIocConfig.RegisterDependency(new DateTimeHelper()); dth = this.GetDependency<DateTimeHelper>();
The above examples are all creating an instance to use for every time the dependency is resolved.
Instead, we could pass an instance of a singleton in the traditional way:
NanoIocConfig.RegisterDependency<IRepository>(ViewModelRepository.Instance);
If you want different instances each time a dependency is resolved simply pass a factory and get the new instances that way.
What do you think?
Useful?
Interesting?
Something you may consider using?
Want it bundled into a NuGet package? (Either as a library or the single source file)
I'd love to know what you think.
* It would be awesome if in a (prefereably near) future version of the Windows Phone SDK, it included tooling to allow it to be easier to implement good development practices in our Windows Phone code. It's awesome that they've made it easy for people to get started with developing for the platform but those beginners need, in time, to know how to write better code and if the tools stop them there's no incentive for them to learn. And for those of us who consider ourselves professionals and do this for a living, we want to be able to apply best practices to our code (work) and not have the SDK and the tooling get in the way and stop us doing basic things.
You said you wanted automatic constructor parameter injection but I don't see that in NanoIoC.
ReplyDelete@Kristof yes, that's what I want but the platform doesn't provide a way of allowing it. This is my next best thing.
ReplyDeleteSorry should have been clearer.
Have you seen Caliburn.Micro's SimpleContainer and PhoneContainer?
ReplyDeletehttp://nuget.org/packages/Caliburn.Micro.Container
//Eddie Garmon
@Eddie Yes I'm aware of Caliburn Micro but it doesn't match exactly what I'm after.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletepolo ralph lauren outlet
ReplyDeletejordan 23
cheap jordan shoes
abercrombie outlet
coach outlet
michael kors outlet
lv handbags
cheap oakley sunglasses
chanel outlet
cheap jordans
north face backpacks
kors handbags outlet
goose outlet
uggs
ugg outlet
cheap nfl jerseys from china
authentic louis vuitton handbags
cheap louis vuitton handbags
tory burch outlet
the north face outlet
red bottom heels
hermes outlet
michael kors outlet online
abercrombie & fitch
true religion jeans
ralph lauren uk
louis vuitton handbags
north face jackets
dolce gabbana
lv handbags
Jacket Leather Indonesia
ReplyDeleteCivet Coffee Real Indonesia
CD
marinir
bantal-silikon15.blogspot.com
bumbu-pecel01.blogspot.com/
seo-guru01.blogspot.com/
seo-konsultasi-mudah.blogspot.com/l
marinir-seo-konsultan-12a.blogspot.com/
http://dfes12.blogspot.com/
agen-seo19.blogspot.com/
agen-seo20.blogspot.com/
agen-seo16.blogspot.com/
agen-seo18.blogspot.com/
jksbindo.blogspot.com/
jkt-49.blogspot.com/
seokonsultan1.blogspot.com
guru-seo-malang.blogspot.com/
seo-aaa.blogspot.com/
marinir-seo-8.blogspot.com/2015/05/rty.html
vvtiin.blogspot.com/2015/05/rqw.html
agen-seo17.blogspot.com/2015/05/rtq.html
asdf-14.blogspot.com/2015/05/qwr.html
bantaljkt1.blogspot.com/2015/05/qwa.html
larismanisyaluk.blogspot.com/2015/05/q.html
bantalku1.blogspot.com/2015/05/b.html
bantal999.blogspot.com/2015/05/r.html
distrobantal.blogspot.com/
bantalsilikon01.blogspot.com/
sabunherbalya.blogspot.com/
kesethandukmalang.blogspot.com/
bantalmalangmurah.blogspot.com/
restoran-seo.blogspot.com/
distro-seo.blogspot.com/
jualsangkarpleci.blogspot.com/
marinir-seo.blogspot.com/
bumbu-pecel-malang.blogspot.com/
new york giants jerseys
ReplyDeletecoach outlet store
oakley sunglasses outlet
five fingers shoes
oakley outlet
nike trainers
giuseppe zanotti
ugg boots
philadelphia eagles jerseys
pandora outlet
fitflop
celine outlet
coach outlet online
nike free 5.0
kate spade
boston celtics jersey
manchester united jersey
abercrombie and fitch
hollister clothing
kate spade handbags
ray ban sunglasses
nfl jerseys wholesale
air max 2014
oakley sunglasses wholesale
toms shoes outlet online
mbt shoes
cheap soccer jerseys
lacoste polo shirts
lacoste polo shirts
m a c cosmetics
hollister uk
ralph lauren
valentino shoes
adidas shoes
real madrid jersey
ray ban sunglasses
cheap jordans
michael kors outlet online
pandora jewelry
cheap ray ban sunglasses
0722maoqiuyun
oakley sunglasses cheap, http://www.oakley.in.net/
ReplyDeleteray ban sunglasses sale, http://www.raybansunglass.us.com/
true religion jeans, http://www.truereligionjeanscanada.com/
snow boots outlet, http://www.wintercoats.us.com/
coach outlet store, http://www.coachoutletstores.com.co/
stuart weitzman sale, http://www.stuartweitzmanoutlet.us/
giuseppe zanotti sale, http://www.giuseppe-zanotti-outlet.org/
ugg outlet, http://www.uggoutletstore.eu.com/
asics, http://www.asicsisrael.com/
parajumpers sale, http://www.woolrichoutletstore.us.com/
los angeles clippers, http://www.clippersjerseystore.com/
tiffany jewelry, http://www.tiffany-outlet.us.com/
calvin klein,calvin klein outlet online,calvin klein wholesale,calvin klein shoes,calvin klein underwear,calvin klein coats,calvin klein handbags
cheap ugg boots, http://www.cheapuggboots.net.co/
michael kors uk, http://www.michaelkorsoutlet.org.uk/
ray ban sunglasses,ray ban sunglasses outlet,ray ban,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,rayban sunglasses,cheap ray ban
arizona cardinals, http://www.arizonacardinalsjersey.us/
manchester united jersey, http://www.manchesterunitedonline.com/
kkk10 13
jianbin1208
ReplyDeletemichael kors uk outlet
fred perry shirts
lebron james shoes,lebron 12,lebron 12 shoes,lebron sneakers,lebron iv,lebron 11,lebron 10
nike air max 2014
timberland outlet
adidas jeremy scott
adidas outlet store
canada goose jackets
nike air huarache,nike huarache,nike huarache sneakers,nike huarache shoes
uggs outlet
north face jackets
football shirts uk,soccer jerseys uk,cheap soccer jerseys uk
coach outlet
winter coats outlet
ray ban sale
chanel handbags
michael kors usa
louis vuitton outlet online
lebron 12
michael kors outlet online
uggs outlet
ray ban sunglasses outlet,ray ban sunglasses,ray ban,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,rayban sunglasses,cheap ray ban
canada goose jackets
cheap mlb jerseys
uggs outlet
cheap ugg boots
winter jackets outlet
los angeles clippers
dallas cowboys