0

I have a class like the following

public class MyClass
{
    public string Name { get; set; }

    public string Place { get; set; }

    public string Profession { get; set; }

    public DateTime DateOfBirth { get; set; }
}

Now based on the values of the object initialized using the above class, I need to dynamically from a SELECT statement. But I have 2 challenges here:

  1. I absolutely wish to avoid a cascading if - else or switch - case construct to check if the value of each property in the object is null (I hate both switch - case and if - else)
  2. The names of properties and the corresponding columns in my database may not match exactly

For the second problem, I am planning to put in a kind of 'mapping' into an XML file, which will map the property names to their corresponding columns, something like:

<xml>
   <mapping>
     <map property="Name" sql="NAME"/>
     <map property="Place" sql="LOCATION"/>
     <map property="Profession" sql="PROFESSION"/>
     <map property="DateOfBirth" sql="DOB"/>
   </mapping>
</xml>

If someone out there has a better idea, please let me know.

For the first challenge, I am not quite able to figure out how to dynamically generate a syntactically correct SELECT statement (replete with spaces and commas) using modern C# methods. I have been taking a look at fluent interfaces, PredicateBuilder, IQueryable, but not really able to arrive at a nice way of doing it.

Could someone out there please help me figure out a way to do this without those ugly if - elses or switch - cases? Any and all help would be appreciated!

Thanks!

A cool bull
  • 341
  • 1
  • 3
  • 12
  • Sounds like you need an ORM tool. NHibernate? E-F? – rory.ap Apr 25 '17 at 14:25
  • This sort of thing would naturally be taken care of by an ORM tool, but the component I am developing actually needs to do this sort of thing from the ground up – A cool bull Apr 25 '17 at 14:27

0 Answers0