Recently I was working with a very interesting form problem and MVC 2, anyway I manage to solve the problem and wanted to use jQuery validation plugin. I knew i had use the htmlattributes property to inject the attributes and this is how it is done.
So here we are saying to the MVC View engine is that when the textbox for the Date field is generated add size as an html attribute with a value of 25,and this is how the output looks in Firebug.
So far so good and now I wanted to use JQuery Validation plugin to validate the date textbox before I submit the form. This is how JQuery validation plugin does a automatic validation of a form.
This is direct code from the Jquery validation plugin page which is at http://docs.jquery.com/Plugins/validation
However when i tried using the class reserved as a htmlattribute I got a clueless runtime error as “CS1513: } expected” and after some mucking around I came to know that you have to use @class instead of class . So when i change my code to this ..
and as you can see we got what we wanted:)
In this post I will show you how to use dependency injection in a typical WCF Service and the architecture of the solution is a standard layered approach i.e. Service calls the business logic layer and business logic layer calls the data access layer etc.
In order to use Dependency Injection in WCF we are going to use the WcfFacility provided by Castle Windsor. This is another one of those good extension points in Castle Windsor which implements the IFacility interface and this is the class that does all the underlining pluming of WCF Service host.
In order to get started with WCF facility lets download it from the castle project from github website which is at http://github.com/castleproject/Castle.Facilities.Wcf.
Build the project which will build the binary, and we will need all the three binaries i.e.
I had a bit of gotchas here as my project had reference to the previous version of Castle.Core and Castle.Windsor i.e. 2.5.0 and the newly build Castle.Facilities.WcfIntegration was of version 2.5.1 so make sure you copy all the three assemblies to your project.
Now my favourite part that is File > New > WCF Service application
Lets get down to writing some code. First we will create the data access layer using a repository pattern, this is just an illustration of an end-to-end dependency injection.
Moving one layer up lets build the business logic layer in which we will also use constructor injection to inject the dependency, also for easy illustration I am going to create the business logic layer and service layer as simple pass through and you will see that they don’t do much but in real life project it would be much more sophisticated and complex too.
We are almost there, lets finish the services layer too.
And this is how my end project structure looks like.
Now with all the coding done lets see how we will wire up rest of the stuff. First we need to change the declaration in service file and in order to do that, just right-click on the CustomerService.svc and open with XML (Text) Editor and add the following code.
With the above declaration we are saying that when ever the customer service is requested the DefaultServiceHostFactory will take care of how the service is created and instantiated. Let’s wire this up with the WcfFacility and since it has to happen at some startup process so we will do that in Global.asax file of the WCF Service project.
As you can see in the above code we are instantiating a new instance of WindsorContainer and adding the WcfFacility and then we go ahead & register all the interfaces and its concrete implementation and at the end we register the service contract ( interface) and the implementing service class.
Now let’s do a quick test by running the wcf test client and as you can see the service comes out without any errors so it’s all good.
And when we invoke the GetById method we get our desired result.
Hope this serve as a quick start tutorial into DI and you will be able to use Dependency Injection in your WCF project.