コード例 #1
0
        /// <summary>
        /// Creates the select list for.
        /// </summary>
        /// <typeparam name="T">Type of enum.</typeparam>
        /// <param name="selected">The selected.</param>
        /// <param name="title">The title.</param>
        /// <param name="value">Value of title.</param>
        /// <returns>
        /// Select list with enum possible values
        /// </returns>
        public static IList <SelectListItem> CreateSelectListItemsFor <T>(T selected, string title = null, string value = null)
        {
            Type  type    = typeof(T);
            var   items   = new List <SelectListItem>();
            Array options = null;

            if (IsNullable(selected, type))
            {
                items.Add(new SelectListItem {
                    Text = title, Value = value, Selected = object.Equals(selected, null)
                });
                options = Enum.GetValues(Nullable.GetUnderlyingType(type));
            }
            else
            {
                options = Enum.GetValues(type);
            }

            foreach (var option in options)
            {
                int numericValue   = Convert.ToInt32(option);
                var selectListItem = new SelectListItem {
                    Text = EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(option).Name, Value = numericValue.ToString(), Selected = object.Equals(selected, option)
                };
                items.Add(selectListItem);
            }

            return(items);
        }
コード例 #2
0
        /// <summary>
        /// Enums the value info dropdown internal.
        /// </summary>
        /// <param name="html">The html. </param>
        /// <param name="expression"> The expression. </param>
        /// <param name="value"> The value. </param>
        /// <param name="isNullable"> The is Nullable. </param>
        /// <param name="choices"> The choices. </param>
        /// <returns>
        /// Mvc string
        /// </returns>
        internal static MvcHtmlString DropDownListEnumExtendedInfoInternal(
            HtmlHelper html,
            string expression,
            object value,
            bool isNullable,
            IEnumerable choices)
        {
            StringBuilder build = new StringBuilder();

            build.AppendFormat("<select name='{0}'>", html.Encode(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression)));
            if (choices != null)
            {
                if (isNullable)
                {
                    build.AppendOption(string.Empty, string.Empty, value == null);
                }

                foreach (var choice in choices)
                {
                    build.AppendOption(
                        Convert.ToInt32(choice),
                        html.Encode(EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(choice).Name),
                        object.Equals(value, choice));
                }
            }

            build.Append("</select>");
            return(MvcHtmlString.Create(build.ToString()));
        }
コード例 #3
0
        public void StringValue_SaveOrUpdateCopyPersistance()
        {
            var enumField = ExtendedTestEnum.Something;

            Assert.That(EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(enumField).CustomValue, Is.TypeOf <string>());

            var model = new EnumExtendedModel();

            model.SampleEnum         = enumField;
            model.SampleNullableEnum = ExtendedTestEnum.Misc;

            this.Session.Save(model);
            this.Session.Evict(model);

            this.Session.SaveOrUpdateCopy(model);
            this.Session.Flush();
            this.Session.Clear();

            var modelFromDb = this.Session.Get <EnumExtendedModel>(model.Id);

            Assert.That(modelFromDb.SampleEnum, Is.EqualTo(enumField));

            model.SampleEnum = ExtendedTestEnum.Pending;
            this.Session.SaveOrUpdateCopy(model);
            this.Session.Flush();
            this.Session.Clear();

            var modelFromDbTwo = this.Session.Get <EnumExtendedModel>(model.Id);

            Assert.That(modelFromDbTwo.SampleEnum, Is.EqualTo(ExtendedTestEnum.Pending));
        }
