Descriptor

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/albert528108/article/details/52302737
/// <summary>
    /// Descriptor information.
    /// </summary>
    public class Descriptor<K> {

        /// <summary>
        /// Key.
        /// </summary>
        public K Key { get; set; }

        /// <summary>
        /// Name.
        /// </summary>
        public String Name { get; set; }

        /// <summary>
        /// Description
        /// </summary>
        public String Description { get; set; }

        /// <summary>
        /// Get descriptor caption.
        /// </summary>
        public String Caption {
            get {
                if (String.IsNullOrEmpty(Description))
                    return Name;

                return String.Format("{0} - {1}", Name, Description);
            }
        }

        /// <summary>
        /// To String
        /// </summary>
        /// <returns>Default string display on binding list</returns>
        public override string ToString() {
            return Caption;
        }

        /// <summary>
        /// Equals
        /// </summary>
        /// <param name="obj">Equals object</param>
        /// <returns>When object has same key return true else return false</returns>
        public override bool Equals(object obj) {
            if (obj is Descriptor<K>) {
                return ((Descriptor<K>)obj).Key.Equals(Key);
            }

            return base.Equals(obj);
        }

        /// <summary>
        /// Get hashcode.
        /// </summary>
        /// <returns>Hashcode</returns>
        public override int GetHashCode() {
            return Key.GetHashCode();
        }
    }


        /// <summary>
        /// Get all resource descriptors.
        /// </summary>
        /// <returns>Resource descriptor list</returns>
        public static async Task<IList<Descriptor<Int32>>> GetAllDescriptors() {
            using (MesproContext db = GetContext()) {
                return await db.Resources
                               .OrderBy(m => m.Name)
                               .Select(m => new Descriptor<Int32>() { Key = m.ID, Name = m.Name, Description = m.Description })
                               .ToListAsync();
            }
        }


展开阅读全文

没有更多推荐了,返回首页