如何使用sql连接对象重载此数据访问层方法



两个服务都使用IUnitDataProvider的AddChildrenUnit方法。

TemplateService必须向此方法传递一个已打开的连接对象,因为CreateTemplate方法必须在AddTemplate和"创建根单元节点"的事务中运行。

UnitService没有将连接对象传递给AddChildrenUnit方法,因此代码不会编译!!!

我现在的问题是:我不能更改AddChildrenUnit方法并删除sqlconnection参数,否则CreateTemplate方法中的AddChildrenUnit将不再编译。

那么我现在该怎么办呢?我唯一能想到的是AddChildrenUnit的重载版本,一次带有SqlConnection参数,另一次没有这个参数。

太麻烦了。。。

你知道更好的解决方案吗?

模板服务

public void CreateTemplate(Template template)
{
using (var transaction = new TransactionScope()) 
using (var connection = new SqlConnection(_connectionString))
{
connection.Open();
_templateDataProvider.AddTemplate(template,connection);
Unit rootUnit = new Unit{ TemplateId = template.TemplateId, ParentId = null, Name = "Root" };
_unitDataProvider.AddChildrenUnit(rootUnit,connection);
transaction.Complete();
}
}

UnitService

public void AddChildrenUnit(Unit unit)
{
lock (this)
{
IEnumerable<Unit> childrenUnits = _unitDataProvider.GetChildrenUnits(unit.UnitId); // Selected ParentId
int hierarchyIndexOfSelectedUnitId = childrenUnits.Select(u => u.HierarchyIndex).DefaultIfEmpty(0).Max(c => c);
int hierarchyIndexOfNewChild = hierarchyIndexOfSelectedUnitId + 1;
unit.HierarchyIndex = hierarchyIndexOfNewChild;
_unitDataProvider.AddChildrenUnit(unit);
}
}

UNITDATAPROVIDER

/// <summary>
///  INSERT new child at the end of the children which is the highest HierarchyIndex 
/// </summary>
/// <param name="unit"></param>
public void AddChildrenUnit(Unit unit) // 10 ms
{
using (var trans = new TransactionScope())
using (var con = new SqlConnection(_connectionString))
using (var cmd = new SqlCommand("INSERT INTO UNIT (Name,TemplateId,ParentId,CreatedAt,HierarchyIndex) VALUES (@Name,@TemplateId,@ParentId,@CreatedAt,@HierarchyIndex);Select Scope_Identity();",con))
{
con.Open();
// INSERT new child at the end of the children which is the highest HierarchyIndex                
cmd.Parameters.AddWithValue("HierarchyIndex", unit.HierarchyIndex); 
cmd.Parameters.AddWithValue("TemplateId", unit.TemplateId);
cmd.Parameters.AddWithValue("Name", unit.Name);
cmd.Parameters.Add("CreatedAt", SqlDbType.DateTime2).Value = unit.CreatedAt; 
unit.UnitId = Convert.ToInt32(cmd.ExecuteScalar());
trans.Complete();
}             
}

这个怎么样?

public void AddChildrenUnit(Unit unit) // 10 ms
{
AddChilrenUnit(unit, new SqlConnection(_connectionString));
}
public void AddChildrenUnit(Unit unit, SqlConnection connection)
{
using (var trans = new TransactionScope())
using (connection))
using (var cmd = new SqlCommand("INSERT INTO UNIT (Name,TemplateId,ParentId,CreatedAt,HierarchyIndex) VALUES (@Name,@TemplateId,@ParentId,@CreatedAt,@HierarchyIndex);Select Scope_Identity();",con))
{
con.Open();
// INSERT new child at the end of the children which is the highest HierarchyIndex                
cmd.Parameters.AddWithValue("HierarchyIndex", unit.HierarchyIndex); 
cmd.Parameters.AddWithValue("TemplateId", unit.TemplateId);
cmd.Parameters.AddWithValue("Name", unit.Name);
cmd.Parameters.Add("CreatedAt", SqlDbType.DateTime2).Value = unit.CreatedAt; 
unit.UnitId = Convert.ToInt32(cmd.ExecuteScalar());
trans.Complete();
}             
}

下面是我要做的。

public interface IUnitDataProvider
{
void AddChildrenUnit(Unit unit, string connectionString);
}

public class UnitService:IUnitDataProvider
{
//Implement AddChildrenUnit the way want
//pass the connection string but u dont use it
}
public interface UnitDataProvider:IUnitDataProvider
{
//Implement AddChildrenUnit the way want
}

公共类模板类{

private IUnitDataProvider _unitDataProvider;
public templateClass(IUnitDataProvider provider)
{
_unitDataProvider=provider
}
public void CreateTemplate(Template template)
{
//pre -addUnit code here
_unitDataProvider.AddChildrenUnit(unit, connectionstring);
//Post -addUnit code here
}

}

因此,根据您的使用情况,您可以将正确的具体实现传递给TemplateClass的构造函数(我将其命名为TemplateClass,我不知道您怎么称呼它)

最新更新