I know it's kind of the wrong thing to do, but I'm dealing with a legacy codebase that has NULLS when it means empty strings and vice versa.
I can't immediately see how it is possible, but is it possible to get (or modifiy dapper so it will) return an empty string instead of a null string when mapping back from the database.
I tend to use a global extension method on string called ConvertNull() which converts any null values to an empty string. You can then call this anywhere without your code looking cluttered. If you're using this directly on an aspx page, just make sure you've imported the namespace of the extension methods and then the method will be available to you:
Then call this on an instance of a string.
Usage:
myStringInstance.ConvertNull().Replace("\r\n", "<br />");
You can control this with your queries, for example:
So in the above example, coalesce will return an empty string when Foo is null.
Dapper doesn't call any setter when it sees a null, so options might include:
""
in the constructornull
in the accessorSo:
or:
However, this only applies to reading values; I can't think of a nice way to get dapper to turn
""
intonull
when passing the dto in as the parameter object; options include:""
tonull
(perhaps write astring NullIfBlank(this string s)
extension method)null
in place of""
, and have your database query bind to@NameOrNull
rather than@Name
In short: depending how you load the data to the dapper you may get two different scenarios.
First: Turn up your data provider layer, for example like in this post - How to return null from a Dapper query rather than default(T)?.
Second way to try: you may modify your
GetTypeDeserializer
like in the following post - Change Dapper so that it maps a database null value to double.NaNThird and the last: it is my friendly advice to work on your previous questions acceptance rate. In this way you may increase chances of replies for your questions.
Hope all this will help.