コード例 #4
0
        /// <summary>
        /// Nulls the safe get.
        /// </summary>
        /// <param name="rs">The data reader.</param>
        /// <param name="names">The column names.</param>
        /// <param name="owner">The object owner.</param>
        /// <returns>Object converted from database</returns>
        public override object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            int result = 0;

            var valueInDatabase = rs[names[0]];

            if (valueInDatabase is DBNull)
            {
                return(default(TEnum));
            }

            var stringvalueInDatabase = (string)rs[names[0]];

            if (string.IsNullOrEmpty(stringvalueInDatabase))
            {
                return(default(TEnum));
            }

            foreach (string value in stringvalueInDatabase.Split(new[] { this.separator }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (string.IsNullOrEmpty(value))
                {
                    continue;
                }

                object valueInInt = EnumExtendedInfoAttribute.GetEnumValueByCustomValue <TEnum>(value);
                result = result + (int)valueInInt;
            }

            return(Enum.ToObject(typeof(TEnum), result));
        }
        /// <summary>
        /// Retrieve an instance of the mapped class from a JDBC resultset.
        /// Implementors should handle possibility of null values.
        /// </summary>
        /// <param name="rs">a IDataReader</param>
        /// <param name="names">column names</param>
        /// <param name="owner">the containing entity</param>
        /// <returns>Object converted from database</returns>
        /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception>
        public override object NullSafeGet(global::System.Data.IDataReader rs, string[] names, object owner)
        {
            object value = rs[names[0]];

            if (value == DBNull.Value)
            {
                return(null);
            }

            return(EnumExtendedInfoAttribute.GetEnumValueByCustomValue <TEnum>(value));
        }
コード例 #6
0
        /// <summary>
        /// Write an instance of the mapped class to a prepared statement.
        /// Implementors should handle possibility of null values.
        /// A multi-column type should be written to parameters starting from index.
        /// </summary>
        /// <param name="cmd">a IDbCommand</param>
        /// <param name="value">the object to write</param>
        /// <param name="index">command parameter index</param>
        /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception>
        public override void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            if (value != null)
            {
                if (value is TEnum)
                {
                    var    customValues = EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(value).GetCustomValues();
                    string result       = null;

                    if (customValues.Length != 0)
                    {
                        result = string.Join(this.separator, Array.ConvertAll(customValues, i => i.ToString()));
                    }

                    NHibernateUtil.String.NullSafeSet(cmd, result, index);
                }
            }
        }
 /// <summary>
 /// Write an instance of the mapped class to a prepared statement.
 /// Implementors should handle possibility of null values.
 /// A multi-column type should be written to parameters starting from index.
 /// </summary>
 /// <param name="cmd">a IDbCommand</param>
 /// <param name="value">the object to write</param>
 /// <param name="index">command parameter index</param>
 /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception>
 public override void NullSafeSet(global::System.Data.IDbCommand cmd, object value, int index)
 {
     ((IDataParameter)cmd.Parameters[index]).Value =
         EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(value).CustomValue ?? DBNull.Value;
 }
コード例 #8
0
 /// <summary>
 /// Write an instance of the mapped class to a prepared statement.
 /// Implementors should handle possibility of null values.
 /// A multi-column type should be written to parameters starting from index.
 /// </summary>
 /// <param name="cmd">a IDbCommand</param>
 /// <param name="value">the object to write</param>
 /// <param name="index">command parameter index</param>
 /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception>
 public virtual void NullSafeSet(IDbCommand cmd, object value, int index)
 {
     ((IDataParameter)cmd.Parameters[index]).Value =
         EnumExtendedInfoAttribute.GetExtendedInfoByEnumValue(value).CustomValue;
 }
コード例 #9
0
        /// <summary>
        /// Retrieve an instance of the mapped class from a JDBC resultset.
        /// Implementors should handle possibility of null values.
        /// </summary>
        /// <param name="rs">a IDataReader</param>
        /// <param name="names">column names</param>
        /// <param name="owner">the containing entity</param>
        /// <returns>Object converted from database</returns>
        /// <exception cref="T:NHibernate.HibernateException">HibernateException</exception>
        public virtual object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            object value = rs[names[0]];

            return(EnumExtendedInfoAttribute.GetEnumValueByCustomValue <TEnum>(value));
        }