有没有任何linqish方法来保存用户表单数据 - 重新考虑我的代码

时间:2013-05-15 09:54:56

标签: c# asp.net linq

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

这是我的代码..

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(数据),是否有任何建议使用Linq或任何东西改进我的代码..

你的时间非常感谢...

3 个答案:

答案 0 :(得分: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));
    } 
}

答案 1 :(得分: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();

答案 2 :(得分:1)

首先,我想说我喜欢oniantAdil 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();
    }