)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Skip Navigation Links

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

Object entity class

    1: using System;
    2: using System.Collections.Generic;
    3: using System.ComponentModel.DataAnnotations;
    4: using System.ComponentModel.DataAnnotations.Schema;
    5: using System.Text.Json;
    6: using System.Linq;
    7:  
    8: namespace Ia.Cl.Model.Db
    9: {
   10:     ////////////////////////////////////////////////////////////////////////////
   11:  
   12:     /// <summary publish="true">
   13:     /// Object entity class
   14:     /// </summary>
   15:     /// <remarks> 
   16:     /// Copyright © 2020-2021 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   17:     ///
   18:     /// 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
   19:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   20:     ///
   21:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   22:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   23:     /// 
   24:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   25:     /// 
   26:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   27:     /// </remarks> 
   28:     public partial class Object
   29:     {
   30:         ////////////////////////////////////////////////////////////////////////////
   31:  
   32:         /// <summary>
   33:         ///
   34:         /// </summary>
   35:         public Object() { }
   36:  
   37:         /// <summary/>
   38:         [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
   39:         public Guid Id { get; set; }
   40:  
   41:         /// <summary/>
   42:         public string Content { get; set; }
   43:  
   44:         /// <summary/>
   45:         public DateTime Created { get; set; }
   46:  
   47:         /// <summary/>
   48:         public DateTime Updated { get; set; }
   49:  
   50:         /// <summary/>
   51:         public Nullable<DateTime> Deleted { get; set; }
   52:  
   53:         /// <summary/>
   54:         //public virtual Ia.Cl.Model.Nfc.Tag Tag { get; set; }
   55:  
   56:         /// <summary/>
   57:         public Guid UserId { get; set; }
   58:  
   59:         ////////////////////////////////////////////////////////////////////////////
   60:  
   61:         /// <summary>
   62:         ///
   63:         /// </summary>
   64:         public bool Update(Object updatedObject)
   65:         {
   66:             // below: this will not update Id, ProjectId, UId, Created, Deleted
   67:             bool updated;
   68:  
   69:             updated = false;
   70:  
   71:             if (this.Content != updatedObject.Content) { this.Content = updatedObject.Content; updated = true; }
   72:  
   73:             //if (this.Tag != updatedObject.Tag) { this.Tag = updatedObject.Tag; updated = true; }
   74:  
   75:             if (this.UserId != updatedObject.UserId) { this.UserId = updatedObject.UserId; updated = true; }
   76:  
   77:             if (updated) this.Updated = DateTime.UtcNow.AddHours(3);
   78:  
   79:             return updated;
   80:         }
   81:  
   82:         ////////////////////////////////////////////////////////////////////////////
   83:         ////////////////////////////////////////////////////////////////////////////
   84:  
   85:         ////////////////////////////////////////////////////////////////////////////
   86:  
   87:         /// <summary>
   88:         ///
   89:         /// </summary>
   90:         public static Ia.Cl.Model.Db.Default.PersistentStorageState CreateOrUpdate<T>(Guid guid, T t)
   91:         {
   92:             return Write<T>(guid, t);
   93:         }
   94:  
   95:         ////////////////////////////////////////////////////////////////////////////
   96:  
   97:         /// <summary>
   98:         ///
   99:         /// </summary>
  100:         public static Ia.Cl.Model.Db.Default.PersistentStorageState Write<T>(Guid guid, T t)
  101:         {
  102:             Ia.Cl.Model.Db.Default.PersistentStorageState persistentStorageState;
  103:  
  104:             persistentStorageState = new Default.PersistentStorageState();
  105:  
  106:             var jsonSerializerOptions = new JsonSerializerOptions
  107:             {
  108:                 WriteIndented = true
  109:             };
  110:  
  111:             /*
  112:             using (var db = new Ia.Cl.Model.IaDbContext())
  113:             {
  114:                 var json = JsonSerializer.Serialize<T>(t, jsonSerializerOptions);
  115: 
  116:                 var newObject = new Ia.Cl.Model.Db.Object
  117:                 {
  118:                     Id = guid,
  119:                     Content = json
  120:                 };
  121: 
  122:                 var @object = (from o in db.Objects where o.Id == guid select o).SingleOrDefault();
  123: 
  124:                 if (@object == null)
  125:                 {
  126:                     db.Objects.Add(newObject);
  127: 
  128:                     persistentStorageState = Ia.Cl.Model.Db.Default.PersistentStorageState.Created;
  129:                 }
  130:                 else
  131:                 {
  132:                     if (@object.Update(newObject))
  133:                     {
  134:                         db.Objects.Attach(@object);
  135:                         db.Entry(@object).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  136: 
  137:                         persistentStorageState = Ia.Cl.Model.Db.Default.PersistentStorageState.Updated;
  138:                     }
  139:                     else
  140:                     {
  141:                         persistentStorageState = Ia.Cl.Model.Db.Default.PersistentStorageState.None;
  142:                     }
  143:                 }
  144: 
  145:                 db.SaveChanges();
  146:             }
  147:             */
  148:  
  149:             return persistentStorageState;
  150:         }
  151:  
  152:         ////////////////////////////////////////////////////////////////////////////
  153:  
  154:         /// <summary>
  155:         ///
  156:         /// </summary>
  157:         public static T Read<T>(Guid guid)
  158:         {
  159:             T t;
  160:  
  161:             /*
  162:             using (var db = new Ia.Cl.Model.IaDbContext())
  163:             {
  164:                 var item = (from o in db.Objects where o.Id == guid select o).SingleOrDefault();
  165: 
  166:                 if (item != null)
  167:                 {
  168:                     var json = item.Content;
  169: 
  170:                     if (!string.IsNullOrEmpty(json))
  171:                     {
  172:                         t = Deserialize<T>(json);
  173:                     }
  174:                     else t = default;
  175:                 }
  176:                 else t = default;
  177:             }
  178:             */
  179:  
  180:             t = default;
  181:  
  182:             return t;
  183:         }
  184:  
  185:         /*
  186:         ////////////////////////////////////////////////////////////////////////////
  187: 
  188:         /// <summary>
  189:         ///
  190:         /// </summary>
  191:         public static List<Ia.Cl.Model.Db.Object> ListByProjectId(int projectId)
  192:         {
  193:             List<Ia.Cl.Model.Db.Object> list;
  194: 
  195:             using (var db = new Ia.Cl.Model.IaDbContext())
  196:             {
  197:                 list = (from o in db.Objects where o.Id == Guid.Empty select o).ToList();
  198:             }
  199: 
  200:             return list;
  201:         }
  202: 
  203:         ////////////////////////////////////////////////////////////////////////////
  204: 
  205:         /// <summary>
  206:         ///
  207:         /// </summary>
  208:         public static List<Ia.Cl.Model.Db.Object> ListByTypeId(int typeId)
  209:         {
  210:             List<Ia.Cl.Model.Db.Object> list;
  211: 
  212:             using (var db = new Ia.Cl.Model.IaDbContext())
  213:             {
  214:                 list = (from o in db.Objects where o.Id == Guid.Empty select o).ToList();
  215:             }
  216: 
  217:             return list;
  218:         }
  219: 
  220:         ////////////////////////////////////////////////////////////////////////////
  221: 
  222:         /// <summary>
  223:         ///
  224:         /// </summary>
  225:         public static void DeleteByProjectId(int projectId)
  226:         {
  227:             using (var db = new Ia.Cl.Model.IaDbContext())
  228:             {
  229:                 var list = from o in db.Objects where o.Id == Guid.Empty select o;
  230: 
  231:                 db.Objects.RemoveRange(list);
  232:                 db.SaveChanges();
  233:             }
  234:         }
  235:         */
  236:  
  237:         ////////////////////////////////////////////////////////////////////////////
  238:  
  239:         /// <summary>
  240:         ///
  241:         /// </summary>
  242:         private static T Deserialize<T>(string json)
  243:         {
  244:             T t;
  245:  
  246:             if (!string.IsNullOrEmpty(json))
  247:             {
  248:                 t = JsonSerializer.Deserialize<T>(json);
  249:             }
  250:             else t = default;
  251:  
  252:             return t;
  253:         }
  254:  
  255:         ////////////////////////////////////////////////////////////////////////////
  256:         ////////////////////////////////////////////////////////////////////////////
  257:     }
  258:  
  259:     ////////////////////////////////////////////////////////////////////////////
  260:     ////////////////////////////////////////////////////////////////////////////
  261: }