[英]DbTransaction class and about interfaces in C#
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. 我的第一个问题是关于这个“DbTransaction”课程。 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? 我的第二个问题是,为什么在DataProvider(实现类)中,函数调用自身的另一个重载?
DbTransaction
is a common base-class for representing database transactions in ADO.NET; DbTransaction
是在ADO.NET中表示数据库事务的通用基类; each actual ADO.NET provider subclasses this (typically) - for example SqlTransaction : DbTransaction
(the sql-server client). 每个实际的ADO.NET提供程序都是这个(通常)的子类 - 例如
SqlTransaction : DbTransaction
(sql-server客户端)。
Calling an overload of self is a common way of implementing optional parameters, without code duplication prior to their addition in C# 4.0. 调用self的重载是实现可选参数的常用方法,在C#4.0中添加之前没有代码重复 。 In this case, that is essentially a pre-4.0 way of implementing:
在这种情况下,这基本上是一种4.0之前的实现方式:
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
The first question is impossible to answer for sure without seeing the whole code, but it's probably referring to System.Data.Common.DbTransaction
. 第一个问题是不可能的回答是肯定的 ,没有看到整个代码,但它可能是指
System.Data.Common.DbTransaction
。
As for the implementation - presumably it's a way of reusing the code, that's all. 至于实现 - 可能是重用代码的一种方式,就是这样。 If the implementation of the method with a parameter can handle a parameter value of
null
as "do it in a new transaction" (or whatever the behaviour of the parameterless method should be) naturally, why wouldn't you want one overload to call the other? 如果使用参数的方法的实现可以处理参数值
null
作为“在新事务中执行”(或无参数方法的任何行为应该是),自然为什么不想要一个重载来调用其他?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.