Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbTransaction class and about interfaces in C#

Tags:

c#

interface

I came across an example of the implementation an an interface. Portion of code is

public partial interface IDataProvider
{
    DataTable GetEmployeeAbsenceDurationTypes();

    void AlterEmployeeAbsenceDurationTypes(DataTable lookUpTable);
}

public partial class DataProvider : IDataProvider
{    

    public DataTable GetEmployeeAbsenceDurationTypes()
    {
        return GetEmployeeAbsenceDurationTypes((DbTransaction)null);
    }
    public DataTable GetEmployeeAbsenceDurationTypes(DbTransaction tran)
    {
        //Db Operations
    }
}

My first question is about this "DbTransaction" class. Its not in my project, is it a build in class?

My second question is, why in the DataProvider (the implementing class), the function is calling another overload of itself?

like image 567
Muhammad Umer Asif Avatar asked Jan 18 '23 19:01

Muhammad Umer Asif


1 Answers

DbTransaction is a common base-class for representing database transactions in ADO.NET; each actual ADO.NET provider subclasses this (typically) - for example SqlTransaction : DbTransaction (the sql-server client).

Calling an overload of self is a common way of implementing optional parameters, without code duplication prior to their addition in C# 4.0. In this case, that is essentially a pre-4.0 way of implementing:

public DataTable GetEmployeeAbsenceDurationTypes(DbTransaction tran = null) {...}

either implementation (overloads or optional parameter) allows usage of the form:

obj.GetEmployeeAbsenceDurationTypes(); // without transaction
obj.GetEmployeeAbsenceDurationTypes(tran); // with transaction
like image 107
Marc Gravell Avatar answered Jan 30 '23 10:01

Marc Gravell