Rajeeshcv.com

Sharing my knowledge

ASP.Net MVC – Conditional rendering Partial Views with Action<T> delegate

Posted on: 28 Jan 2010 | Filed under: .NET, ASP.NET, CodeProject, MVC | Tagged under: ASP.NET, CodeProject, HTML Extension, MVC | comments (1)

This is an update to my previous post regarding conditional rendering partial views, in that I used the internal implementation of the Html.RenderPartail(…) method to create the Html extension. Later I found a simple way to achieve the same using Action<T> delegate

<p>Partial rendering with Action Methods</p>        
<% Html.PartialIf(this.Model.Exists, html => html.RenderPartial("MyPartialView")); %>

If you look at the “PartialIf” implementation, it is simple, cleaner than the previous technique I have mentioned in my post.

public static void PartialIf(this HtmlHelper htmlHelper, bool condition, Action<HtmlHelper> action)
{
    if (condition)
    {
        action.Invoke(htmlHelper);
    }
}

That’s it :)


ASP.Net MVC - Conditional rendering Partial Views

Posted on: 21 Jan 2010 | Filed under: .NET, ASP.NET, MVC | Tagged under: ASP.NET, MVC | comments (2)

Update : Later I found a cleaner and simple approach to do the same – read this post ASP.Net MVC – Conditional rendering Partial Views with Action<T> delegate

Following my previous post about Conditional Rendering, one of my colleague asked me how to render the partial view based on a condition.

Normal way of doing this is

<p>Normal partial rendering based on condition</p>           
<% if(this.Model.Exists)
 {
     Html.RenderPartial("MyPartialView");
 } %>

I am not sure about any other technique for rendering partial view conditionally other than this (correct me if I am wrong :) ).

Then I thought about copying the pattern I have used in my previous post and came up with this code which could conditionally render partial views and you could use the Html extension like below, which more clean than the previous

<% Html.PartialIf(this.Model.Exists, "MyPartialView"); %>

Read more...

Tip for writing better NUnit test cases

Posted on: 18 Dec 2009 | Filed under: .NET, TDD | No comments yet

If you have method, which does the string reverse. So normally we writes test cases in Nunit something like this

[Test]
public void TestCase1()
{
ExString exString = new ExString();
Assert.AreEqual("dcba", exString.Reverse("abcd"));
}

[Test]
public void TestCase2()
{
ExString exString = new ExString();
Assert.AreEqual("abba", exString.Reverse("abba"));
}

This means, you are repeating the same code again and again in order to test different possibilities. The down side of this approach is, in long run it will be very difficult to manage the test case when compared to the actual code.

To solve this particular problem NUnit has got parameterised test cases attribute, so the same could be re write above test case to

[TestCase("abcd", "dcba", TestName = "Test Case 1")]
[TestCase("abcd1", "1dcba", TestName = "Test Case 2")]
[TestCase("aaaa", "aaaa", TestName = "Test Case 3")]
[TestCase("abba", "abba", TestName = "Test Case 4")]
public void Can_Reverse_A_Text(string actual, string expected)
{
ExString exString = new ExString();
Assert.AreEqual(expected, exString.Reverse(actual));
}

This gives a clean way for testing different possibilities, all these shown in the NUnit test runner as different test cases like below.

image

Hope this will help in some way or other


Code generation - An easy way

Posted on: 05 Jun 2009 | Filed under: .NET | No comments yet

Recently I read about Text Template Transformation Toolkit (T4), I was really amazed by this because I thought only CodeDOM was the only way to generate code.

Text Template Transformation Toolkit (T4) is template based code generation engine which comes with Visual Studio 2008(It is not a framework feature).

This simple example shows how powerful it is -

if you have a string array and you want to generate a strongly typed enum from this.

Steps

1) Open up VS 2008 and Create a project.

Read more...