在.net代码中删除文字字符串

时间:2022-05-28 22:28:49

What have you done to remove (Helpers/extension methods) string literal in code?

你在代码中删除(帮助/扩展方法)字符串文字做了什么?

e.g. I have nhibernate search criteria code like this all over the place.

例如我到处都有这样的nhibernate搜索条件代码。

Expression.Eq("Status", LoginStatus.LoggedIn),

“Status” being the property of an entity object used as a string in the case.

“Status”是在案例中用作字符串的实体对象的属性。

Update: Primary reason in this case is to enable refactoring. If I write a helper method which reflects the object and gets the value, will make the above expression strongly typed.

更新:在这种情况下的主要原因是启用重构。如果我编写一个反映对象并获取值的辅助方法,将使上面的表达式强类型化。

5 个解决方案

#1


2  

This is what "Resources" and "Settings" is for. You can find this by right clicking your project in Visual Studio and clicking "Properties", then going to the "Resources" or "Settings" tab.

这就是“资源”和“设置”的用途。您可以通过在Visual Studio中右键单击项目并单击“属性”,然后转到“资源”或“设置”选项卡来找到它。

For pre-built resources that won't change often, use resources. For things you want to be configurable use Settings instead because it will auto-generate blocks of configuration for your App.config. You will still need to manually copy and paste these values if you do not want to use the defaults.

对于不会经常更改的预构建资源,请使用资源。对于您希望配置的内容,请使用“设置”,因为它会自动生成App.config的配置块。如果您不想使用默认值,则仍需要手动复制和粘贴这些值。

The nice thing about both is that VS will build a nice static class with properties you can use throughout your code. VS will maintain the class and resources for you dynamically as long as you continue to use the wizard.

两者的好处是VS将构建一个很好的静态类,其中包含可在整个代码中使用的属性。只要您继续使用向导,VS将动态维护类和资源。

#2


1  

I'll usually declare them as constants, or, if I have groups of related strings, I'll create an enum instead.

我通常会将它们声明为常量,或者,如果我有相关字符串组,我将创建一个枚举。

Either way, at least they have a descriptive name attached to them (instead of using "magic strings"), and their use will always be consistent.

无论哪种方式,至少他们附有一个描述性的名称(而不是使用“魔术字符串”),他们的使用将始终是一致的。

#3


1  

In the past, I've used CodeRush (or your favourite refactoring tool) to convert to a const string in the class, and then moved said const strings to be public members of the entity class to which they apply.

在过去,我使用CodeRush(或您最喜欢的重构工具)转换为类中的const字符串,然后将所述const字符串移动为它们应用的实体类的公共成员。

The real answer here, if you're looking to get your code less brittle in the face of refactoring is to get out of the strings business, and use Linq 4/to NHibernate, but you'd have to research whether it's completeness is sufficeint for your purposes.

这里真正的答案是,如果你想在重构时让你的代码不那么脆弱,那就是退出字符串业务,并使用Linq 4 /到NHibernate,但是你必须研究它的完整性是否足够为了您的目的。

#4


1  

Realized that I could do this the Expression trees way. Using Code as data!

意识到我可以用Expression树的方式做到这一点。使用Code作为数据!

Something like this

像这样的东西

  protected IList<T> _FindByProperty<TResult>(Expression<Func<T, TResult>> expression, TResult value)  
   {  
   return _FindByProperty((expression.Body as MemberExpression).Member.Name, value);  
  } 

IList<User> costCenters = _FindByProperty( user=> user.Name, "name"); 

Credits: http://suryagaddipati.wordpress.com/2009/03/14/code-as-data-in-c-taking-advantage-of-expression-trees/

致谢:http://suryagaddipati.wordpress.com/2009/03/14/code-as-data-in-c-taking-advantage-of-expression-trees/

This is related to a lot questions in the expression-trees tag.

这与表达式树标记中的很多问题有关。

#5


0  

I use a similar approach as Cherian. I got my idea from the FluentNhibernate's ReflectionHelper.

我使用与Cherian类似的方法。我从FluentNhibernate的ReflectionHelper中得到了我的想法。

The principle is to use expression trees and then you could just put in a x => x.Status expression. The method would return the property name as string.

