شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة ش.ش.و.

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Miscellaneous

Public general use code classes and xml files that we've compiled and used over the years:

Miscellaneous Entity Framework class for Next Generation Network (NGN) data model.

   1:  using Newtonsoft.Json;
   2:  using System;
   3:  using System.Collections.Generic;
   4:  using System.Linq;
   5:   
   6:  namespace Ia.Ngn.Cl.Model.Data
   7:  {
   8:      ////////////////////////////////////////////////////////////////////////////
   9:   
  10:      /// <summary publish="true">
  11:      /// Miscellaneous Entity Framework class for Next Generation Network (NGN) data model.
  12:      /// </summary>
  13:      /// 
  14:      /// <remarks> 
  15:      /// Copyright © 2006-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
  16:      ///
  17:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  18:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  19:      ///
  20:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  21:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  22:      /// 
  23:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  24:      /// 
  25:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  26:      /// </remarks> 
  27:      public partial class Miscellaneous
  28:      {
  29:          /// <summary/>
  30:          public Miscellaneous() { }
  31:   
  32:          ////////////////////////////////////////////////////////////////////////////
  33:   
  34:          /// <summary>
  35:          ///
  36:          /// </summary>
  37:          public static void Create(Ia.Ngn.Cl.Model.Miscellaneous item)
  38:          {
  39:              if (item != null)
  40:              {
  41:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
  42:                  {
  43:                      item.Created = item.Updated = DateTime.UtcNow.AddHours(3);
  44:   
  45:                      db.Miscellaneous.Add(item);
  46:                      db.SaveChanges();
  47:                  }
  48:              }
  49:              else throw new System.ArgumentOutOfRangeException("item is null");
  50:          }
  51:   
  52:          ////////////////////////////////////////////////////////////////////////////
  53:   
  54:          /// <summary>
  55:          ///
  56:          /// </summary>
  57:          public static void Create(string name, string value)
  58:          {
  59:              Ia.Ngn.Cl.Model.Miscellaneous misc;
  60:   
  61:              if (!string.IsNullOrEmpty(name))
  62:              {
  63:                  misc = new Ia.Ngn.Cl.Model.Miscellaneous
  64:                  {
  65:                      Name = name,
  66:                      Value = value
  67:                  };
  68:   
  69:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
  70:                  {
  71:                      misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
  72:   
  73:                      db.Miscellaneous.Add(misc);
  74:                      db.SaveChanges();
  75:                  }
  76:              }
  77:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
  78:          }
  79:   
  80:          ////////////////////////////////////////////////////////////////////////////
  81:   
  82:          /// <summary>
  83:          ///
  84:          /// </summary>
  85:          public static void Create(string name, bool flag)
  86:          {
  87:              Ia.Ngn.Cl.Model.Miscellaneous misc;
  88:   
  89:              if (!string.IsNullOrEmpty(name))
  90:              {
  91:                  misc = new Ia.Ngn.Cl.Model.Miscellaneous
  92:                  {
  93:                      Name = name,
  94:                      Flag = flag
  95:                  };
  96:   
  97:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
  98:                  {
  99:                      misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
 100:   
 101:                      db.Miscellaneous.Add(misc);
 102:                      db.SaveChanges();
 103:                  }
 104:              }
 105:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
 106:          }
 107:   
 108:          ////////////////////////////////////////////////////////////////////////////
 109:   
 110:          /// <summary>
 111:          ///
 112:          /// </summary>
 113:          public static Ia.Ngn.Cl.Model.Miscellaneous Read(long id)
 114:          {
 115:              Ia.Ngn.Cl.Model.Miscellaneous item;
 116:   
 117:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 118:              {
 119:                  item = (from m in db.Miscellaneous where m.Id == id select m).SingleOrDefault();
 120:              }
 121:   
 122:              return item;
 123:          }
 124:   
 125:          ////////////////////////////////////////////////////////////////////////////
 126:   
 127:          /// <summary>
 128:          ///
 129:          /// </summary>
 130:          public static string Read(string name)
 131:          {
 132:              string value;
 133:   
 134:              if (!string.IsNullOrEmpty(name))
 135:              {
 136:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 137:                  {
 138:                      value = (from m in db.Miscellaneous where m.Name == name select m.Value).SingleOrDefault();
 139:                  }
 140:              }
 141:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
 142:   
 143:              return value;
 144:          }
 145:   
 146:          ////////////////////////////////////////////////////////////////////////////
 147:   
 148:          /// <summary>
 149:          ///
 150:          /// </summary>
 151:          public static T Read<T>(string name)
 152:          {
 153:              string json;
 154:              T t;
 155:   
 156:              if (!string.IsNullOrEmpty(name))
 157:              {
 158:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 159:                  {
 160:                      json = (from m in db.Miscellaneous where m.Name == name select m.Value).SingleOrDefault();
 161:   
 162:                      if (json != null) t = JsonConvert.DeserializeObject<T>(json);
 163:                      else t = default(T);
 164:                  }
 165:              }
 166:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
 167:   
 168:              return t;
 169:          }
 170:   
 171:          ////////////////////////////////////////////////////////////////////////////
 172:   
 173:          /// <summary>
 174:          ///
 175:          /// </summary>
 176:          public static bool ReadFlag(string name)
 177:          {
 178:              bool flag;
 179:   
 180:              if (!string.IsNullOrEmpty(name))
 181:              {
 182:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 183:                  {
 184:                      flag = (from m in db.Miscellaneous where m.Name == name select m.Flag).SingleOrDefault();
 185:                  }
 186:              }
 187:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
 188:   
 189:              return flag;
 190:          }
 191:   
 192:          ////////////////////////////////////////////////////////////////////////////
 193:   
 194:          /// <summary>
 195:          ///
 196:          /// </summary>
 197:          public static List<Ia.Ngn.Cl.Model.Miscellaneous> ReadList()
 198:          {
 199:              List<Ia.Ngn.Cl.Model.Miscellaneous> itemList;
 200:   
 201:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 202:              {
 203:                  itemList = (from m in db.Miscellaneous select m).ToList();
 204:              }
 205:   
 206:              return itemList;
 207:          }
 208:   
 209:          ////////////////////////////////////////////////////////////////////////////
 210:   
 211:          /// <summary>
 212:          ///
 213:          /// </summary>
 214:          public static void Update(Ia.Ngn.Cl.Model.Miscellaneous item)
 215:          {
 216:              if (item != null)
 217:              {
 218:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 219:                  {
 220:                      item = (from m in db.Miscellaneous where m.Id == item.Id select m).SingleOrDefault();
 221:   
 222:                      item.Updated = DateTime.UtcNow.AddHours(3);
 223:   
 224:                      db.Miscellaneous.Attach(item);
 225:   
 226:                      db.Entry(item).State = System.Data.Entity.EntityState.Modified;
 227:                      db.SaveChanges();
 228:                  }
 229:              }
 230:              else throw new System.ArgumentOutOfRangeException("item is null");
 231:          }
 232:   
 233:          ////////////////////////////////////////////////////////////////////////////
 234:   
 235:          /// <summary>
 236:          ///
 237:          /// </summary>
 238:          public static void CreateOrUpdate(string name, string value)
 239:          {
 240:              Ia.Ngn.Cl.Model.Miscellaneous misc;
 241:   
 242:              if (!string.IsNullOrEmpty(name))
 243:              {
 244:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 245:                  {
 246:                      misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
 247:   
 248:                      if (misc == null)
 249:                      {
 250:                          misc = new Ia.Ngn.Cl.Model.Miscellaneous
 251:                          {
 252:                              Name = name,
 253:                              Value = value
 254:                          };
 255:                          misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
 256:   
 257:                          db.Miscellaneous.Add(misc);
 258:                      }
 259:                      else
 260:                      {
 261:                          misc.Updated = DateTime.UtcNow.AddHours(3);
 262:   
 263:                          misc.Value = value;
 264:   
 265:                          db.Miscellaneous.Attach(misc);
 266:   
 267:                          db.Entry(misc).State = System.Data.Entity.EntityState.Modified;
 268:                      }
 269:   
 270:                      db.SaveChanges();
 271:                  }
 272:              }
 273:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
 274:          }
 275:   
 276:          ////////////////////////////////////////////////////////////////////////////
 277:   
 278:          /// <summary>
 279:          ///
 280:          /// </summary>
 281:          public static void CreateOrUpdate(string name, object value)
 282:          {
 283:              string json;
 284:              Ia.Ngn.Cl.Model.Miscellaneous misc;
 285:   
 286:              if (!string.IsNullOrEmpty(name))
 287:              {
 288:                  json = JsonConvert.SerializeObject(value, Formatting.Indented);
 289:   
 290:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 291:                  {
 292:                      misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
 293:   
 294:                      if (misc == null)
 295:                      {
 296:                          misc = new Ia.Ngn.Cl.Model.Miscellaneous
 297:                          {
 298:                              Name = name,
 299:                              Value = json
 300:                          };
 301:                          misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
 302:   
 303:                          db.Miscellaneous.Add(misc);
 304:                      }
 305:                      else
 306:                      {
 307:                          misc.Updated = DateTime.UtcNow.AddHours(3);
 308:   
 309:                          misc.Value = json;
 310:   
 311:                          db.Miscellaneous.Attach(misc);
 312:   
 313:                          db.Entry(misc).State = System.Data.Entity.EntityState.Modified;
 314:                      }
 315:   
 316:                      db.SaveChanges();
 317:                  }
 318:              }
 319:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
 320:          }
 321:   
 322:          ////////////////////////////////////////////////////////////////////////////
 323:   
 324:          /// <summary>
 325:          ///
 326:          /// </summary>
 327:          public static void CreateOrUpdateFlag(string name, bool flag)
 328:          {
 329:              Ia.Ngn.Cl.Model.Miscellaneous misc;
 330:   
 331:              if (!string.IsNullOrEmpty(name))
 332:              {
 333:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 334:                  {
 335:                      misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
 336:   
 337:                      if (misc == null)
 338:                      {
 339:                          misc = new Ia.Ngn.Cl.Model.Miscellaneous
 340:                          {
 341:                              Name = name,
 342:                              Flag = flag
 343:                          };
 344:                          misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
 345:   
 346:                          db.Miscellaneous.Add(misc);
 347:                      }
 348:                      else
 349:                      {
 350:                          misc.Updated = DateTime.UtcNow.AddHours(3);
 351:   
 352:                          misc.Flag = flag;
 353:   
 354:                          db.Miscellaneous.Attach(misc);
 355:   
 356:                          db.Entry(misc).State = System.Data.Entity.EntityState.Modified;
 357:                      }
 358:   
 359:                      db.SaveChanges();
 360:                  }
 361:              }
 362:              else throw new System.ArgumentOutOfRangeException("name is null or empty");
 363:          }
 364:   
 365:          ////////////////////////////////////////////////////////////////////////////
 366:   
 367:          /// <summary>
 368:          ///
 369:          /// </summary>
 370:          public static void SetFlag(string name)
 371:          {
 372:              CreateOrUpdateFlag(name, true);
 373:          }
 374:   
 375:          ////////////////////////////////////////////////////////////////////////////
 376:   
 377:          /// <summary>
 378:          ///
 379:          /// </summary>
 380:          public static void ResetFlag(string name)
 381:          {
 382:              CreateOrUpdateFlag(name, false);
 383:          }
 384:   
 385:          ////////////////////////////////////////////////////////////////////////////
 386:   
 387:          /// <summary>
 388:          ///
 389:          /// </summary>
 390:          public static void Delete(long id)
 391:          {
 392:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 393:              {
 394:                  var v = (from m in db.Miscellaneous where m.Id == id select m).FirstOrDefault();
 395:   
 396:                  db.Miscellaneous.Remove(v);
 397:                  db.SaveChanges();
 398:              }
 399:          }
 400:   
 401:          ////////////////////////////////////////////////////////////////////////////
 402:          ////////////////////////////////////////////////////////////////////////////
 403:      }
 404:   
 405:      ////////////////////////////////////////////////////////////////////////////
 406:      ////////////////////////////////////////////////////////////////////////////
 407:  }