Umbraco 6 MVC and Dependency Injection

Thanks to some feedback on an Umbraco forum post I have re factored my code and made use of Dependency Injection.

I am using the awesome ninject and if you use the nuget package manager it is so easy its un real.

So navigate to https://www.nuget.org/packages/Ninject.MVC3/3.0.0.6 you can see the nuget install command Install-Package Ninject.MVC3 -Version 3.0.0.6 so install that into your web project.  This will pull down everything also in your app_start folder it will create a class NinjectWebCommon in there you have method


private static void RegisterServices(IKernel kernel)

{
kernel.Bind<ISearchService>().To<SearchService>()
}

You add your bindings there. Next update your surface controller mine looks like


private readonly ISearchService _searchService;
///
/// inject in this dependancy using ninject
///

///
public SearchFormSurfaceController(ISearchService searchService)
{
_searchService = searchService;
}

Its as easy as that. Now you too can be all hip and upto date with all the cool kids!!

Advertisements

Umbraco MVC localised data annotations

So in MVC you decorate your model with data annotations things like

[Display("Email Address")]
[Required]
public string EmailAddress {get; set;}

With these attributes you pass in some text or you can use a .net resource. In that case you give it assembly and key

[Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)]

It would be nice if we could use the Umbraco dictionary as well. So I have created my own classes so for the display and required attributes i have


[UmbracoDisplayLocalised("Email")]
[Email("InvalidEmail")]
[UmbracoLocalisedRequired("InvalidEmail")]
public string EmailAddress {get; set;}

What I pass in is Umbraco dictionary key so now my model when rendered in view has localised labels. I also have a custom build of the excellent data annotations project which also reads from the Umbraco dictionary.

I have sent it to Lee Kelleher Sir uComponents to have a play. It may appear in there or I will release as a separate project on our. My source code is available  on github

https://github.com/ismailmayat/MvcUmbracoDataAnnotations