T4Config 1.0.5

Configuration class generator for app.config and web.config appsettings and connectionStrings.

There is a newer version of this package available.
See the version list below for details.
Install-Package T4Config -Version 1.0.5
dotnet add package T4Config --version 1.0.5
<PackageReference Include="T4Config" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add T4Config --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: T4Config, 1.0.5"
For F# scripts that support #r syntax, copy this into the source code to reference the package.

T4Config

A down and dirty way to have strongly typed Config Settings and Connection Strings in your projects by using T4.

In a nutshell we create an interface called IConfigurations/ IConnectionStrings, so you can do all of your fancy DI/IoC stuff, and a concrete implementation called Configurations/ ConnectionStrings. (both names totally customizable.

The T4 template loops through the web.config (or app.config) appSettings and generates a read only property for each key and returns the values placed in the settings value.

If you have a configuration section of appSettings which looked like this;

<appSettings>
    <add key="Key1" value="value1" />
    <add key="Key2" value="2212DE83-DFC3-44A8-81BA-0A8D132C1F79" />
    <add key="Key3" value="42" />
</appSettings>

After the T4 file compiles you would end up with this;

    public interface IConfigurations
    {
        string Key1 { get; }

        Guid Key2 { get; }

        int Key3 { get; }

        bool Key4 { get; }

    }

    public class Configurations : IConfigurations
    {
        private static readonly Lazy<string> _key1 = new Lazy<string>(() => GetSetting("Key1"));

        private static readonly Lazy<Guid> _key2 = new Lazy<Guid>(() => new Guid(GetSetting("Key2")));

        private static readonly Lazy<int> _key3 = new Lazy<int>(() => Convert.ToInt32(GetSetting("Key3")));

        private static readonly Lazy<bool> _key4 = new Lazy<bool>(() => Convert.ToBoolean(GetSetting("Key4")));


	public virtual string Key1  { get; } = _key1.Value;
	public virtual Guid Key2  { get; } = _key2.Value;
	public virtual int Key3  { get; } = _key3.Value;
	public virtual bool Key4  { get; } = _key4.Value;

        public static string GetSetting(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
    }

    public interface IConnectionStrings
    {
        string LocalSqlServer { get; }
    }

    public class ConnectionStrings : IConnectionStrings
    {
        private static readonly Lazy<string> _localSqlServer = new Lazy<string>(() => ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);

	public virtual string LocalSqlServer { get; } = _localSqlServer.Value;
    }

As you can see by this output, same goes for connection strings as well!

Install

Can be install from Nuget as well;

Install-Package T4Config

https://www.nuget.org/packages/T4Config/

How do I do it

  • Copy the Configurations.tt file to the src directory of your project (or install via nuget).
  • Right click on the Configurations.tt file and click "Run Custom Tool".

Enjoy!

T4Config

A down and dirty way to have strongly typed Config Settings and Connection Strings in your projects by using T4.

In a nutshell we create an interface called IConfigurations/ IConnectionStrings, so you can do all of your fancy DI/IoC stuff, and a concrete implementation called Configurations/ ConnectionStrings. (both names totally customizable.

The T4 template loops through the web.config (or app.config) appSettings and generates a read only property for each key and returns the values placed in the settings value.

If you have a configuration section of appSettings which looked like this;

<appSettings>
    <add key="Key1" value="value1" />
    <add key="Key2" value="2212DE83-DFC3-44A8-81BA-0A8D132C1F79" />
    <add key="Key3" value="42" />
</appSettings>

After the T4 file compiles you would end up with this;

    public interface IConfigurations
    {
        string Key1 { get; }

        Guid Key2 { get; }

        int Key3 { get; }

        bool Key4 { get; }

    }

    public class Configurations : IConfigurations
    {
        private static readonly Lazy<string> _key1 = new Lazy<string>(() => GetSetting("Key1"));

        private static readonly Lazy<Guid> _key2 = new Lazy<Guid>(() => new Guid(GetSetting("Key2")));

        private static readonly Lazy<int> _key3 = new Lazy<int>(() => Convert.ToInt32(GetSetting("Key3")));

        private static readonly Lazy<bool> _key4 = new Lazy<bool>(() => Convert.ToBoolean(GetSetting("Key4")));


	public virtual string Key1  { get; } = _key1.Value;
	public virtual Guid Key2  { get; } = _key2.Value;
	public virtual int Key3  { get; } = _key3.Value;
	public virtual bool Key4  { get; } = _key4.Value;

        public static string GetSetting(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
    }

    public interface IConnectionStrings
    {
        string LocalSqlServer { get; }
    }

    public class ConnectionStrings : IConnectionStrings
    {
        private static readonly Lazy<string> _localSqlServer = new Lazy<string>(() => ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);

	public virtual string LocalSqlServer { get; } = _localSqlServer.Value;
    }

As you can see by this output, same goes for connection strings as well!

Install

Can be install from Nuget as well;

Install-Package T4Config

https://www.nuget.org/packages/T4Config/

How do I do it

  • Copy the Configurations.tt file to the src directory of your project (or install via nuget).
  • Right click on the Configurations.tt file and click "Run Custom Tool".

Enjoy!

Dependencies

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.5.1 327 4/27/2019
1.0.5 227 4/27/2019
1.0.4.4 5,928 5/7/2015
1.0.4.3 576 5/7/2015
1.0.4.1 569 5/7/2015
1.0.3.8 566 4/9/2015
1.0.3.7 570 4/9/2015
1.0.3 577 4/6/2015
1.0.2 652 2/27/2015
1.0.1.2 571 2/27/2015
1.0.1 592 2/26/2015
1.0.0 590 2/26/2015