# Wednesday, September 08, 2010

Ah… there is one left. We do not only want to remove properties form the model state the typed way. What about adding error messages?

Here we go:

using System; using System.Linq.Expressions; using System.Reflection; using System.Web.Mvc; namespace devcoach.Web.Mvc { /// <summary> /// Extensions for the <see cref="ModelStateDictionary"/> class. /// </summary> public static class ModelStateDictionaryExtensions { /// <summary> /// Removes the specified member from the <see cref="ModelStateDictionary"/>. /// </summary> /// <param name="me">Me.</param> /// <param name="lambdaExpression">The lambda expression.</param> public static void Remove<TViewModel>( this ModelStateDictionary me, Expression<Func<TViewModel, object>> lambdaExpression) { me.Remove(GetPropertyName(lambdaExpression)); } /// <summary> /// Adds the model error. /// </summary> /// <typeparam name="TViewModel">The type of the view model.</typeparam> /// <param name="me">Me.</param> /// <param name="lambdaExpression">The lambda expression.</param> /// <param name="errorMessage">The error message.</param> public static void AddModelError<TViewModel>( this ModelStateDictionary me, Expression<Func<TViewModel, object>> lambdaExpression, string errorMessage) { me.AddModelError(GetPropertyName(lambdaExpression), errorMessage); } /// <summary> /// Adds the model error. /// </summary> /// <typeparam name="TViewModel">The type of the view model.</typeparam> /// <param name="me">Me.</param> /// <param name="lambdaExpression">The lambda expression.</param> /// <param name="exception">The exception.</param> public static void AddModelError<TViewModel>( this ModelStateDictionary me, Expression<Func<TViewModel, object>> lambdaExpression, Exception exception) { me.AddModelError(GetPropertyName(lambdaExpression), exception); } /// <summary> /// Gets the name of the property. /// </summary> /// <param name="lambdaExpression">The lambda expression.</param> /// <returns></returns> private static string GetPropertyName(this Expression lambdaExpression) { var e = lambdaExpression; while (true) { switch (e.NodeType) { case ExpressionType.Lambda: e = ((LambdaExpression)e).Body; break; case ExpressionType.MemberAccess: var propertyInfo = ((MemberExpression)e).Member as PropertyInfo; return propertyInfo != null ? propertyInfo.Name : null; case ExpressionType.Convert: e = ((UnaryExpression)e).Operand; break; default: return null; } } } } }

On forkcan again…

Wednesday, September 08, 2010 12:55:12 AM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 

I’m currently working with Philip on a project which also has an ASP.NET MVC (by the way I really like the .MVC idea somebody came up the last weeks) project. We came to the point where we needed to remove the required property “Password” if the user is registering using OAuth.

Argh. That’s is not typed!

So I wrote a small extension method that I added to the devcoach.Web.Mvc assembly I’d like to share here:

 

using System; using System.Linq.Expressions; using System.Reflection; using System.Web.Mvc; namespace devcoach.Web.Mvc { /// <summary> /// Extensions for the <see cref="ModelStateDictionary"/> class. /// </summary> public static class ModelStateDictionaryExtensions { /// <summary> /// Removes the specified member from the <see cref="ModelStateDictionary"/>. /// </summary> /// <param name="me">Me.</param> /// <param name="lambdaExpression">The lambda expression.</param> public static void Remove<TViewModel>( this ModelStateDictionary me, Expression<Func<TViewModel, object>> lambdaExpression) { me.Remove(GetPropertyName(lambdaExpression)); } /// <summary> /// Gets the name of the property. /// </summary> /// <param name="lambdaExpression">The lambda expression.</param> /// <returns></returns> private static string GetPropertyName(this Expression lambdaExpression) { var e = lambdaExpression; while (true) { switch (e.NodeType) { case ExpressionType.Lambda: e = ((LambdaExpression)e).Body; break; case ExpressionType.MemberAccess: var propertyInfo = ((MemberExpression)e).Member as PropertyInfo; return propertyInfo != null ? propertyInfo.Name : null; case ExpressionType.Convert: e = ((UnaryExpression)e).Operand; break; default: return null; } } } } }

Using the extension method you can use the following fully typed syntax inside your controllers:

[HttpPost] public ActionResult Registration(AccountRegistrationViewModel model) { if (model.IsTokenRegistration) { ModelState.Remove<AccountRegistrationViewModel>(t => t.Password); ModelState.Remove<AccountRegistrationViewModel>(t => t.RetypePassword); } //... }

If you like to add comments or commit improvements have a look at forkcan

Wednesday, September 08, 2010 12:24:58 AM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, September 03, 2010

I just installed Adobe Air Launchpad Beta. It is an application that eases up development of Adobe Air Projects.  By providing a wizard UI that collects information on how the app will be called, look like and so on … The result is a generated project.

Even If I’m really not a fan of Adobe… The approach is more likeable in contrast to a simplified development UI. Why? Because it eases up the process for everyone. For a professional it saves a few seconds; for a newbee it hides the complexity of getting started and flattens the learning curve.

clip_image001

clip_image002

clip_image003

clip_image004

 

Friday, September 03, 2010 12:41:06 PM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 
# Tuesday, August 24, 2010

NGem is a small utility that can help you to resolve assembly references (third-party or you own framework stuff).

This is the second post on how to work with NGem.

Lets assume you create a new project and start with creating its directory:

image

In the new project directory you can now run NGem to resolve the latest versions of the assemblies that you like to reference from a http reachable location. To set the location of the repository where the ngems are stored just edit the config files appsetting called nGemSource.

<?xml version="1.0"?> <configuration> <appSettings> <add key="nGemSource" value="http://ngem.devplex.net/gems"/> </appSettings> </configuration

Now type:

NGEM.exe install {your_package_name}

To see how you can create a ngem package take a look at the post here.

image

Because I’ve choosen to download my ngems from a password protected location (Windows Authentication and SSL + Basic Authentication are supported) NGem asks me for a username and a password (these will be stored – currently unencrypted – under your user profile directory (protected by the systems ACLs) is a settings file.

NGem will take care of adding the package plus all of its references to a directory called lib:

image

That’s it folks. I really think its easy this way coping with references.

Whats next? A build task? A Visual Studio Plug-In? Let me and Philip Proplesch know!

--Daniel

Tuesday, August 24, 2010 12:09:20 AM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 
# Monday, August 23, 2010

NGem is a small utility that can help you to resolve assembly references (third-party or you own framework stuff).

This is the first post on how to work with NGem.

So lets imagine you have created a few assemblies:

image

The assembly in the directory ObjectModel.Data relies on the one called ObjectModel. So we need to create a reference…

image

Its pretty straight forward. Just create a file in the corresponding directory called References.xml with the following content:

<?xml version="1.0"?> <references> <add name="devcoach.ObjectModel" /> </references>

Now lets build the ngems:

ngem.exe make “D:\lib\“ devcoach ObjectModel

image

ngem.exe make “D:\lib\“ devcoach ObjectModel.Data

image

Now just upload the ngems to a http reachable destination and you’re done.

Monday, August 23, 2010 11:52:31 PM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 
# Sunday, August 22, 2010

I’d really like to thank Scott Hanselman for digging into the nitty gritty details of how to change the Visual Studio Default Browser.

But… let me cite one of the comments:

“Should adding browser options to Visual Studio be <sarcasm>this easy</sarcasm>?”

I hope we all agree on: Go fix this!

As I have not found my way to powershell (yet) and I want to have VS Integration (as Scott finalizes his post) too: Here comes the macro version…

Sunday, August 22, 2010 10:13:35 PM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 

I was really enthusiastic about Nu as I first read about it. But the problem for a lot of customers and also for myself is that it cannot cope with private assemblies – stuff that is not open source.

Today I started a new open source project with PhilipNGem: A gem like 3rdPartyAssembly resolver and packer with support for private gem repositories.

You can find the source code at github.

Sunday, August 22, 2010 2:35:50 AM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 
# Wednesday, August 18, 2010

image

Heute hatten wir unser erstes .NET Coding Dojo. Auch wenn Ilker es nicht zu uns geschafft hat, war es ein voller Erfolg und hat allen Teilnehmern viel Spass gemacht.

Hier das Slide: http://www.lennybacon.com/download/Events/2010/2010-08-NET-Coding-DoJo.pdf

Und der Code von Philip: http://github.com/philipproplesch/NetugNiederrhein/tree/master/src/2010-08/FizzBuzz/

Wednesday, August 18, 2010 10:26:14 PM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 
# Monday, August 09, 2010

Last week I did talks on ASP.NET MVC and ASP.NET Controls at the .NET Developer Group Braunschweig and Dotnet User Group Dortmund. This way I’d like to thank Lars Keller and Paul Mitzel as well as all attendees for joining and havig such a great time!

Slides will be up on the user group pages soon as I submitted them today to Paul and Lars.

Monday, August 09, 2010 4:50:55 PM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  | 

I just went over to itunes to get the black keys album brothers. What an amazing experience again. Let’s crunsch some numbers:

The album has 15 tracks

itunes crashed 22 times until all downloads finished.

WTF #1

 

Last but not least… itunes did not add the tracks to my purchases or my library. damn. A look into the the file system shows what’s going on.

image

WTF #2

Ok so I tried to start the download again as it seems it had not finished, but…

image

Fail, Apple, Fail, Fail

Monday, August 09, 2010 1:02:54 PM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [0]  |