Tasklet configuration (2)

Posted by: laurentl in taskletbest practices on Print PDF

Last time I explained how to make a tasklet generic using simple configuration with the appSettings and the labels. This time I'll explain how to create a more "complex" configuration.

 When you have to configure you tasklet based on more than just "flat" parameters, you need to use custom configuration. This allows you to have collections and "trees".

 

For example, a tasklet that needs to send an email to multiple destinations and each of these destinations have an email address and the name of the destination.

To do this we need to have a collection of destinations and for each of the destination, an email address and a name.


<configuration>

<
destinations>

<add name="Laurent Lopez">

<emails>

<add type="home" email="laurent@private.net"/>

<add type="work" email="laurent@createitn.net"/>

</emails>

</add>

...

</destinations>

</configuration>

 

Now to be able to access the configuration from the tasklet code you need to create a class that inherits from Microsoft.Dynamics.Mobile.Framework.ConfigurationSection.

You also need a class that is the collection of destinations and finally you need the definition of the destination.

 

class DestinationSection : ConfigurationSection

{

    // Properties

    [ConfigurationProperty("destinations", IsRequired = true)]   

    public DestinationElementCollection Destinations 

    {

        get

        {

            return (DestinationElementCollection)this["destinations"];

        }

    }

}

class DestinationElementCollection: ConfigurationElementCollection

{

    protected override ConfigurationElement CreateNewElement()

    {

        return new DestinationElement();

    }

    protected override object GetElementKey(ConfigurationElement element)

    {

        return ((DestinationElement)element).Name;

    }

    public DestinationElement this[string destinationName]

    {

        get

        {

            return (DestinationElement)base.BaseGet(destinationName);

        }

    }

}

class DestinationElement : ConfigurationElement

{

    [ConfigurationProperty("name", IsRequired = true)]

    public string Name

    {

        get

        {

            return (string)this["name"];

        }

    }

    [ConfigurationProperty("email", IsRequired = true)]

    public string Email

    {

        get

        {

            return (string)this["email"];

        }

    }

}

 Finally to access it from your tasklet code (you can't call this in the tasklet constructor as the Configuration property is not populated yet):

 

DestinationSection destinationSection = Configuration.GetSection<DestinationSection>("configuration");

foreach (DestinationElement destination in destinationSection.Destinations)

{

   ...

}

 

It is also possible to define another ConfigurationElementCollection inside a ConfigurationElement. Like that a destination could contain multiple elements:

<configuration>

    <destinations>

        <
add name="Laurent Lopez">

            <emails>

                <
add type="home" email="laurent@private.net"/>

                <add type="work" email="laurent@createitn.net"/> 

           </
emails>

        </add>

    </
destinations>

</configuration>

 

 For that, you need a new ConfigurationElementCollection for the emails element and a ConfigurationElement to define the email and type:

class EmailElementCollection : ConfigurationElementCollection

{

   
protected override ConfigurationElement CreateNewElement()

    {

        return new EmailElement();

    }

    protected override object GetElementKey(ConfigurationElement element)

    {

        return ((EmailElement)element).Type;

    }

}

class EmailElement : ConfigurationElement

{

    [ConfigurationProperty("type", IsRequired = true)]

    public string Type

    {

        get

        {

            return (string)this["type"];

        }

    }

    [ConfigurationProperty("email", IsRequired = true)]

    public string Email

    {

        get

        {

            return (string)this["email"];

        }

    }

}

 

In the DestinationElement we need to add the emails collection:

class DestinationElement : ConfigurationElement

{

    [ConfigurationProperty("name", IsRequired = true)]

    public string Name

    {

        get

        {

            return (string)this["name"];

        }

    }

    [
ConfigurationProperty("emails", IsRequired = true)]    public EmailElementCollection Emails

    {

        get { return (EmailElementCollection)this["emails"]; }

    }

}

 

And here is the code to access the new properties:

DestinationSection destinationSection = Configuration.GetSection<DestinationSection>("configuration");foreach (DestinationElement destination in destinationSection.Destinations)

{

    Console.WriteLine(destination.Name);

    foreach (EmailElement email in destination.Emails)

    {

        Console.WriteLine(email.Email + " / " + email.Type);

    }

}

Of course  you can still add your labels and appSettings to the configuration.

Comments (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy