Beispiel #1
0
        public DBCollection(Session session)
        {
            Type    = typeof(T);
            Mapping = new DBMapping(session.Assemblies, Type);

            IsSystemData = Type.GetCustomAttribute <UserObject>() == null;

            RaisePropertyChanges = IsSystemData;
            Session = session;

            ReadOnly = IsSystemData ? (Session.Mode & SessionMode.System) != SessionMode.System : (Session.Mode & SessionMode.Users) != SessionMode.Users;

            if (IsSystemData)
            {
                BindingList <T> binding = new BindingList <T>
                {
                    RaiseListChangedEvents = RaisePropertyChanges
                };

                binding.AddingNew += (o, e) => e.NewObject = CreateNew();
                Binding            = binding;
            }
            else
            {
                Binding = new List <T>();
            }
        }
Beispiel #2
0
        internal override void Load(byte[] data, DBMapping mapping)
        {
            using (MemoryStream mStream = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(mStream))
                {
                    Index = reader.ReadInt32();

                    int count = reader.ReadInt32();

                    for (int i = 0; i < count; i++)
                    {
                        T ob = new T {
                            Collection = this
                        };
                        Binding.Add(ob);

                        ob.RawData = reader.ReadBytes(reader.ReadInt32());
                        ob.Load(mapping);

                        if (ob.ForeignKeys != null)
                        {
                            Session.KeyedObjects.Enqueue(ob);
                        }

                        Dictionary[ob.Index] = ob;
                    }
                }
            VersionValid = mapping.IsMatch(Mapping);
        }
Beispiel #3
0
        public DBCollection(Session session)
        {
            Type    = typeof(T);
            Mapping = new DBMapping(Type);

            IsSystemData = Type.GetCustomAttribute <UserObject>() == null;

            RaisePropertyChanges           = IsSystemData;
            Binding.RaiseListChangedEvents = RaisePropertyChanges;
            Session = session;

            ReadOnly = IsSystemData ? (Session.Mode & SessionMode.System) == 0 : (Session.Mode & SessionMode.Users) == 0;

            Binding.AddingNew += (o, e) => e.NewObject = CreateNew();
        }
Beispiel #4
0
        internal void Load(DBMapping mapping)
        {
            using (MemoryStream mStream = new MemoryStream(RawData))
                using (BinaryReader reader = new BinaryReader(mStream))
                {
                    foreach (DBValue dbValue in mapping.Properties)
                    {
                        object value = dbValue.ReadValue(reader);

                        if (dbValue.Property == null)
                        {
                            continue;
                        }

                        if (dbValue.Property.PropertyType.IsSubclassOf(typeof(DBObject)))
                        {
                            if (ForeignKeys == null)
                            {
                                ForeignKeys = new Dictionary <PropertyInfo, int>();
                            }

                            ForeignKeys[dbValue.Property] = (int)value;
                            continue;
                        }

                        if (dbValue.PropertyType.IsEnum)
                        {
                            if (dbValue.PropertyType.GetEnumUnderlyingType() == dbValue.Property.PropertyType)
                            {
                                dbValue.Property.SetValue(this, value);
                                continue;
                            }
                        }
                        else if (dbValue.PropertyType == dbValue.Property.PropertyType)
                        {
                            dbValue.Property.SetValue(this, value);
                            continue;
                        }

                        try
                        {
                            dbValue.Property.SetValue(this, Convert.ChangeType(value, dbValue.PropertyType));
                        }
                        catch { }
                    }
                }
        }
Beispiel #5
0
        public bool IsMatch(DBMapping mapping)
        {
            if (Properties.Count != mapping.Properties.Count)
            {
                return(false);
            }

            for (int i = 0; i < Properties.Count; i++)
            {
                if (!Properties[i].IsMatch(mapping.Properties[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #6
0
        internal void Save(DBMapping mapping)
        {
            using (MemoryStream mStream = new MemoryStream())
                using (BinaryWriter writer = new BinaryWriter(mStream))
                {
                    foreach (DBValue dbValue in mapping.Properties)
                    {
                        if (dbValue.Property.PropertyType.IsSubclassOf(typeof(DBObject)))
                        {
                            DBObject linkOb = (DBObject)dbValue.Property.GetValue(this);
                            dbValue.WriteValue(linkOb?.Index ?? 0, writer);
                        }
                        else
                        {
                            dbValue.WriteValue(dbValue.Property.GetValue(this), writer);
                        }
                    }
                    RawData = mStream.ToArray();
                }

            OnSaved();
        }
Beispiel #7
0
        internal override void Load(byte[] data, DBMapping mapping)
        {
            VersionValid = mapping.IsMatch(Mapping);

            using (MemoryStream mStream = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(mStream))
                {
                    Index = reader.ReadInt32();

                    int count = reader.ReadInt32();

                    for (int i = 0; i < count; i++)
                    {
                        T ob = new T {
                            Collection = this
                        };
                        Binding.Add(ob);

                        ob.RawData = reader.ReadBytes(reader.ReadInt32());
                        ob.Load(mapping);
                    }
                }
        }
Beispiel #8
0
        internal void Load(DBMapping mapping)
        {
            using (MemoryStream mStream = new MemoryStream(RawData))
                using (BinaryReader reader = new BinaryReader(mStream))
                {
                    foreach (DBValue dbValue in mapping.Properties)
                    {
                        object value = dbValue.ReadValue(reader);

                        if (dbValue.Property == null)
                        {
                            continue;
                        }

                        if (dbValue.Property.PropertyType.IsSubclassOf(typeof(DBObject)))
                        {
                            if (!Collection.Session.Relationships.TryGetValue(dbValue.Property.PropertyType, out DBRelationship relationship))
                            {
                                lock (Collection.Session.Relationships)
                                    if (!Collection.Session.Relationships.TryGetValue(dbValue.Property.PropertyType, out relationship))
                                    {
                                        Collection.Session.Relationships[dbValue.Property.PropertyType] = relationship = new DBRelationship(dbValue.Property.PropertyType);
                                    }
                            }

                            if (!relationship.LinkTargets.TryGetValue((int)value, out DBRelationshipTargets targets))
                            {
                                lock (relationship)
                                    if (!relationship.LinkTargets.TryGetValue((int)value, out targets))
                                    {
                                        relationship.LinkTargets[(int)value] = targets = new DBRelationshipTargets();
                                    }
                            }

                            if (!targets.PropertyTargets.TryGetValue(dbValue.Property, out ConcurrentQueue <DBObject> list))
                            {
                                lock (targets)
                                    if (!targets.PropertyTargets.TryGetValue(dbValue.Property, out list))
                                    {
                                        targets.PropertyTargets[dbValue.Property] = list = new ConcurrentQueue <DBObject>();
                                    }
                            }

                            list.Enqueue(this);


                            continue;
                        }

                        if (dbValue.PropertyType.IsEnum)
                        {
                            if (dbValue.PropertyType.GetEnumUnderlyingType() == dbValue.Property.PropertyType)
                            {
                                dbValue.Property.SetValue(this, value);
                                continue;
                            }
                        }
                        else if (dbValue.PropertyType == dbValue.Property.PropertyType)
                        {
                            dbValue.Property.SetValue(this, value);
                            continue;
                        }

                        try
                        {
                            dbValue.Property.SetValue(this, Convert.ChangeType(value, dbValue.PropertyType));
                        }
                        catch { }
                    }
                }
        }
Beispiel #9
0
 internal abstract void Load(byte[] data, DBMapping mapping);