Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading objects for a specified type fail #7

Open
marcofranssen opened this issue Mar 17, 2014 · 1 comment
Open

Loading objects for a specified type fail #7

marcofranssen opened this issue Mar 17, 2014 · 1 comment

Comments

@marcofranssen
Copy link

When calling the load Method T equals FeedItem. In the lambda I get an exception because it is read from db in a different object. This problem only occurs when the application is restarted on my Phone. As long as the application keeps running all queries and succeed.

//BaseDatabaseInstance.cs
public ITableDefinition CreateTableDefinition<T, TKey>(Func<T, TKey> keyFunction) where T : class, new()
        {
            return new TableDefinition<T, TKey>(Driver,
                                                ( key => _Load<T>( typeof( T ), key, new CycleCache() ).Result ),
                                                keyFunction);
        }

The call to _Load eventually reaches this code in SerializationHelper... Where on line 408 (last line in this snippet) the code creates an instance of type FeedItem. So Far so Good.

/// <summary>
        ///     Recursive load operation
        /// </summary>
        /// <param name="type">The type to save (passed to support NULL)</param>
        /// <param name="key">The associated key (for cycle detection)</param>
        /// <param name="br">The reader</param>
        /// <param name="cache">Cycle cache</param>
        public object Load(Type type, object key, BinaryReader br, CycleCache cache)
        {
            _logManager.Log(SterlingLogLevel.Verbose,
                            string.Format("Sterling is de-serializing type {0}", type.FullName), null);

            if (_DeserializeNull(br))
            {
                return null;
            }

            // make a template
            var instance = Activator.CreateInstance(type);

In this same method eventually we reach following code (line 455) Here the typeIndexer suddenly changes the type to Notification. This is where everything fails. Since Notification is a totally different type.

            else
            {
                type = Type.GetType(_typeIndexer(br.ReadInt32()));
                if (instance.GetType() != type)
                {
                    instance = Activator.CreateInstance(type);
                }

                // push to the stack
                cache.Add(type, instance, key);

                // build the reflection cache);
                if (!_propertyCache.ContainsKey(type))
                {
                    //_CacheProperties(type);
                    _CacheProperties(type);
                }
            }

This is my database format

public class MyDatabase : BaseDatabaseInstance
    {
        public const string FeedItemInstanceIdIndex = "FeedItemInstanceIdIndex";
        public const string NotificationInstanceIdIndex = "NotificationInstanceIdIndex";
        public const string NotificationUnreadIndex = "NotificationUnreadIndex";

        protected override List<ITableDefinition> RegisterTables()
        {
            return new List<ITableDefinition>
            {
                CreateTableDefinition<Instance, int>(i => i.Id),
                CreateTableDefinition<FeedItem, int>(f => f.Id)
                    .WithIndex<FeedItem, int, int>(FeedItemInstanceIdIndex, fi => fi.InstanceId),
                CreateTableDefinition<Notification, int>(n => n.Id)
                    .WithIndex<Notification, int, int>(NotificationInstanceIdIndex, n => n.InstanceId)
                    .WithIndex<Notification, bool, int>(NotificationUnreadIndex, n => n.Unread)
            };
        }
    }

Any Idea how to solve this?

@marcofranssen
Copy link
Author

After explaining the problem here I finally tried by removing the base class which is used for both the FeedItem and Notification. Then the serializer works.

So in order to solve this issue the serializer thingy should be enhanced to also support baseclasses correctly. Any idea where and how to solve the issue. With some little assistance I could fix it myself probably.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant