English Deutsch

XML configuration

The dependencices, arguments and more can be declared via XML, that gives you the advance to replace the configuration without recompilation.

A disadvance is the missing strong typing, therefore write-, or runtime-failts can be happen.

Syntax

With the XML-Syntax, all kinds of registrations are possible to use, except of delegates / lambda expressions and closed generic types. So, mostly everything that you can express with code, you can also express with XML.

LightCore can read XML out of a standard .NET-configurationfile (app.config or web.config).
For this, it`s needed to add the LightCore sectionhandler in the configurationfile.
It`s important, that you name the section as LightCoreConfiguration.

        
     <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="LightCoreConfiguration" type="LightCore.Configuration.XamlConfigSectionHandler, LightCore.Configuration" />
    </configSections>
        
    

A example configuration:

        
            <LightCoreConfiguration xmlns="clr-namespace:LightCore.Configuration;assembly=LightCore.Configuration">
                <LightCoreConfiguration.TypeAliases>
                    <TypeAlias Alias="HttpRequest" Type="LightCore.Integration.Web.Lifecycle.HttpRequestLifecycle, LightCore.Integration.Web" />
                </LightCoreConfiguration.TypeAliases>
                <LightCoreConfiguration.Registrations>
                    <Registration ContractType="LightCore.TestTypes.IRepository{2}, LightCore.TestTypes" ImplementationType="LightCore.TestTypes.Repository{2}, LightCore.TestTypes" />
                    <Registration ContractType="LightCore.TestTypes.IFoo" ImplementationType="LightCore.TestTypes.Foo">
                        <Registration.Arguments>
                            <Argument Value="Alexander" />
                            <Argument Value="44" Type="int" Name="AgeProperty"/>
                            <Argument Value="true" Type="bool" />
                        </Registration.Arguments>
                    </Registration>
                </LightCoreConfiguration.Registrations>
                <LightCoreConfiguration.RegistrationGroups>
                    <RegistrationGroup Name="Real">
                        <RegistrationGroup.Registrations>
                            <Registration ContractType="System.ComponentModel.IDisposable" ImplementationType="System.IO.Stream, mscorlib"/>
                        </RegistrationGroup.Registrations>
                    </RegistrationGroup>
                    <RegistrationGroup Name="Fake">
                        <RegistrationGroup.Registrations>
                            <Registration ContractType="System.ComponentModel.IDisposable" ImplementationType="MyDisposableType, MyProject"/>
                        </RegistrationGroup.Registrations>
                    </RegistrationGroup>
                </LightCoreConfiguration.RegistrationGroups>
            </LightCoreConfiguration>
        
    

Open generic types

You can register open generic types using the common .NET Syntax:
"LightCore.TestTypes.Repository`2, LightCore.TestTypes"

or using the LightCore variant of syntax::
"LightCore.TestTypes.Repository{2}, LightCore.TestTypes"

The number in the registration represents how many typparameters the type contains, e.g. bspw. Repository<Foo, int>.

Aliase

A concept withing the XML-configuration makes it possible to declare aliase for a type. That can be very helpful by often used types such as new lifecycles for LightCore.

The aliases below are out-of-the-box registered and can be used immediately:

Type Aliases

Is used for arguments of a registration.

Alias Typ
Int32, Integer, int System.Int32, mscorlib
String, string System.String, mscorlib
Boolean, bool System.Boolean, mscorlib
Guid System.Guid, mscorlib

Lifecycle Type Aliases

Is used to declare the default lifecycle (see top) or a lifecycle for a registration.
(LightCore.Integration.Web.dll contains the HttpRequestLifecycle and have to be manually added to the aliases, if needed.

Types in the XML-file have to be declared throught an alias or fullqualified.
For example: "Name.Space.Class, Assembly" => "LightCore.Lifecycle.SingletonLifecycle, LightCore".

Alias Typ
Transient LightCore.Lifecycle.TransientLifecycle, LightCore
Singleton LightCore.Lifecycle.SingletonLifecycle, LightCore
ThreadSingleton LightCore.Lifecycle.ThreadSingletonLifecycle, LightCore

LightCoreConfiguration Attributes

Attribute name Description Valid values
ActiveRegistrationGroups Optional attribute, which is used to declare the RegistrationGroups to use (the active and registered one). Groupname which should be active, one group or a few, delimited by comma: "XML" or "XML, Utils".
DefaultLifecycle Optional attribute, which declares the default-lifecycle for a registration. Alias or full qualified type for a lifecycle (implements ILifecycle interface).

TypeAlias Attributes

Attribute name Description Valid values
Alias Alias name to be used. A string.
Type Type to which the alias points.. A full qualified declaration, e.g.: "LightCore.Lifecycle.SingletonLifecycle, LightCore".

Registration Attributes

A registration can hold arguments and a registrationgroup registrations and again arguments.

Attribute name Description Valid values
ContractType Alias for a type or a full qualified type declaration, which is the contract. A full qualified decleration, e.g.: "TestProject.IFoo, TestProject".
ImplementationType Alias for a type or a full qualified type declaration, which is implementation. A full qualified decleration, e.g.: "TestProject.IFoo, TestProject".
Lifecycle Optionales Attribut über das der Standard-Lebenszyklus für die Registrierungen steuert. Alias or a full qualified type decleration for a lifecycle (implements ILifecycle interface).
Enabled Optional attribute which means the registration is disabled on false and enabled on true. (default is: true). true or false.

RegistrationGroup Attributes

Attribute name Description Valid values
Name A name for the group. A string.
Enabled Optional attribute which means the registration is disabled on false and enabled on true. (default is: true). true or false.

Configure LighTCore for XML

LightCore can read registrations with RegistrationModule implementations (see: Extensibility). You can choose between the following variants:


  • with the app.config / web.config.
                    
                        var builder = new ContainerBuilder();
                        RegistrationModule xamlModule = new XamlRegistrationModule();
                                            
                        builder.RegisterModule(xamlModule);
                    
                
  • with a filename that points to a file contains the XML.
                    
                        var builder = new ContainerBuilder();
                        string configurationFilePath = "LightCore-Configuration.xml";
                        RegistrationModule xamlModule = new XamlRegistrationModule(configurationFilePath);
                                            
                        builder.RegisterModule(xamlModule);
                    
                
  • with an stream instance, which contains the XML (e.g. from a database).
                    
                        var builder = new ContainerBuilder();
                        Stream configurationStream = GetStreamFromDatabase();
                        RegistrationModule xamlModule = new XamlRegistrationModule(configurationStream);
                                            
                        builder.RegisterModule(xamlModule);
                    
                

The different way`s to register types are combinable.