/// <summary> /// 根据配置和指定的数据源初始化<see cref="SyncSession"/>。 /// </summary> /// <param name="planName">配置文件中的计划名称</param> /// <param name="dataProvider"><see cref="DataProviderBase"/>的派生类型的对象,提供源数据。</param> /// <returns></returns> public static SyncSession CreateSession(string planName, DataProviderBase dataProvider) { Configuration.PermissionCenterIncomeSyncSettings config = Configuration.PermissionCenterIncomeSyncSettings.GetConfig(); var planConfig = config.SyncPlans[planName]; if (planConfig == null) { throw new ArgumentException("未找到同步方案 " + planName, "providerName"); } HashSet <string> comparerNames = new HashSet <string>(); HashSet <string> setterNames = new HashSet <string>(); PropertyMappingCollection mappings = new PropertyMappingCollection(); mappings.SourceKeyProperty = planConfig.SourceKeyProperty; foreach (PropertyMappingConfigurationElement item in planConfig.PropertyMappings) { comparerNames.Add(item.ComparerName); setterNames.Add(item.SetterName); //需要哪些比较器和设置器 mappings.Add(item.SourceProperty, new PropertyMapping(item.SourceProperty, item.TargetProperty, item.ComparerName, item.SetterName, new WrappedNameValueCollection(item.Parameters))); } var comparers = CreateComparers(comparerNames, config.Comparers); var setters = CreateSetters(setterNames, config.PropertySetters); IList <LogProviderBase> loggers = CreateLoggers(planConfig); SyncSession task = new SyncSession(planName, dataProvider, comparers, setters, loggers, mappings); task.batchSize = planConfig.BatchSize; return(task); }
private SyncSession(string planName, DataProviderBase provider, PropertyComparerCollection comparers, PropertySetterCollection setters, IList <LogProviderBase> loggers, PropertyMappingCollection mappings) { this.planName = planName; this.provider = provider; this.comparers = comparers; this.setters = setters; this.loggers = loggers; this.mappings = mappings; this.batchSize = 1; }
public virtual void Execute(string planName, DataProviderBase dataProvider) { SyncSession session = SyncSession.CreateSession(planName, dataProvider); SyncEventArgs e = new SyncEventArgs(session); OnBeforeExecute(e); SCDataOperationLockContext.Current.DoAddLockAction(true, "向权限中心同步", () => InnerExecute(session)); OnAfterExecute(e); }
private int FetchToBuffer(List <NameObjectCollection> buffer, DataProviderBase dataSource, int size) { int count = 0; while (count < size && dataSource.MoveNext()) { count++; NameObjectCollection data = new NameObjectCollection(dataSource.CurrentData); buffer.Add(data); } return(count); }
private void DoOneByOneExecute(SyncSession session) { DataProviderBase provider = session.SourceProvider; provider.Reset(); while (provider.MoveNext()) { string key = (string)provider.CurrentData[session.Mappings.SourceKeyProperty]; if (string.IsNullOrWhiteSpace(key)) { throw new SyncException("数据源提供的对象的ID为空"); } SchemaObjectBase scObj = LoadSchemaObject(key); CompareAndChange(session, provider.CurrentData, session.Mappings.AllObjectValues, scObj); } }
/// <summary> /// 根据配置初始化一个<see cref="SyncSession"/> /// </summary> /// <param name="planName">配置文件中的计划名称</param> /// <returns></returns> public static SyncSession CreateSession(string planName) { Configuration.PermissionCenterIncomeSyncSettings config = Configuration.PermissionCenterIncomeSyncSettings.GetConfig(); var planConfig = config.SyncPlans[planName]; if (planConfig == null) { throw new ArgumentException("未找到同步方案 " + planName, "providerName"); } if (planConfig.DataProvider == null) { throw new System.Configuration.ConfigurationErrorsException("配置文件中,未配置dataProvider"); } DataProviderBase provider = (DataProviderBase)CreateDataProvider(planConfig.DataProvider); return(CreateSession(planName, provider)); }
private void DoBatchExecute(SyncSession session) { DataProviderBase provider = session.SourceProvider; provider.Reset(); List <NameObjectCollection> buffer = new List <NameObjectCollection>(session.BatchSize); int len; do { buffer.Clear(); len = FetchToBuffer(buffer, provider, session.BatchSize); if (len > 0) { string[] keys = new string[len]; for (int i = buffer.Count - 1; i >= 0; i--) { keys[i] = (string)buffer[i][session.Mappings.SourceKeyProperty]; if (string.IsNullOrWhiteSpace(keys[i])) { throw new SyncException("数据源提供的对象的ID为空"); } } SchemaObjectCollection scObjs = LoadSchemaObjects(keys); foreach (NameObjectCollection item in buffer) { string id = (string)item[session.Mappings.SourceKeyProperty]; SchemaObjectBase scObj = scObjs[id]; if (scObjs != null) { CompareAndChange(session, item, session.Mappings.AllObjectValues, scObj); } } } } while (len > 0 && len != session.BatchSize); }
private int FetchToBuffer(List<NameObjectCollection> buffer, DataProviderBase dataSource, int size) { int count = 0; while (count < size && dataSource.MoveNext()) { count++; NameObjectCollection data = new NameObjectCollection(dataSource.CurrentData); buffer.Add(data); } return count; }