Setup getting started

Alex Maris edited this page Jan 26, 2016 · 5 revisions

Setup / Getting Started

In addition to more "traditional" registration/setup, TinyIoC also provides a default singleton implementation and "AutoRegister" for automatic discovery/registration of types.

	// TinyIoC provides a lazyily constructed singleton
	// version of itself if you want to use it.
	var container = TinyIoCContainer.Current;

	// By default we can resolve concrete types without
	// registration
	var instance = container.Resolve<MyConcreteType>();

	// We can automatically register all concrete types, abstract base classes
	// and interfaces with a single call.
	container.AutoRegister();
	var implementation = container.Resolve<IMyInterface>();

	// By default we silently ignore duplicate implementations,
	// but we can choose to throw a meaningful exception that contains
	// the interface/base type and all the potential implementations:
	container.AutoRegister(DuplicateImplementationActions.Fail);

	// We can also provide an "autoregistration predicate" to tweak
	// which types will / won't be registered:
	container.AutoRegister(t => t != typeof(IMyExcludedInterface));

AutoRegister can also be combined with "normal" registrations so autoregister can handle the majority of registrations but you can "override" specific registrations afterwards.

For more information on the more traditional registration interface see Registration / Lifetimes.