示例#1
0
        public object Find(Type entityType, object primaryKeValue)
        {
            if (primaryKeValue == null)
            {
                return(null);
            }
            if (AttrHelper.IsSimple(primaryKeValue.GetType()) == false)
            {
                if (primaryKeValue.GetType() != entityType)
                {
                    throw new InvalidDataException("Find by pk whitch is not simple and not same type");
                }
                primaryKeValue = ModelHelper.GetPKValue(primaryKeValue);
                if (primaryKeValue == null)
                {
                    throw new InvalidDataException("Find by pk whitch is not simple and not have pk");
                }
            }
            object       o     = null;
            PropertyInfo pPK   = AttrHelper.GetProperty <JPrimaryKey>(entityType);
            SData        sdata = GetSData(entityType);

            sdata.PkCache.TryGetValue(primaryKeValue, out o);
            return(o);
        }
示例#2
0
        private void SaveEntityData(SData s)
        {
            object list = s.DataList;

            SaveEntityDataLocal((IList)list, s.DataType, null);
            s.Modified = false;
        }
示例#3
0
        private SData LoadEntityFromDisk(Type t)
        {
            JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(t);

            if (entityAttr.DsType != typeof(JsonDs))
            {
                throw new InvalidOperationException("Not a json file stored entity");
            }

            //long tstart = DateTime.Now.Ticks;
            SData sdata = new SData()
            {
                DataType = t
            };
            Type   lt       = typeof(List <>);
            Type   listType = lt.MakeGenericType(t);
            object list     = null;

            string   filename = GetDataFilePathForType(t);
            FileInfo fileInfo = new FileInfo(filename);

            if (fileInfo.Exists)
            {
                list = JsonSerializeHelper.LoadForType(filename, listType);
            }
            else
            {
                list = Activator.CreateInstance(listType);
            }
            foreach (var x in (IList)list)
            {
                INotifyPropertyChanged notifier = x as INotifyPropertyChanged;
                if (notifier != null)
                {
                    notifier.PropertyChanged += Notifier_PropertyChanged;
                }
            }
            PropertyInfo pkProp = AttrHelper.GetProperty <JPrimaryKey>(t);

            if (pkProp != null)
            {
                foreach (var o in (IList)list)
                {
                    object pk = pkProp.GetValue(o);
                    try
                    {
                        sdata.PkCache.Add(pk, o);
                    }
                    catch (ArgumentException ex)
                    {
                        Log.ProcessDebug("Dublicate pk entity: " + t + " pk value:  " + pk + " Error text: " + ex);
                    }
                }
            }
            sdata.DataList = list;
            sdatas.Add(sdata);

            return(sdata);
        }
示例#4
0
        public void SaveEntityData(Type type)
        {
            SData s = GetSData(type);

            if (s != null)
            {
                SaveEntityData(s);
            }
        }
示例#5
0
        private void Notifier_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            SData sdata = GetSData(sender.GetType());

            if (sdata != null)
            {
                sdata.Modified = true;
            }
        }
示例#6
0
        public void SetEntityModified(Type t)
        {
            SData sdata = GetSData(t);

            if (sdata != null)
            {
                sdata.Modified = true;
            }
        }
示例#7
0
        public bool IsEntityModified(Type t)
        {
            SData sdata = GetSData(t);

            if (sdata != null)
            {
                return(sdata.Modified);
            }
            else
            {
                return(false);
            }
        }
示例#8
0
        private SData GetSData(Type t)
        {
            SData sdata = null;

            foreach (var s in sdatas)
            {
                if (s.DataType.Equals(t))
                {
                    sdata = s;
                    break;
                }
            }
            return(sdata);
        }
示例#9
0
        public void GenNextPkValue(object o)
        {
            Type         t   = o.GetType();
            PropertyInfo pPK = AttrHelper.GetProperty <JPrimaryKey>(t);

            if (pPK != null)
            {
                if (AttrHelper.IsAttributePresent <JAutoIncrement>(t, pPK.Name))
                {
                    //string maxValue = DataUtils.genKey(null);
                    IList list     = FindAll(t);
                    SData sdata    = GetSData(t);
                    int   maxValue = 0;
                    if (sdata.MaxId == null)
                    {
                        foreach (var l in list)
                        {
                            var pkValue  = pPK.GetValue(l);
                            int curValue = int.Parse(pkValue.ToString());
                            if (curValue > maxValue)
                            {
                                maxValue = curValue;
                            }
                        }
                        sdata.MaxId = maxValue;
                    }
                    sdata.MaxId = sdata.MaxId + 1;
                    if (pPK.PropertyType == typeof(string))
                    {
                        pPK.SetValue(o, sdata.MaxId.ToString());
                    }
                    else
                    {
                        pPK.SetValue(o, sdata.MaxId);
                    }
                }
            }
        }
示例#10
0
        //todo Update Cache when user modify pk
        #endregion
        public IList InitiallyLoadData(Type t)
        {
            SData sdata = LoadEntityFromDisk(t);

            return((IList)sdata.DataList);
        }
示例#11
0
        private IList GetJsonList(Type t)
        {
            SData sdata = GetSData(t);

            return((IList)sdata.DataList);
        }