DbTransaction class and about interfaces in C #

I came across an example implementation of an interface. Code piece

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 an assembly in the class?

My second question: why in the DataProvider (implementation class) does the function cause another overload of itself?

+4
source share
2 answers

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

Calling self overloading is a general way to implement optional parameters without duplicating code before adding them to C # 4.0. In this case, it is essentially a way of implementing up to 4.0:

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

any implementation (overload or optional parameter) allows you to use the form:

 obj.GetEmployeeAbsenceDurationTypes(); // without transaction obj.GetEmployeeAbsenceDurationTypes(tran); // with transaction 
+5
source

The first question cannot be answered accurately without seeing all the code, but it probably refers to System.Data.Common.DbTransaction .

As for the implementation - apparently this is a way to reuse the code, that's all. If an implementation of a method with a parameter can handle the value of the null parameter as "do it in a new transaction" (or whatever the behavior of the method without parameters), why don't you need one overload to call another?

+4
source

Source: https://habr.com/ru/post/1390594/


All Articles