/// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static DAO.MessageContentInfo ToDao(this MessageContentInfo obj, DAO.Message msg)
        {
            if (obj == null)
            {
                return(null);
            }

            #region Validate parameters
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }
            #endregion

            var dao = new DAO.MessageContentInfo();
            dao.Comment  = obj.Comment;
            dao.FileSize = obj.FileSize;
            dao.Length   = obj.Length;
            dao.LINK     = obj.LINK;
            dao.Message  = msg;
            dao.Name     = obj.Name;
            dao.Type     = obj.Type;

            return(dao);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static DAO.MessageProperty ToDao(this MessageProperty obj, DAO.Message msg)
        {
            if (obj == null)
            {
                return(null);
            }

            #region Validate parameters
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }
            #endregion

            var dao = new DAO.MessageProperty();
            dao.Comment = obj.Comment;
            dao.Format  = obj.Format;
            dao.LINK    = obj.LINK;
            dao.Message = msg;
            dao.Name    = obj.Name;
            dao.Type    = obj.Type;
            dao.Value   = obj.Value;

            return(dao);
        }
        /// <summary>
        /// Сохранить сообщение.
        /// </summary>
        /// <param name="msg"></param>
        public virtual void SaveMessage(Message msg)
        {
            #region Validate parameters
            if (msg == null)
            {
                throw new ArgumentNullException("msg");
            }
            #endregion

            DAO.Message dao = msg.ToDao();

            using (UnitOfWork work = BeginWork())
            {
                if (dao.LINK == 0)
                {
                    work.Save(dao);
                }
                else
                {
                    work.Update <DAO.Message>(ref dao);
                }

                work.End();
            }

            dao.CloneTo(msg);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dao"></param>
        /// <returns></returns>
        public static Message ToObj(this DAO.Message dao)
        {
            if (dao == null)
            {
                return(null);
            }

            var obj = new Message();

            dao.CloneTo(obj);

            return(obj);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dao"></param>
        /// <param name="obj"></param>
        public static void CloneTo(this DAO.Message dao, Message obj)
        {
            #region Validate parameters
            if (dao == null)
            {
                throw new ArgumentNullException("dao");
            }

            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            #endregion

            obj.LINK      = dao.LINK;
            obj.Async     = (dao.Async == null ? false : dao.Async.Value);
            obj.Body      = dao.Body.ToObj();
            obj.Channel   = dao.Channel;
            obj.Class     = dao.Class;
            obj.Contents  = dao.Contents.Select(contentInfo => contentInfo.ToObj()).ToArray();
            obj.CorrLINK  = dao.CorrLINK;
            obj.CorrGUID  = dao.CorrGUID;
            obj.Date      = dao.Date;
            obj.Direction = dao.Direction;
            obj.From      = dao.From;
            obj.GUID      = dao.GUID;
            obj.Name      = dao.Name;
            //obj.Posts = dao.Posts.Select(post => post.ToObj()).ToArray();
            obj.Priority   = dao.Priority;
            obj.Properties = dao.Properties.Select(prop => prop.ToObj()).ToArray();
            obj.Proxy      = dao.Proxy;
            //obj.Queue = dao.Queue;
            obj.Status  = dao.Status.ToObj();
            obj.Subject = dao.Subject;
            obj.To      = dao.To;
            obj.TTL     = dao.TTL;
            obj.Type    = dao.Type;
            obj.Version = dao.Version;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static DAO.Message ToDao(this Message obj)
        {
            if (obj == null)
            {
                return(null);
            }

            var dao = new DAO.Message();

            dao.Async     = obj.Async ? obj.Async : new Nullable <bool>();
            dao.Body      = obj.Body.ToDao();
            dao.Channel   = (String.IsNullOrEmpty(obj.Channel) ? null : obj.Channel);
            dao.Class     = (String.IsNullOrEmpty(obj.Class) ? null : obj.Class);
            dao.Contents  = obj.Contents.Select(contentInfo => contentInfo.ToDao(dao)).ToList();
            dao.CorrLINK  = obj.CorrLINK;
            dao.CorrGUID  = (String.IsNullOrEmpty(obj.CorrGUID) ? null : obj.CorrGUID);
            dao.Date      = obj.Date;
            dao.Direction = (String.IsNullOrEmpty(obj.Direction) ? null : obj.Direction);
            dao.From      = (String.IsNullOrEmpty(obj.From) ? null : obj.From);
            dao.GUID      = obj.GUID;
            dao.LINK      = obj.LINK;
            dao.Name      = (String.IsNullOrEmpty(obj.Name) ? null : obj.Name);
            //dao.Posts = obj.Posts.Select(post => post.ToDao(dao)).ToList();
            dao.Priority   = (obj.Priority == 0 ? new Nullable <int>() : obj.Priority);
            dao.Properties = obj.Properties.Select(prop => prop.ToDao(dao)).ToList();
            dao.Proxy      = (String.IsNullOrEmpty(obj.Proxy) ? null : obj.Proxy);
            //dao.Queue = obj.Queue;
            dao.Status  = obj.Status.ToDao();
            dao.Subject = (String.IsNullOrEmpty(obj.Subject) ? null : obj.Subject);
            dao.To      = (String.IsNullOrEmpty(obj.To) ? null : obj.To);
            dao.TTL     = obj.TTL;
            dao.Type    = (String.IsNullOrEmpty(obj.Type) ? null : obj.Type);
            dao.Version = (String.IsNullOrEmpty(obj.Version) ? null : obj.Version);

            return(dao);
        }