1

我有一个简单的表单,它获取用户数据并将其保存到数据库。虽然它工作正常但似乎很重复..我不确定我是否可以以某种方式打开我的代码以检查用户输入并将其保存在数据库中。

这是我的代码..

public void SaveUserData()
      {
            MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails();
            MWCompetitionsEntryDetails entry = new MWCompetitionsEntryDetails();

            if (!IsNullOrEmpty(firstNameText.Value))
                  user.FirstName = firstNameText.Value;
            if (!IsNullOrEmpty(lastNameText.Value))
                  user.LastName = lastNameText.Value;
            if (!IsNullOrEmpty(emailText.Value))
                  user.UserEmailAddress = emailText.Value;
            if (!IsNullOrEmpty(address1Text.Value))
                  user.Address1= address1Text.Value;
            if (!IsNullOrEmpty(address2Text.Value))
                  user.Address2 = address2Text.Value;
            if (!IsNullOrEmpty(cityText.Value))
                  user.City = cityText.Value;
            if (!IsNullOrEmpty(provinceText.Value))
                  user.Province= provinceText.Value;
            if (!IsNullOrEmpty(postCodeText.Value))
                  user.PostalCode = postCodeText.Value;
            if (!IsNullOrEmpty(countryText.SelectedItem.Text))
                  user.Country = countryText.SelectedItem.Text;

            user.Save();
      }

      public static bool IsNullOrEmpty<T>(T value)
      {
            if (typeof(T) == typeof(string))
                  return string.IsNullOrEmpty(value as string);

            return value.Equals(default(T));
      } 

而不是在每个字段上寻找 IsNullOrEmpty(data) ,是否有任何建议可以使用 Linq 或任何东西改进我的代码..

非常感谢您的时间...

4

3 回答 3

2

您可以在属性的设置器中检查 If null 或 empty:

public class MWCompetitionContestantsDetails
{
    private string _firstName;

    public string FirstName
    {
        get
        {
            return this._firstName;
        }
        set
        {
            if(!IsNullOrEmpty(value))
            {
                this._firstName = value;
            }
        }
    }       

    .........

    public static bool IsNullOrEmpty<T>(T value)
    {
        if (typeof(T) == typeof(string))
              return string.IsNullOrEmpty(value as string);

        return value.Equals(default(T));
    } 
}
于 2013-05-15T10:04:25.627 回答
1

只要您的属性是字符串,您就可以使用对象初始化:

MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails(){
        FirstName = firstNameText.Value,
        LastName = lastNameText.Value,
        UserEmailAddress = emailText.Value,
        Address1= address1Text.Value,
        Address2 = address2Text.Value,
        City = cityText.Value,
        Province= provinceText.Value,
        PostalCode = postCodeText.Value,
        Country = countryText.SelectedItem.Text
};
user.Save();
于 2013-05-15T11:08:29.877 回答
1

首先,我想说我喜欢oniant's 和Adil Mammadov' 的解决方案,因为它们非常简单。
但是,我已经编写了我喜欢在类似情况下使用的花哨方法的代码,我想分享它:

      class Save<T> : ISave
    {
        private readonly System.Action<T> _assignValue;
        private readonly System.Func<T> _getValue;

        public void Do()
        {
            T value = _getValue();
            if (!IsNullOrEmpty(value))
            {
                _assignValue(value);
            }
        }

        public static Save<T> Value<T>(System.Func<T> getValue, System.Action<T> assignValue)
        {
            return new Save<T>(getValue, assignValue);
        }

        private Save(System.Func<T> getValue, System.Action<T> assignValue)
        {
            _getValue = getValue;
            _assignValue = assignValue;
        }

        private static bool IsNullOrEmpty<T>(T value)
        {
            if (typeof(T) == typeof(string))
                return string.IsNullOrEmpty(value as string);

            return value.Equals(default(T));
        }
    }

    internal interface ISave
    {
        void Do();
    }

    public static void SaveUserData()
    {
        MWCompetitionContestantsDetails user = new MWCompetitionContestantsDetails();
        MWCompetitionsEntryDetails entry = new MWCompetitionsEntryDetails();

        new List<ISave>
        {
            Save<string>.Value( () => firstNameText.Value, x => user.FirstName = x),
            Save<string>.Value( () => lastNameText.Value, x => user.LastName = x),              
            Save<int>.Value( () => age.Value, x => user.Age = x),// int's supported :)              
            // etc
        }
        .ForEach(x => x.Do());
        user.Save();
    }
于 2013-05-15T11:31:40.207 回答