Tuesday, March 10, 2015

HUAWEI HG659 5G WLAN with iPad, iMac and MacBook Pro

Recently I received an HUAWEI HG695 router. It has a dual band wireless for 2.4 and 5 Ghz. But even the 5G was enabled on the router it did not show up on my wifi-list on my iMac, iPad and MacBook.
The answer lies in one advanced WLAN setting:


Change the bandwidth from 20Mhz to 20/40Mhz and all works like a charm.

Friday, March 06, 2015

JSON serialized object in NHibernate

The other day I needed to store some user settings into the database. The settings involved the properties of a chart, with title, axis, series, manual series etc. Normally there would be a entity for the chart, entities for the series with some kind of inheritance and then create all the mappings.

A lot of overhead for storing some settings, in case of a NoSQL database like MongoDB you just save the settings object.

I thought it would be nice to just map the settings object as a single column like:

 Property(p => p.Settings);

Therefore I developed a  JSONMappableType for NHibernate:

Property(p => p.Settingsm =>
        {
            m.Length(8096);
          m.Type<JsonMappableType<Settings>>();
        });

This serializes the complete Settings object to a JSON-string into a single column. And when retrieving from the database the Settings object is deserialized from the JSON-string.

I derived the mapping from NHibernates IUserType and used Newtonsoft.Json for the (de-)serialisation of the object. I had to override the DefaultContractResolver, because the order of the properties is random at each call which made object 'dirty' all the time. Ordering the properties by name solved that problem.


using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NHibernate;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
using System;
using System.Linq;
namespace My.Mappings
{
    public class JsonMappableType<T> :IUserType
    {
        public object Assemble(object cachedobject owner)
        {
            //Used for casching, as our object is immutable we can just return it as is
            return cached;
        }
        public object DeepCopy(object value)
        {
            //We deep copy the Translation by creating a new instance with the same contents
            if (value == null)
                return null;
            if (value.GetType() != typeof(T))
                return null;
            return FromJson(ToJson((T)value));
        }
        
       
        public object Disassemble(object value)
        {
            //Used for casching, as our object is immutable we can just return it as is
            return value;
        }
        public new bool Equals(object xobject y)
        {
            //Use json-query-string to see if their equal 
            // on value so we use this implementation
            if (x == null || y == null)
                return false;
            if (x.GetType() != typeof(T) || y.GetType() != typeof(T))
                return false;
            return ToJson((T)x).Equals(ToJson((T)y));
        }
        public int GetHashCode(object x)
        {
            if (x != null && x.GetType() == typeof(T))
                return ToJson((T)x).GetHashCode();
            return x.GetHashCode();
        }
        public bool IsMutable
        {
            get { return false; }
        }
        public object NullSafeGet(System.Data.IDataReader rsstring[] namesobject owner)
        {
            //We get the string from the database using the NullSafeGet used to get strings 
            string jsonString = (string)NHibernateUtil.String.NullSafeGet(rsnames[0]);
            //And save it in the T object. This would be the place to make sure that your string 
            //is valid for use with the T class
            return FromJson(jsonString);
        }
        public void NullSafeSet(System.Data.IDbCommand cmdobject valueint index)
        {
            //Set the value using the NullSafeSet implementation for string from NHibernateUtil
            if (value == null)
            {
                NHibernateUtil.String.NullSafeSet(cmdnullindex);
                return;
            }
            value = ToJson((T)value);
            NHibernateUtil.String.NullSafeSet(cmdvalueindex);
        }
        public object Replace(object originalobject targetobject owner)
        {
            //As our object is immutable we can just return the original
            return original;
        }
        public Type ReturnedType
        {
            get { return typeof(T); }
        }

        public SqlType[] SqlTypes
        {
            get
            {
                //We store our translation in a single column in the database that can contain a string
                SqlType[] types = new SqlType[1];
                types[0] = new SqlType(System.Data.DbType.String);
                return types;
            }
        }
        private static JsonSerializerSettings jsonSetting = new JsonSerializerSettings
        {
            ContractResolver = new OrderedContractResolver(),
            Formatting = Formatting.None,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            PreserveReferencesHandling = PreserveReferencesHandling.Objects,
            DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate
            
        };
        public static T FromJson(string jsonString)
        {
            if (!string.IsNullOrWhiteSpace(jsonString))
                return JsonConvert.DeserializeObject<T>(jsonStringjsonSetting);
            return Activator.CreateInstance<T>();
        }

        public static string ToJson(T obj)
        {
            return JsonConvert.SerializeObject(objjsonSetting);
        }
        /// <summary>
        /// Order the json-properties to create a simple equals case for JsonMappableType.Equals
        /// </summary>
        private class OrderedContractResolver : DefaultContractResolver
        {
            protected override System.Collections.Generic.IList<JsonPropertyCreateProperties(System.Type typeMemberSerialization memberSerialization)
            {
                return base.CreateProperties(typememberSerialization).OrderBy(p => p.PropertyName).ToList();
            }
        }
    }


}