/// <summary> /// Processes the mtm list. Adds if they don't exist in the database. /// </summary> /// <param name="rec"></param> public void ProcessMTM(BaseClass rec) { foreach (MTMRelationInfo mtmInfo in rec.MTMInfoList()) { IEnumerable ownMTMList = (IEnumerable)mtmInfo.OwnMTMProp.GetValue(rec); object opposite_dbset = ctx.GetDBSetByType(mtmInfo.OppType); foreach (BaseClass child_mtm in ownMTMList) { if (child_mtm.id == 0) { object[] parameters = new object[1]; parameters[0] = child_mtm; opposite_dbset.GetType().GetMethod("Add").Invoke(opposite_dbset, parameters); //id kontrol et } IList child_mtm_target_list = mtmInfo.OppMTMProp.GetValue(child_mtm) as IList; if (child_mtm_target_list.Contains(rec)) //listemdeki elemanın, aynı ilişki listesinde ben var mıyım? { //bunun yerine allrecords'tan knotrol et? continue; //varsam zaten db'dedir kurallara göre. } else { child_mtm_target_list.Add(rec); if (!ctx.MTMToWrite.Any((KeyValuePair <string, List <Tuple <DKDB.BaseClass, DKDB.BaseClass> > > e) => e.Key == mtmInfo.tableName)) { ctx.MTMToWrite.Add(mtmInfo.tableName, new List <Tuple <DKDB.BaseClass, DKDB.BaseClass> >()); } KeyValuePair <string, List <Tuple <DKDB.BaseClass, DKDB.BaseClass> > > kp = ctx.MTMToWrite.FirstOrDefault((KeyValuePair <string, List <Tuple <DKDB.BaseClass, DKDB.BaseClass> > > e) => e.Key == mtmInfo.tableName); int id_column = WhoAmI(mtmInfo.tableName); if (id_column == 1) { kp.Value.Add(new Tuple <DKDB.BaseClass, DKDB.BaseClass>(rec, child_mtm)); } else if (id_column == 2) { kp.Value.Add(new Tuple <DKDB.BaseClass, DKDB.BaseClass>(child_mtm, rec)); } } } } }
/// <summary> /// Reads the middle table of many-to-many relationship. Bad performance currently, because it reads the /// middle file everytime a record is read. /// </summary> /// <param name="rec"></param> public void ReadMTM(BaseClass rec) { foreach (MTMRelationInfo mtmInfo in rec.MTMInfoList()) { String filepath = Path.Combine(ctx.DatabaseFolder, mtmInfo.tableName) + ".dat"; //mtm middle tablo yolu Stream mtmStream = File.OpenRead(filepath); int line = Convert.ToInt32(mtmStream.Length / (sizeof(int) * 3)); List <MTMRec> mtmRecs = new List <MTMRec>(); //middle tablo kayıtları for (int i = 0; i < line; i++) { Tuple <DKDB.BaseClass, Dictionary <PropertyInfo, int> > fillingLog; fillingLog = FileOps.ReadSingleRecord(mtmStream, i + 1, typeof(MTMRec)); mtmRecs.Add((MTMRec)fillingLog.Item1); } mtmStream.Close(); int my_id_column = WhoAmI(mtmInfo.tableName); //MTMRec içinde id1 mi benim sütunum yoksa id2 mi? ctx'teki sıraya göre kontrol edilir MTMReq mtmReq = new MTMReq(rec, mtmInfo.OwnMTMProp); foreach (MTMRec mtmRec in mtmRecs) //her bir middle tablo kaydı için: { if (my_id_column == 1 && mtmRec.id_1 == rec.id) { mtmReq.AddOppId(mtmRec.id_2);//opposite id } else if (my_id_column == 2 && mtmRec.id_2 == rec.id) { mtmReq.AddOppId(mtmRec.id_1);//opposite id } } KeyValuePair <string, Tuple <Type, Type> > kp = BaseClass.AllMTMRelations.FirstOrDefault(r => r.Key == mtmInfo.tableName); object opposite_dbset = ctx.GetDBSetByType(my_id_column == 1 ? kp.Value.Item2 : kp.Value.Item1); object[] parameters = new object[1]; parameters[0] = mtmReq; //list of ids of the records that we want to read from opposite db PropertyInfo fi = opposite_dbset.GetType().GetProperty("MTMReqList"); List <MTMReq> rtbfMTM = fi.GetValue(opposite_dbset) as List <MTMReq>; rtbfMTM.GetType().GetMethod("Add").Invoke(rtbfMTM, parameters); } }