Tuesday, September 20, 2011

Change the default Check-in Action of Visual Studio 2010

Changing the default check-in Action should be fairly simple, an option under Source Control, but it's not. In order to accomplish that, you have to change a registry key...

HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\TeamFoundation\SourceControl\Behavior

change @ResolveAsDefaultCheckinAction to 'False'

from Stack Overflow answer

Wednesday, March 31, 2010

ObjectQuery Include Extension

I was tired of using string to include a relation with Linq to Entities like
contexte.Users.Include("Roles")
So I decided to write an extension that would enable to use an expression like
contexte.Users.Include(u => u.Roles)
so here is the code:
    public static class ObjectQueryExtensions
{
public static ObjectQuery<TEntity> Include<TEntity, TProperty>(this ObjectQuery<TEntity> query, Expression<Func<TEntity, TProperty>> expression) where TEntity : class
{
string name = expression.GetPropertyName();
return query.Include(name);
}
}
and you also need this:
    public static class ExpressionExtensions
{
public static string GetPropertyName<TObject, TProperty>(this Expression<Func<TObject, TProperty>> expression) where TObject : class
{
if (expression.Body.NodeType == ExpressionType.Call)
{
MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
string name = ExpressionExtensions.GetPropertyName(methodCallExpression);
return name.Substring(expression.Parameters[0].Name.Length + 1);
}
return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
}

private static string GetPropertyName(MethodCallExpression expression)
{
MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
if (methodCallExpression != null)
{
return GetPropertyName(methodCallExpression);
}
return expression.Object.ToString();
}
}

Monday, February 8, 2010

DisplayNameAttribute Localized

So here is a simple Attribute, extending the DisplayNameAttribute, inspired from a post on stackoverflow


using System;
using System.Linq;
using System.ComponentModel;
using System.Reflection;
using System.Resources;

namespace MvcExtensions.Attributes
{
[AttributeUsage(AttributeTargets.Property AttributeTargets.Field, AllowMultiple = false)]
public class DisplayNameLocalizedAttribute : DisplayNameAttribute
{
public DisplayNameLocalizedAttribute(Type resourceType, string resourceKey)
: base(LookupResource(resourceType, resourceKey)) { }

internal static string LookupResource(Type resourceType, string resourceKey)
{
PropertyInfo property = resourceType.GetProperties().FirstOrDefault(p => p.PropertyType == typeof(System.Resources.ResourceManager));
if (property != null)
{
return ((ResourceManager)property.GetValue(null, null)).GetString(resourceKey);
}
return resourceKey;
}
}
}

An I use it simply like this:


using MvcExtensions.Attributes;
using MyMvc2Rc2TestApp.App_LocalResources;

namespace MyTestMVC2App.Areas.UserManagement.Models
{
public class User
{
[DisplayNameLocalized(typeof(DisplayName), "User_UserName")]
public string UserName { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_Password")]
public string Password { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_FirstName")]
public string FirstName { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_LastName")]
public string LastName { get; set; }
[DisplayNameLocalized(typeof(DisplayName), "User_Email")]
public string Email { get; set; }
}
}

I have a DisplayName.resx file in App_LocalResources, it is an Embedded Resource with Public Access Modifier