----------------------------------------------------------------------------------------------------------------------------

Meta data storage

Overview

The primary feature of a file storage is to hold certain bytes (string, objects, etc). When using NFileStorage myself for some time, I found out sometimes additional information about the core data needs to be stored. You can compare this a little bit with a traditional file system, which stores data, but also some 'surrounding' information, like when a file was created, or when it was accessed the last time. In NFileStorage will store some generic meta data, but also some custom metadata that you can define yourself.

If you want to define your own Custom metadata, you should make a class that derives the ICustomMetaData class;

namespace FileStorage.MetaData
{
    public interface ICustomMetaData : DynamiteXml.IDynamiteXml
    {
        string GetInfo();
    }
}

The IDynamiteXml interface we extend here is only a naming interface, so the only function you will have to implement, is the string GetInfo() method. This GetInfo() method is used by the Metadata subcommand of the CLI.

Predefined custom metadata classes

We have defined some default classes that implement the ICustomMetaData; the EmptyCustomMetaData and the KeyValueMetaData.

The EmptyCustomMetaData is used to indicate there is no custom meta data (its empty). This class is used when the 'null' value is passed when adding a new object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynamiteXml;

namespace FileStorage.MetaData.Helper
{
    /// <summary>
    /// Empty custom meta data helper class, will be used as the default customer metadata, when the
    /// code specifies 'null' as custom meta data. 
    /// </summary>
    [Serializable]
    public class EmptyCustomMetaData : ICustomMetaData
    {
        /// <summary>
        /// Empty constructor is required for de-serialization
        /// </summary>
        public EmptyCustomMetaData()
        {
        }

        #region ICustomMetaData Members

        public string GetInfo()
        {
            return "{Empty; no meta data}";
        }

        #endregion
    }
}

The KeyValueMetaData class is a class that holds a dictionary of string keys and string values connected to each string, which are serialiazable (by default a dictionary is not serializable). You can use this class to easily add any number of key/value combinations for your use.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DynamiteXml;

namespace FileStorage.MetaData.Helper
{
    /// <summary>
    /// KeyValueMetaData helper class, can be used to store string key/value pairs as custom meta data
    /// </summary>
    [Serializable]
    public class KeyValueMetaData : ICustomMetaData
    {
        /// <summary>
        /// Empty constructor is required for de-serialization
        /// </summary>
        public KeyValueMetaData()
        {
        }

        // note we explicitly us the SerializableDictionary
        private SerializableDictionary<string, string> _it = new SerializableDictionary<string, string>();

        public SerializableDictionary<string, string> KeyValues
        { 
            get
            {
                return _it;
            }
            set
            {
                _it = value;
            }
        }

        #region ICustomMetaData Members

        public string GetInfo()
        {
            var sb = new StringBuilder();
            foreach (var pair in KeyValues)
            {
                sb.AppendLine(String.Format("[Key {0} -> Value {1}]", pair.Key, pair.Value));
            }
            return sb.ToString();
        }

        #endregion
    }
}

If the default two classes are not sufficient, you can create your own class that hold a specific set of properties you want to persist as Custom meta data.

Related pages


----------------------------------------------------------------------------------------------------------------------------

Last edited May 30, 2009 at 10:20 AM by barkgj, version 7

Comments

No comments yet.