Content
Introduction
You will learn the basics of LightCore with this introduction.
For the advanced features, please choose on the right menu bar.
Know the framework
Only one reference to LightCore.dll is needed.
Aside this, there are a few optional components:
- LightCore.dll
- Core component, not optional
- LightCore.Configuration.dll
- Optional, only needed if LightCore should be configured with xml
- LightCore.Integration.Web.dll
- Optional integration for ASP.NET and ASP.NET MVC
- LightCore.CommonServiceLocator.dll
- Optional adapter for LightCore to use with the CSL (Common Service Locator)
Additional files within the deployment of LightCore
- LightCore.xsd
- Should be added to your solution or project. That gives you Intellisense for the LightCore Configuration in Visual Studio
- SampleConfiguration.xml
- Examples for a LightCore Konfiguration, with .NET app- / web.config and without the .NETconfiguration api
Simple example
You can add registrations on the application startup with the ContainerBuilder class. If you call the Build() method on the ContainerBuilder, it will return an configured container.
It is usual to hold the container once per application in the application scope (static):
For simple usage, you will register types and use the container to get instances.
See this simple example:
var builder = new ContainerBuilder();
builder.Register<IFoo, Foo>();
IContainer container = builder.Build();
IFoo foo = container.Resolve<IFoo>();
You may use two generic type arguments, within the Register() method, the first represents the contract, and the second the implementation. After that, you can get an instance with by one Resolve() call.
Registering Dependencies
As we’ve seen, the Register() method can be called with two generic type arguments.
But there is another way to register dependencies. You can pass two Type instances to theRegister() method. They will be used, if the
Type typeOfContract = typeof(IFoo);
Type typeOfImplementation = typeof(Foo);
builder.Register(typeOfContract, typeOfImplementation);
Another option is the use of delegates / lambda expressions for registerations. These are fast and flexible on resolution.
With the argument (in the code below c), it`s possible to take advance of the container to resolve inner dependencies.
This way, you can build a huge object hierarchy only with delegates.
builder.Register<IFoo>(c => new Foo());
builder.Register<IFoo>(c => new Foo(c.Resolve<IBar>()));
builder.Register<IFoo>(c => FooFactory.GetFoo());
Its also possible, to register dependencies over xml. See: here.
If this is not enough, you can write your own implementation of a RegistrationModule.
Such a implementation will contain registration-logic.