Archive
HTML Atrributes in MVC 2
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.
<%: Html.TextBoxFor(model => model.Date, new {size = 25}) %>
<%: Html.ValidationMessageFor(model => model.Date)%>
</div>
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 ..
<%: Html.TextBoxFor(model => model.Date, new {@class = "required"}) %>
<%: Html.ValidationMessageFor(model => model.Date)%>
</div>
and as you can see we got what we wanted:)

Dependency Injection in WCF Service
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.
- Castle.Core.dll
- Castle.Windsor.dll
- Castle.Facilities.WcfIntegration.dll

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.
ICustomerRepository.cs
{
Customer GetById(int Id);
}
Customer.cs
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
CustomerRepository.cs
{
public Customer GetById(int Id)
{
return new Customer {
Id = 5,
FirstName = "Joe",
LastName = "Blogg",
Age = 45
};
}
}
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.
ICustomerBl.cs
{
Customer GetById(int Id);
}
CustomerBl.cs
{
private readonly ICustomerRepository repository;
public CustomerBl(ICustomerRepository repository)
{
this.repository = repository;
}
public Customer GetById(int Id)
{
return repository.GetById(Id);
}
}
We are almost there, lets finish the services layer too.
{
[OperationContract]
Customer GetById(int Id);
}
CustomerService.cs
{
private readonly ICustomerBl customerBl;
public CustomerService(ICustomerBl customerBl)
{
this.customerBl = customerBl;
}
public Customer GetById(int Id)
{
return customerBl.GetById(Id);
}
}
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.
CustomerService.svc
Service="WindsorWcfIntegration.CustomerService"
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration"
CodeBehind="CustomerService.svc.cs" %>
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.
Global.asax.cs
{
public WindsorContainer Container { get; protected set; }
protected void Application_Start(object sender, EventArgs e)
{
Container = new WindsorContainer();
Container.AddFacility<WcfFacility>();
Container.Register(
Component.For<ICustomerRepository>().ImplementedBy<CustomerRepository>(),
Component.For<ICustomerBl>().ImplementedBy<CustomerBl>(),
Component.For<ICustomerService>().ImplementedBy<CustomerService>()
);
}
}
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.