简体   繁体   English

在 .NET 中格式化字符串的正确位置在哪里?

[英]Where is the correct place to format a string in .NET?

We have a field called employee number in our database.我们的数据库中有一个名为员工编号的字段。 It is char(10) field.它是 char(10) 字段。 we are storing the numbers right justified.我们存储的数字是正确的。 so we are storing the numbers as " 1" , " 2" etc. I am not sure why we started doing this because it happened before I started working here.所以我们将数字存储为" 1"" 2"等。我不确定我们为什么开始这样做,因为它发生在我开始在这里工作之前。

Because of this, each function in the business logic that has an employee number as one of the parameters needs to right justify the number.因此,业务逻辑中的每个 function 都需要将员工编号作为参数之一进行右对齐。 If we forget that, it won't update the database properly.如果我们忘记了这一点,它将无法正确更新数据库。

My question is:我的问题是:

Is there a better way to do this so that we don't have to format the number in each function?有没有更好的方法来做到这一点,这样我们就不必格式化每个 function 中的数字?

Stop storing your employee ID's in the database right justified and do any justification AFTER you pull them out when you want to display them.停止将您的员工 ID 正确存储在数据库中,并在您想要显示它们时将它们拉出后进行任何理由。

Edit : Employee Number should be stored at the very least as varchar(10) (assuming that it can accept non numeric characters) or as an integer (if it is an actual "number" as it is assumed to be).编辑:员工编号至少应存储为 varchar(10) (假设它可以接受非数字字符)或 integer (如果它是假设的实际“数字”)。 Upon retrieval of the number, you would then right justify it if necessary in whatever application you are displaying it in.检索到该数字后,如果有必要,您可以在显示它的任何应用程序中正确证明它。

Note : I said this in comments but it needs to be made explicitly clear: Trying to work around the underlying problem is only going to lead to more problems down the line and an overall sense of confusion among the different developers who work on the project and will inherit the project in the future.注意:我在评论中说过这一点,但需要明确说明:试图解决潜在问题只会导致更多问题,并且在从事该项目的不同开发人员和未来将继承该项目。

I don't agree with the use of the char datatype, but to address the question, one way you could do this, is with a trigger.我不同意使用 char 数据类型,但要解决这个问题,你可以这样做的一种方法是使用触发器。 Update a table with the number in its Integer form and then have the trigger format it and store it in the correct table.用 Integer 表格中的数字更新表格,然后对其进行触发器格式化并将其存储在正确的表格中。

Again, I would just refactor the datatype, but it sounds like you aren't in a position to do that.同样,我只是重构数据类型,但听起来你不在 position 中这样做。

Even though it's a bad practice as others have posted couldn't you expose a read only property from them employee class that takes care of the formatting即使这是其他人发布的一个不好的做法,你不能从他们那里公开一个只读属性,负责格式化的员工 class

I agree with the others that refactoring would be the first suggestion.我同意其他人的观点,即重构将是第一个建议。 Store the employee id's as raw data and do any formatting that requires the right justification at the view level.将员工 ID 存储为原始数据,并在视图级别进行任何需要正确对齐的格式化。

If this is absolutely not an option, I'd write an extension method on string to handle this.如果这绝对不是一个选项,我会在字符串上编写一个扩展方法来处理这个问题。 Then you can store someString.RightJustify(10).然后你可以存储 someString.RightJustify(10)。 Extension methods have to be static methods in a static class.扩展方法必须是 static class 中的 static 方法。 Something like the following would work:像下面这样的东西会起作用:

public static class ExtensionMethods
{
    public static string RightJustify(this string s, int chars)
    {
        if (s.Length > chars)
            return s.Substring(0, chars);
        else
            return s.PadLeft(chars, ' ');
    }
}

This depends of course of the architechure of the system.这当然取决于系统的架构。 Do you have a datalayer for the buisness to use?您有供企业使用的数据层吗? Maybe you can do the formatting there?也许你可以在那里进行格式化?

I suppose it´sa big refactor to change the DB to int or bigint, if not, do that.我想将数据库更改为 int 或 bigint 是一个很大的重构,如果不是,请这样做。

You might want to consider an extension method that would extend the (assumed to be) integer type of your employee id.您可能需要考虑一种扩展方法来扩展(假设是)您的员工 ID 的 integer 类型。

public static int ToRightAlignedString(this int obj)
{
    return ("          " + obj.ToString()).Right(10);
}

You could define a type (struct, most likely) that wrapped an integer, and use that in your code for the employee number.您可以定义一个包装 integer 的类型(结构,最有可能),并在您的代码中使用它作为员工编号。 Something like this (C#).像这样的东西(C#)。

struct EmployeeNumber
{
    private int empNumber;

    public String EmpNum
    {
        set
        {
            int test;
            if (Int32.TryParse(value, out test))
            {
                empNumber = test;
            }
        }

        get
        {
            return empNumber.ToString("D10");
        }
    }
}

NOTE: I agree that fixing the database is the Right Thing™ to do.注意:我同意修复数据库是正确的事情™。 However, I also know that option is not always available.但是,我也知道该选项并不总是可用的。 Using a custom data type instead of an int in your code would be a handy way to eliminate issues in converting back and forth to the database strings.在代码中使用自定义数据类型而不是 int 将是消除来回转换为数据库字符串的问题的便捷方法。

Public Function foo(ByVal valToStore As Object) As String
    Dim RetVal As String = String.Empty
    If TypeOf valToStore Is String Then
        RetVal = DirectCast(valToStore, String)
    ElseIf TypeOf valToStore Is Integer Then
        RetVal = DirectCast(valToStore, Integer).ToString
    End If
    RetVal = RetVal.Trim.PadLeft(10, " "c)
    Return RetVal
End Function

i agree that the database should be storing this as a number, not a string.我同意数据库应该将其存储为数字,而不是字符串。 i took a guess that you wanted a VB answer.我猜你想要一个 VB 答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM