我有来自不同来源的代码,目标是在CSV文件与定义的模式匹配后读取该文件,然后将其复制到表格数据库中。在将文件复制到表格数据库中之前,CSV文件必须具有以下两列:PartitionKey
和RowKey
。如果分区键不在那里,那么它应该将正在传递的ID作为参数。下面是代码,我不明白Func<Dictionary<>,string>
部分在做什么。有人能向我解释一下它的用途和工作原理吗?
//主要函数WriteToTable的调用方式如下:
await WriteToTable(lines, dataclass,
p => documentId,
p => $"{dataclass.SubType}_{p["RowKey"].Int32Value.Value.ToString("D10")}", upsert);
//写入表
public async Task WriteToTable(string lines, DataClass dataclass,
Func<Dictionary<string, EntityProperty>, string> genPartitionKey,
Func<Dictionary<string, EntityProperty>, string> genRowKey, bool upsert)
{
const int BatchSize = 100;
if (HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genPartitionKey = (Dictionary<string, EntityProperty> props) => props["PartitionKey"].StringValue;
genRowKey = (Dictionary<string, EntityProperty> props) => props["RowKey"].ToString();
}
var tableRecords =ReadCSV(lines, dataclass.TableSchema.Fields)
.Select(props => new DynamicTableEntity(genPartitionKey(props), genRowKey(props), string.Empty, props))
.ToList();
await batchInsertIntoTableStorage(BatchSize,tableRecords, upsert);
}
static readonly string[] RequiredTableKeys = { "PartitionKey", "RowKey" };
private bool HasPartitionAndRowKey(List<TableField> fields)
{
return fields.Select(f => f.Name).Intersect(RequiredTableKeys).Count() == RequiredTableKeys.Length;
}
这是函数如何工作的最简单方法
public bool Validate(Func<string,bool> dependentMethod)
{
string parmeter = "after execution inside method";
bool isvalid = dependentMethod(parmeter);
return isvalid;
}
public bool DependentMethod(string input)
{
// process there statement and return out put after your business logics
return true;
}
public void CheckValidation()
{
bool isValid= Validate(DependentMethod);
}
根据您的方法
public async Task WriteToTable(string lines, DataClass dataclass,
Func<Dictionary<string, EntityProperty>, string> genPartitionKey,
Func<Dictionary<string, EntityProperty>, string> genRowKey, bool upsert)
这是你的方法头,这里有两个函数genPartitionKey,genRowKey
实施
if (HasPartitionAndRowKey(dataclass.TableSchema.Fields))
{
genPartitionKey = (Dictionary<string, EntityProperty> props) => props["PartitionKey"].StringValue;
genRowKey = (Dictionary<string, EntityProperty> props) => props["RowKey"].ToString();
}
如果check为true,则他会重新分配您的方法genPartitionKey=(Dictionary<string, EntityProperty> props)=>{return props["RowKey"].ToString();}
与第二个相同
在这个表达中,他把这两种方法都称为var tableRecords =ReadCSV(lines, dataclass.TableSchema.Fields).Select(props => new DynamicTableEntity(genPartitionKey(props), genRowKey(props), string.Empty, props)).ToList();
我称之为
Validate(DependentMethod);