Skip to content

Data.Linq.DataContext

Igor Tkachev edited this page May 20, 2016 · 1 revision

Home / Data / Linq

BLToolkit Linq provider uses the IDataContext interface to communicate with the data sources such as database, WCF, etc. BLToolkit contains the following IDataContext implementations:

DbManager

DbManager is the wrapper around a database connection and should be treated accordingly. Any instance of the DbManager class has to be disposed after use.

using (var ctx = new DbManager())
    return ctx.GetTable<Person>().ToList();

DataContext

DataContext creates new connection object for every incoming query and dispose it immediately. So this object does not have to be disposed itself.

var ctx = new DataContext();
return ctx.GetTable<Person>().ToList();

However, setting the KeepConnectionAlive property to true changes this behavior and makes DataContext similar to DbManager.

using (var ctx = new DataContext { KeepConnectionAlive = true })
    return ctx.GetTable<Person>().ToList();

The DataContextTransaction class is designed to work with DataContext to provide transaction functionality.

var ctx = new DataContext();

List<Person> result;

using (var tran = new DataContextTransaction(ctx))
{
    tran.BeginTransaction();

    list = ctx.GetTable<Person>().ToList();

    tran.CommitTransaction();

    return list;
}

ServiceModelDataContext

ServiceModelDataContext provides Linq over WCF.

using (var host = new ServiceHost(new LinqService(), new Uri("net.tcp://localhost:1234")))
{
    host.Description.Behaviors.Add(new ServiceMetadataBehavior());
    host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
    host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
    host.AddServiceEndpoint(
        typeof(ILinqService),
        new NetTcpBinding(SecurityMode.None)
        {
            MaxReceivedMessageSize = 10000000,
            MaxBufferPoolSize      = 10000000,
            MaxBufferSize          = 10000000,
            CloseTimeout           = new TimeSpan(00, 01, 00),
            OpenTimeout            = new TimeSpan(00, 01, 00),
            ReceiveTimeout         = new TimeSpan(00, 10, 00),
            SendTimeout            = new TimeSpan(00, 10, 00),
        },
        "LinqOverWCF");

    host.Open();

    var ctx = new ServiceModelDataContext(
        new NetTcpBinding(SecurityMode.None)
        {
            MaxReceivedMessageSize = 10000000,
            MaxBufferPoolSize      = 10000000,
            MaxBufferSize          = 10000000,
            CloseTimeout           = new TimeSpan(00, 01, 00),
            OpenTimeout            = new TimeSpan(00, 01, 00),
            ReceiveTimeout         = new TimeSpan(00, 10, 00),
            SendTimeout            = new TimeSpan(00, 10, 00),
        },
        new EndpointAddress("net.tcp://localhost:1234/LinqOverWCF"));

    var list = ctx.GetTable<Person>().ToList();

    host.Close();

    return list;
}
Clone this wiki locally