0

My application is huge and after one year we've found that customers "like" inserting whitespaces in their names or surnames... Is there any simple way to remove whitespaces from those strings (and not searching for every line in program) or I have to search my whole code and change every line which sets Osoba? Wanted to do set { Osoba = value.Trim(): } but I also had to assign get which I can't do in model's property.

public class Event
    {
        [Key]
        public int EventID { get; set; }

        public DateTime AddTime { get; set; }

        public DateTime Start { get; set; }

        public DateTime End { get; set; }

        public EventCreateType EventCreateType { get; set; }

        public EventStatus Status { get; set; }

        public string Osoba { get; set; }

        (...)
    } 
Cezar
  • 345
  • 6
  • 18
  • Use `String.Trim()` inside the http post action method which handles the form submit and save the trimmed data to database – Shyju May 30 '18 at 16:47
  • That's the point, but what if there are 100 http post actions? Is there any simple way? – Cezar May 30 '18 at 16:48
  • Write helper methods for each of them. For example : Each view model may have a method called `TrimValues()` which trims it's property values. – Shyju May 30 '18 at 16:49
  • You wants to change the old data or the new data which is going to be inserted in db? – Samaritan_Learner May 30 '18 at 16:49
  • New data. Old ones I've already replaced. – Cezar May 30 '18 at 16:50
  • Maybe you could implement it as a setter? I don't know if that would work though. – Daxtron2 May 30 '18 at 16:52
  • @TJ Wolschon I can't. If i set a setter, then I have to set a getter. This can't be set in same property without referencing to another property. – Cezar May 30 '18 at 16:53
  • `get { return this.Osoba; } set { this.Osoba = value.Trim(); }` – Daxtron2 May 30 '18 at 16:54
  • @TJ Wolschon heh, no, that can't be implemented. I will get `System.StackOverflowException` due to endless getting `Osoba` – Cezar May 30 '18 at 16:55
  • Bummer, we use that on one of the projects I'm on so I thought it might work. – Daxtron2 May 30 '18 at 16:56
  • Can an Aspect Oriented approach help? see https://stackoverflow.com/questions/232884/aspect-oriented-programming-vs-object-oriented-programming – L0uis May 30 '18 at 16:57
  • 1
    Since it is an asp.net mvc application, have a look at https://stackoverflow.com/questions/1718501/best-way-to-trim-strings-after-data-entry-should-i-create-a-custom-model-binder; they implemented the trim via a modelbinder, meaning at the moment the posted text gets assigned to the viewmodel. – pfx May 30 '18 at 17:09
  • @pfx that's what I needed! Thanks! – Cezar May 30 '18 at 17:26

0 Answers0