Implementing search functionality with multiple optional parameters against database table

前端 未结 3 690
离开以前
离开以前 2021-01-02 12:54

I would like to check if there is a preferred design pattern for implementing search functionality with multiple optional parameters against database table where the access

3条回答
  •  既然无缘
    2021-01-02 13:13

    Method 1: dynamic SQL can take parameters, its pretty trivial to do and pretty much eliminates the risk of SQL injection. The best argument against dynamic SQL is how non-trivial statements can require some complex logic to generate, although this is a non-issue too if you're using a decent ORM.

    NHiberante and LinqToSql construct dynamic SQL behind the scenes, and they aren't riddled with security holes. In my opinion, you're best considering one of these two technologies before rolling your own DAL.

    Method 2: I have personally used method two in the past with no problems. You commented on the "possible performance issue for the sql", but have you profiled? Compared execution plans? In my own experience, their has been little to no performance hit using the @param is null OR col = @param approach. Remember, if it takes you 10 hours of developer time to optimize code to save 10 microseconds a year of execution time, your net savings is still almost -10 hours.

    Method 3: Combinatorial explosion. Avoid at all costs.

提交回复
热议问题