下面是我的工厂类:
public class BulkFactory<T>
{
private BulkFactory() { }
static readonly Dictionary<string, Func<T>> _dict
= new Dictionary<string, Func<T>>();
public static T Create(string id)
{
Func<T> constructor = null;
if (_dict.TryGetValue(id, out constructor))
return constructor();
throw new ArgumentException("No type registered for this id");
}
public static void Register(string id, Func<T> ctor)
{
_dict.Add(id, ctor);
}
}
这就是我向这家工厂注册各种批量工作的方式:
BulkFactory<IBulk>.Register("LCOUPD", () => new BAT_LCOUPD<BulkLCOUpdateRecord, BAT_LCOUPD_LOG>(bulkJobCode));
BulkFactory<IBulk>.Register("STBILL", () => new BAT_STBILL<BulkStartStopBillUpdateRecord, BAT_STBILL_LOG>(bulkJobCode));
BulkFactory<IBulk>.Register("PLANCH", () => new BAT_PLANCH<BulkPlanChangeUpdateRecord, BAT_PLANCH_LOG>(bulkJobCode));
BulkFactory<IBulk>.Register("CSCORE", () => new BAT_CSCORE<BulkCSCOREUpdateRecord, BAT_CSCORE_LOG>(bulkJobCode));
BulkFactory<IBulk>.Register("CUSTAQ", () => new BAT_CUSTAQ<CustomerAcquisitionTemplate, BAT_CUSTAQ_LOG>(bulkJobCode));
为了遵循 Open Closed 主体,我想将所有这些寄存器条目存储到配置文件中,并希望从配置中加载它。因此,每当我添加一个新的批量作业时,我都不需要修改上面的代码行。
请建议我怎样才能做到这一点。