Improving Visual Studio Performance with RAM disk and Second SSD

One of the best way to improve Visual Studio performance is to replace your HDD with SSD. However, for various reasons it may not always an option. Here are a few other hardware-related suggestions that may help your Visual Studio experience:

  • If you have enough RAM, create a RAM disk (I use freeware from SoftPerfect ). Set your web.config to use it as a temp compilation directory:
     <compilation tempDirectory="R:\TempAspFolderOnRamDisk">
    

    While you are there, you can experiment with batch parameter. It is used to control whether the initial page request will continue to compile additional files in the same directory before completing the original request. I prefer to set it to false.

  • If you have an ability to add a second SSD, you may want to move all your source code to it. However, it may mean you have to change the configuration on your machine to make it work on a different drive/folder. To avoid this configuration headache, instead create a symbolic link between your original source code directory, and the new one on the SSD drive:

    mklink /D "C:\originalSourceCodeDirectory" R:\DirectoryOnSSD
    

    You have to delete or rename the originalSourceCodeDirectory prior to running mklink command.

  • Move your bin/obj folders to RamDisk. Use symbolic links to avoid configuration problems. This did not seem to have any noticeable effect on my configuration, but your mileage may vary.
  • Use some caching software like PrimoCache. It allows to cache data either to your RAM or an additional SSD.

The most beneficial of these techniques for me was moving source code to SSD.

Passing custom objects to FluentValidation

This is a guide for passing custom objects to Fluent Validators. This can be helpful for more complex validation scenarios.

Here’s an example of what we want to achieve:

public class OurModelValidator: AbstractValidator<OurMVCViewModel>
{
public OurModelValidator(OurClassWeWantToPassToValidator ourClassWeWantToPassToValidator)
{
if (ourClassWeWantToPassToValidator.SomeCondition)
{
RuleFor(m => m.PropertyA).NotEmpty();
}
else
{
RuleFor(m => m.PropertyB).NotEmpty();
}
}
}

In order to achieve this, we need to make some changes to the FluentValidationModelValidatorProvider class by overriding its CreateValidator method:

public class OurCustomFluentModelValidatorProvider : FluentValidationModelValidatorProvider
{

protected override IValidator CreateValidator(ModelMetadata metadata, ControllerContext context)
{
var type = this.IsValidatingProperty(metadata) ? metadata.ContainerType : metadata.ModelType;
return GetOurModelValidator(type) ?? this.ValidatorFactory.GetValidator(type);
}

private IValidator GetOurModelValidator(Type modelType)
{
if (modelType == typeof(OurClassWeWantToPassToValidator))
{
var ourClassWeWantToPassToValidator = new OurClassWeWantToPassToValidator();
return new OurModelValidator(ourClassWeWantToPassToValidator);
}

return null;
}
}

Since we want FluentValidation to handle all situation when OurModelValidator is not applicable, we use built-in ValidatorFactory to return default Validator when validating anything but OurMVCViewModel.

And the last step is to register our new provider:

ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());
ModelValidatorProviders.Providers.Add(new DataErrorInfoModelValidatorProvider());
ModelValidatorProviders.Providers.Add(new ClientDataTypeModelValidatorProvider());
ModelValidatorProviders.Providers.Add(new OurCustomFluentModelValidatorProvider());