Getting Dapper to return an empty string instead of a null string

前端 未结 4 565
难免孤独
难免孤独 2021-01-18 13:13

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

相关标签:
4条回答
  • 2021-01-18 13:50

    You can control this with your queries, for example:

        public class Data
        {
            public string Foo { get; set; }
        }
    
        var result = conn.Query<Data>("select Foo = coalesce(Foo, '') from MyTable");
    

    So in the above example, coalesce will return an empty string when Foo is null.

    0 讨论(0)
  • 2021-01-18 13:52

    Dapper doesn't call any setter when it sees a null, so options might include:

    • set the default value to "" in the constructor
    • check for null in the accessor

    So:

    public class SomeDto
    {
        public SomeDto()
        {
            Name = "";
        }
        public string Name {get;set;}
    }
    

    or:

    public class SomeDto
    {
        private string name;
        public string Name { get {return name ?? "";} set {name = value;} }
    }
    

    However, this only applies to reading values; I can't think of a nice way to get dapper to turn "" into null when passing the dto in as the parameter object; options include:

    • creating an anon-type, substituting "" to null (perhaps write a string NullIfBlank(this string s) extension method)
    • having a shim property on the type that returns null in place of "", and have your database query bind to @NameOrNull rather than @Name
    0 讨论(0)
  • 2021-01-18 13:56

    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.NaN

    Third 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.

    0 讨论(0)
  • 2021-01-18 14:14

    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:

    namespace ExtensionMethods
    {
        using System;
    
        public static class StringExtensionsClass
        {
            /// <summary>Converts null strings to empty strings</summary>
            /// <param name="s">Input string</param>
            /// <returns>Original string, or empty string if original string was null</returns>
            public static string ConvertNull(this string s)
            {
                return s ?? "";
            }
        }
    }
    

    Then call this on an instance of a string.

    Usage:

    myStringInstance.ConvertNull().Replace("\r\n", "<br />");

    0 讨论(0)
提交回复
热议问题