tags:

views:

248

answers:

5

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.

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

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

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.

+1  A: 

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.

bcwood
Can you expand on 'enum', in the context of C# ?
Ruben Bartelink
For example, if I had a list of statuses, I would create an enum, something like:public enum Status { Pending, OnHold, InProgress, Complete}
bcwood
+2  A: 

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.

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.

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.

slf
isnt that overkill for strings that represent names of properties on entity classes - they dont get localised, they need to stay in sync wiht the property names
Ruben Bartelink
+1  A: 

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.

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.

Ruben Bartelink
+1  A: 

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

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/

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

Cherian
A: 

I use a similar approach as Cherian. I got my idea from the FluentNhibernate's 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.

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

Bertvan