Table of Contents

Overview

In Soma, a data source configuration is represented as the Soma.Core.IDbConfig interface.
IDbConfig has the ConnectionString property and the Logger property and so on.

Implementations of IDbConfig are corresponding to RDBMSs.

ConnectionString

To specify a ConnectionString, you can implement the ConnectionString property of IDbConfig.

Connecting to Microsoft SQL Server 2008

F#
open Soma.Core

let config = 
  { new MsSqlConfig() with
    member this.ConnectionString = "Data Source=.;Initial Catalog=Example;Integrated Security=True" }

C#
using Soma.Core;

public class Config : MsSqlConfig
{
    public override string ConnectionString { get { return "Data Source=.;Initial Catalog=Example;Integrated Security=True"; } }
}

As precondition, the following settings must be specified in a configuration file(such as machine.config, app.config and web.config).

xml
<system.data>
    <DbProviderFactories>
        <add name="SqlClient Data Provider" 
            invariant="System.Data.SqlClient"
            description=".Net Framework Data Provider for SqlServer" 
            type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </DbProviderFactories>
</system.data>

Connecting to Microsoft SQL Server Compact 4.0

To connect to Microsoft SQL Server Compact 4.0, the runtime is required.

F#
open Soma.Core

let config = 
  { new MsSqlCeConfig() with
    member this.ConnectionString = "Data Source=..\..\Example.sdf" }

C#
using Soma.Core;

public class Config : MsSqlCeConfig
{
    public override string ConnectionString { get { return "Data Source=..\..\Example.sdf"; } }
}

As precondition, the following settings must be specified in a configuration file(such as machine.config, app.config and web.config).

xml
<configuration>
    <system.data>
        <DbProviderFactories>
            <add name="Microsoft SQL Server Compact Data Provider 4.0" 
                invariant="System.Data.SqlServerCe.4.0" 
                description=".NET Framework Data Provider for Microsoft SQL Server Compact" 
                type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
        </DbProviderFactories>
    </system.data>
</configuration>

Connecting to MySQL 5.x

To Connecting to MySQL 5.x、ADO.NET Driver for MySQL is required.

F#
open Soma.Core

let config = 
  { new MySqlConfig() with
    member this.ConnectionString = "server=localhost;User Id=hoge;Password=foo;database=Example" }

C#
using Soma.Core;

public class Config : MySqlConfig
{
    public override string ConnectionString { get { return "server=localhost;User Id=hoge;Password=foo;database=Example"; } }
}

As precondition, the following settings must be specified in a configuration file(such as machine.config, app.config and web.config).

xml
<configuration>
    <system.data>
        <DbProviderFactories>
            <add name="MySQL Data Provider" 
                invariant="MySql.Data.MySqlClient" 
                description=".Net Framework Data Provider for MySQL" 
                type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
        </DbProviderFactories>
    </system.data>
</configuration>

Connecting to Oracle Database 11g

To connect to Oracle Database 11g, Oracle Data Provider for .NET is required.

F#
open Soma.Core

let config = 
  { new OracleConfig() with
    member this.ConnectionString = "DATA SOURCE=localhost;USER ID=SCOTT;PASSWORD=TIGER" }

C#
using Soma.Core;

public class Config : OracleConfig 
{
    public override string ConnectionString { get { return "DATA SOURCE=localhost;USER ID=SCOTT;PASSWORD=TIGER"; } }
}

As precondition, the following settings must be specified in a configuration file(such as machine.config, app.config and web.config).

xml
<configuration>
    <system.data>
        <DbProviderFactories>
            <add name="Oracle Data Provider for .NET" 
                invariant="Oracle.DataAccess.Client" 
                description="Oracle Data Provider for .NET" 
                type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
        </DbProviderFactories>
    </system.data>
</configuration>

Connecting to SQLite 3

To connect to SQLite 3, System.Data.SQLite is required.

F#
open Soma.Core

let config = 
  { new SQLiteConfig() with
    member this.ConnectionString = "Data Source=..\..\Example.db" }

C#
using Soma.Core;

public class Config : SQLiteConfig 
{
    public override string ConnectionString { get { return "Data Source=..\..\Example.db"; } }
}

As precondition, the following settings must be specified in a configuration file(such as machine.config, app.config and web.config).

xml
<configuration>
    <system.data>
        <DbProviderFactories>
            <add name="SQLite Data Provider" 
                invariant="System.Data.SQLite" 
                description=".Net Framework Data Provider for SQLite" 
                type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"/>
        </DbProviderFactories>
    </system.data>
</configuration>

Logging SQL

By default, sql strings issued by Soma are replaced parameters with actual arguments and output to a console window.

To customize default behavior, you can implement the Logger property of IDbConfig.The parameter type of the Logger property is Soma.Core.PreparedStatement.

In the following example, the sqls, which contain parameters, are logged by using NLog.

F#
open System
open NLog
open Soma.Core

let config = 
  let logger = LogManager.GetLogger("config")
  let logAction = Action<PreparedStatement>(fun p -> logger.Info(p.Text))
  { new MsSqlConfig() with
    member this.ConnectionString = "Data Source=.;Initial Catalog=Example;Integrated Security=True" 
    member this.Logger = logAction }

C#
using System;
using NLog;
using Soma.Core;

public class Config : MsSqlConfig
{
    private static Logger logger = LogManager.GetLogger(typeof(Config));
    private static Action<PreparedStatement> logAction = p => logger.Info(p.Text);

    public override string ConnectionString { get { return "Data Source=.;Initial Catalog=Example;Integrated Security=True"; } }
    public override Action<PreparedStatement> Logger { get { return logAction; } }
}

Log Suppression

To suppres sql log、use SilentLogger。

F#
open System
open Soma.Core

let config = 
  { new MsSqlConfig() with
    member this.ConnectionString = "Data Source=.;Initial Catalog=Example;Integrated Security=True" 
    member this.Logger = SilentLogger }

C#
using System;
using NLog;
using Soma.Core;

public class Config : MsSqlConfig
{
    public override string ConnectionString { get { return "Data Source=.;Initial Catalog=Example;Integrated Security=True"; } }
    public override Action<PreparedStatement> Logger { get { return SilentLogger; } }
}

How to Use a Data Source Configuration

F#
open Soma.Core

let config = 
  { new MsSqlConfig() with
    member this.ConnectionString = "Data Source=.;Initial Catalog=Example;Integrated Security=True" }

let dept = Db.find<Department> config [2]
...

C#
using Soma.Core;

public class Config : MsSqlConfig
{
    public override string ConnectionString { get { return "Data Source=.;Initial Catalog=Example;Integrated Security=True"; } }
}

public class Program
{
    public static void Main()
    {
      var db = new Db(new Config());
      var dept = db.Find<Department>(2);
      ...
    }
}

In F#, You are recommended to wrap a configuration instance with your own module or class for convenience.

F#
open Soma.Core

module MyDb = 
  let config = 
    { new MsSqlConfig() with
      member this.ConnectionString = "Data Source=.;Initial Catalog=Example;Integrated Security=True" }
  let find<'T when 'T : not struct> = Db.find<'T> config

let dept = MyDb.find<Department> [2]
...

Last edited May 14, 2011 at 11:35 PM by toshihiro, version 12

Comments

No comments yet.