UnityBaseJS / ub-net-client

UnityBase .Net Client
MIT License
4 stars 1 forks source link

UnityBase .Net Client

This project is a .Net Client for UnityBase application server.

It allows to build .Net application which may perform the following operations:

Connection to UnityBase server from .Net managed code could be useful for scenarios like:

The project is available as NuGet packet.

Samples

Hello World

Connect and output list of users to console:

    var cn = new UbConnection(new Uri("http://localhost:888/"), AuthMethod.Ub("user", "password"));
    var users = cn.Select("uba_user", new[] {"ID", "name"});
    foreach (var user in users)
    {
        Console.WriteLine($"{user["ID"]} {user["name"]}");
    }

Specifying connection parameters in config file

Put the following in app.config file:

<configuration>
    <configSections>
        <section name="unityBase"
                 type="Softengi.UbClient.Configuration.UnityBaseConfigurationSection, Softengi.UbClient" />
    </configSections>

    <unityBase>
        <connection
            baseUri="http://localhost:888/"
            authenticationMethod="ub"
            userName="admin"
            password="admin"
        />
    </unityBase>

    ...
</configuration>

Than in code, create UbConnection instance:

var ubConfig = (UnityBaseConfigurationSection) ConfigurationManager.GetSection("unityBase");
var ubConnection = new UbConnection(ubConfig.Connection);

Using MEF together with UB client

Put the following class in a place where MEF composition container could reach it:

internal sealed class UbConnectionFactory
{
    [Export]
    public UbConnection Connection
    {
        get
        {
            if (_connection != null) return _connection;

            lock (_sync)
            {
                if (_connection == null)
                {
                    var ubConfig = (UnityBaseConfigurationSection) ConfigurationManager.GetSection("unityBase");
                    _connection = new UbConnection(ubConfig.Connection);
                }
            }

            return _connection;
        }
    }

    private volatile UbConnection _connection;
    static private readonly object _sync = new object();
}

Than in any composable part, it would be possible to just declare import:

[Export]
public class MyService
{
    [Import]
    public UbConnection UbConnection { get; set; }

    ...
}