Adding MVC views into a Sharp Architecture project

When you first create a Sharp Architecture 1.6 solution you will find that you can’t easily add a View or Area into the Web project via Solution Explorer’s context menu in Visual Studio. This is because the .csproj file for the Web project does not have the reference number for MVC in its project types section.

To rectify this navigate to the web project’s .csproj file using Windows Explorer and then open it with Notepad. Within the tag enter the following GUID as the first one in the list: {F85E285D-A4E0-4152-9332-AB1D724D3325}.

It should then look something like this:


You will then have the ability to add Views direct from the context menu.

Unit testing MVC Controllers with Model Validation

If you use ModelState.isValid in your controller classes then you will run into problems during unit testing as it will always return true as Model Validation occurs during the Request pipeline, which isn’t the route followed during testing

To overcome I’ve implemented a static controller extension “CallWithModelValidation” as seen below:

public static class ControllerExtensions
        //Enforeces model validation during testing. This is required because model binding doesn't occur in 
        //during testing which means that ModelState.IsValid is always true
        //Taken from
        public static ActionResult CallWithModelValidation<C, R, T>(this C controller
            , Func<C, R> action
            , T model)
            where C : Controller
            where R : ActionResult
            where T : class{
            DataAnnotationsModelValidatorProvider provider = new DataAnnotationsModelValidatorProvider();

            IEnumerable<ModelMetadata> metadata = ModelMetadataProviders
                .GetMetadataForProperties(model, typeof(T));
            foreach (ModelMetadata modelMetadata in metadata) 
                IEnumerable<ModelValidator> validators = provider
                    .GetValidators(modelMetadata, new ControllerContext());
                foreach (ModelValidator validator in validators) 
                    IEnumerable<ModelValidationResult> results = validator.Validate(model);
                    foreach (ModelValidationResult result in results)
                        controller.ModelState.AddModelError(modelMetadata.PropertyName, result.Message);
            return action(controller);

This can then be called in your controller test by doing the following (where in the case below “AddSupplier” is the controller method under test):

_testController.CallWithModelValidation(m => m.AddSupplier(viewModel), viewModel).AssertViewRendered().ForView("CreateSupplier");

Using MVC Ajax helpers with S#arp Architecture

If you are using S#arp, and try to use the Ajax.Beginform() method then you will initially find that it barfs when you try to submit the  form. A typical error message is something like (paraphrasing) Sys.Mvc.AsyncForm.handleClick method not supported . This is because the javascript files that S#arp gives you as default are old (possibly the MVC 1.0) versions.

I suggest that you create a regular ASP.NET MVC2 project and then copy the scripts from the Scripts directory into your S#arp project, in particular MicrosoftAjax.js and MicrosoftAjax.debug.js.  That seems to do the trick.

True in Sharp 1.6.

Word Wrap in CSS

The following does ‘proper’ word wrapping in all the browsers I tested it in (i.e. excluding IE6)

.wordwrap {
    white-space: pre-wrap; /* css-3 */
    white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
    white-space: -pre-wrap; /* Opera 4-6 */
    white-space: -o-pre-wrap; /* Opera 7 */
    word-wrap: break-word; /* Internet Explorer 5.5+ */ 

Posting Code

Chaps here is a guide for

posting source code, 

which considering the nature of this blog should come in useful.