)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Object (Ia.Cl.Models.Db)

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.Models.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.Models.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.Models.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.Models.Db.Default.PersistentStorageState Write<T>(Guid guid, T t)
  101:         {
  102:             Ia.Cl.Models.Db.Default.PersistentStorageState persistentStorageState;
  103:  
  104:             var jsonSerializerOptions = new JsonSerializerOptions
  105:             {
  106:                 WriteIndented = true
  107:             };
  108:  
  109:             using (var db = new Ia.Cl.Models.IaDbContext())
  110:             {
  111:                 var json = JsonSerializer.Serialize<T>(t, jsonSerializerOptions);
  112:  
  113:                 var newObject = new Ia.Cl.Models.Db.Object
  114:                 {
  115:                     Id = guid,
  116:                     Content = json
  117:                 };
  118:  
  119:                 var @object = (from o in db.Objects where o.Id == guid select o).SingleOrDefault();
  120:  
  121:                 if (@object == null)
  122:                 {
  123:                     db.Objects.Add(newObject);
  124:  
  125:                     persistentStorageState = Ia.Cl.Models.Db.Default.PersistentStorageState.Created;
  126:                 }
  127:                 else
  128:                 {
  129:                     if (@object.Update(newObject))
  130:                     {
  131:                         db.Objects.Attach(@object);
  132:                         db.Entry(@object).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  133:  
  134:                         persistentStorageState = Ia.Cl.Models.Db.Default.PersistentStorageState.Updated;
  135:                     }
  136:                     else
  137:                     {
  138:                         persistentStorageState = Ia.Cl.Models.Db.Default.PersistentStorageState.None;
  139:                     }
  140:                 }
  141:  
  142:                 db.SaveChanges();
  143:             }
  144:  
  145:             return persistentStorageState;
  146:         }
  147:  
  148:         ////////////////////////////////////////////////////////////////////////////
  149:  
  150:         /// <summary>
  151:         ///
  152:         /// </summary>
  153:         public static T Read<T>(Guid guid)
  154:         {
  155:             T t;
  156:  
  157:             using (var db = new Ia.Cl.Models.IaDbContext())
  158:             {
  159:                 var item = (from o in db.Objects where o.Id == guid select o).SingleOrDefault();
  160:  
  161:                 if (item != null)
  162:                 {
  163:                     var json = item.Content;
  164:  
  165:                     if (!string.IsNullOrEmpty(json))
  166:                     {
  167:                         t = Deserialize<T>(json);
  168:                     }
  169:                     else t = default;
  170:                 }
  171:                 else t = default;
  172:             }
  173:  
  174:             return t;
  175:         }
  176:  
  177:         /*
  178:         ////////////////////////////////////////////////////////////////////////////
  179: 
  180:         /// <summary>
  181:         ///
  182:         /// </summary>
  183:         public static List<Ia.Cl.Models.Db.Object> ListByProjectId(int projectId)
  184:         {
  185:             List<Ia.Cl.Models.Db.Object> list;
  186: 
  187:             using (var db = new Ia.Cl.Models.IaDbContext())
  188:             {
  189:                 list = (from o in db.Objects where o.Id == Guid.Empty select o).ToList();
  190:             }
  191: 
  192:             return list;
  193:         }
  194: 
  195:         ////////////////////////////////////////////////////////////////////////////
  196: 
  197:         /// <summary>
  198:         ///
  199:         /// </summary>
  200:         public static List<Ia.Cl.Models.Db.Object> ListByTypeId(int typeId)
  201:         {
  202:             List<Ia.Cl.Models.Db.Object> list;
  203: 
  204:             using (var db = new Ia.Cl.Models.IaDbContext())
  205:             {
  206:                 list = (from o in db.Objects where o.Id == Guid.Empty select o).ToList();
  207:             }
  208: 
  209:             return list;
  210:         }
  211: 
  212:         ////////////////////////////////////////////////////////////////////////////
  213: 
  214:         /// <summary>
  215:         ///
  216:         /// </summary>
  217:         public static void DeleteByProjectId(int projectId)
  218:         {
  219:             using (var db = new Ia.Cl.Models.IaDbContext())
  220:             {
  221:                 var list = from o in db.Objects where o.Id == Guid.Empty select o;
  222: 
  223:                 db.Objects.RemoveRange(list);
  224:                 db.SaveChanges();
  225:             }
  226:         }
  227:         */
  228:  
  229:         ////////////////////////////////////////////////////////////////////////////
  230:  
  231:         /// <summary>
  232:         ///
  233:         /// </summary>
  234:         private static T Deserialize<T>(string json)
  235:         {
  236:             T t;
  237:  
  238:             if (!string.IsNullOrEmpty(json))
  239:             {
  240:                 t = JsonSerializer.Deserialize<T>(json);
  241:             }
  242:             else t = default;
  243:  
  244:             return t;
  245:         }
  246:  
  247:         ////////////////////////////////////////////////////////////////////////////
  248:         ////////////////////////////////////////////////////////////////////////////
  249:     }
  250:  
  251:     ////////////////////////////////////////////////////////////////////////////
  252:     ////////////////////////////////////////////////////////////////////////////
  253: }