原则是使用表达式树然后你可以放入一个x => x.Status表达式。该方法将属性名称返回为字符串。

In fact, you could also just use FluentNHibernate? However, I don't know if their querying model is evenly extensive as their mapping interfaces...

事实上,你也可以只使用FluentNHibernate?但是,我不知道他们的查询模型是否作为其映射接口均匀扩展...

#1


2  

This is what "Resources" and "Settings" is for. You can find this by right clicking your project in Visual Studio and clicking "Properties", then going to the "Resources" or "Settings" tab.

这就是“资源”和“设置”的用途。您可以通过在Visual Studio中右键单击项目并单击“属性”,然后转到“资源”或“设置”选项卡来找到它。

For pre-built resources that won't change often, use resources. For things you want to be configurable use Settings instead because it will auto-generate blocks of configuration for your App.config. You will still need to manually copy and paste these values if you do not want to use the defaults.

对于不会经常更改的预构建资源,请使用资源。对于您希望配置的内容,请使用“设置”,因为它会自动生成App.config的配置块。如果您不想使用默认值,则仍需要手动复制和粘贴这些值。

The nice thing about both is that VS will build a nice static class with properties you can use throughout your code. VS will maintain the class and resources for you dynamically as long as you continue to use the wizard.

两者的好处是VS将构建一个很好的静态类,其中包含可在整个代码中使用的属性。只要您继续使用向导,VS将动态维护类和资源。

#2


1  

I'll usually declare them as constants, or, if I have groups of related strings, I'll create an enum instead.

我通常会将它们声明为常量,或者,如果我有相关字符串组,我将创建一个枚举。

Either way, at least they have a descriptive name attached to them (instead of using "magic strings"), and their use will always be consistent.

无论哪种方式,至少他们附有一个描述性的名称(而不是使用“魔术字符串”),他们的使用将始终是一致的。

#3


1  

In the past, I've used CodeRush (or your favourite refactoring tool) to convert to a const string in the class, and then moved said const strings to be public members of the entity class to which they apply.

在过去,我使用CodeRush(或您最喜欢的重构工具)转换为类中的const字符串,然后将所述const字符串移动为它们应用的实体类的公共成员。

The real answer here, if you're looking to get your code less brittle in the face of refactoring is to get out of the strings business, and use Linq 4/to NHibernate, but you'd have to research whether it's completeness is sufficeint for your purposes.

这里真正的答案是,如果你想在重构时让你的代码不那么脆弱,那就是退出字符串业务,并使用Linq 4 /到NHibernate,但是你必须研究它的完整性是否足够为了您的目的。

#4


1  

Realized that I could do this the Expression trees way. Using Code as data!

意识到我可以用Expression树的方式做到这一点。使用Code作为数据!

Something like this

像这样的东西

  protected IList<T> _FindByProperty<TResult>(Expression<Func<T, TResult>> expression, TResult value)  
   {  
   return _FindByProperty((expression.Body as MemberExpression).Member.Name, value);  
  } 

IList<User> costCenters = _FindByProperty( user=> user.Name, "name"); 

Credits: http://suryagaddipati.wordpress.com/2009/03/14/code-as-data-in-c-taking-advantage-of-expression-trees/

致谢:http://suryagaddipati.wordpress.com/2009/03/14/code-as-data-in-c-taking-advantage-of-expression-trees/

This is related to a lot questions in the expression-trees tag.

这与表达式树标记中的很多问题有关。

#5


0  

I use a similar approach as Cherian. I got my idea from the FluentNhibernate's ReflectionHelper.

我使用与Cherian类似的方法。我从FluentNhibernate的ReflectionHelper中得到了我的想法。

The principle is to use expression trees and then you could just put in a x => x.Status expression. The method would return the property name as string.

原则是使用表达式树然后你可以放入一个x => x.Status表达式。该方法将属性名称返回为字符串。

In fact, you could also just use FluentNHibernate? However, I don't know if their querying model is evenly extensive as their mapping interfaces...

事实上,你也可以只使用FluentNHibernate?但是,我不知道他们的查询模型是否作为其映射接口均匀扩展...