its been since few of my friends were behind me for Nested ananymous functions real life sample, due to busy consulting hours and work pressure, i couldnt post this.

here u go guys ..............

copy the entity files

using System;

using System.Collections.Generic;

using System.Text;

namespace TestHarness

{

public class MenuItem

{

private int menuID;

public int MenuID

{

get { return menuID; }

set { menuID = value; }

}

private string menuText;

public string MenuText

{

get { return menuText; }

set { menuText = value; }

}

private string description;

public string Description

{

get { return description; }

set { description = value; }

}

private string url;

public string Url

{

get { return url; }

set { url = value; }

}

private int moduleID;

public int ModuleID

{

get { return moduleID; }

set { moduleID = value; }

}

public MenuItem(int menuid, string menutext, string description, string url, int moduleid)

{

this.menuID = menuid;

this.menuText = menutext;

this.description = description;

this.url = url;

this.moduleID = moduleid;

}

}

}

-------------------------------------

using System;

 

using System.Collections.Generic;

 

using System.Text;

 

namespace TestHarness

{

public class ProductKeys

 

{

private int key;

public int Key

{

get { return key; }

set { key = value; }

}

private int value;

public int Value

{

get { return this.value; }

set { this.value = value; }

}

public ProductKeys(int key, int value)

{

this.key = key;

this.value = value;

}

}

}

---------------------------------

using System;

 

using System.Collections.Generic;

 

using System.Text;

 

namespace TestHarness

{

public class UserPermissions

 

{

private int key;

private int value;

public UserPermissions(int key, int value)

{

this.key = key;

this.value = value;

}

public int Key

{

get { return key; }

set { key = value; }

}


public int Value

{

get { return this.value; }

set { this.value = value; }

}


}

}

-----------------------------------

create factoty methods to populate data

using System;
using System.Collections.Generic;
using System.Text;

namespace TestHarness

{

public class MenuFactory

{

public List<MenuItem> GetMenus()

{

List<MenuItem> menus = new List<MenuItem>();

menus.Add(new MenuItem(1, "Organisation", "", "", 1));

menus.Add(new MenuItem(2, "Persons", "", "", 2));

menus.Add(new MenuItem(3, "Accounts", "", "", 3));

menus.Add(new MenuItem(4, "Bookings", "", "", 4));

menus.Add(new MenuItem(5, "Delegates", "", "", 5));

menus.Add(new MenuItem(6, "Course", "", "", 6));

menus.Add(new MenuItem(7, "Elements", "", "", 7));

menus.Add(new MenuItem(11, "Accmodations", "", "", 8));

menus.Add(new MenuItem(14, "Reports", "", "", 9));

menus.Add(new MenuItem(15, "Help", "", "", 10));

return menus;

}

}

}

---------------------------------------

using System;

 

using System.Collections.Generic;

 

using System.Text;

 

namespace TestHarness

{

public class ProductKeyFactory

 

{

public List<ProductKeys> GetProductKeys()

{

List<ProductKeys> productkeys = new List<ProductKeys>();


productkeys.Add(new ProductKeys(1,1));

productkeys.Add(new ProductKeys(2, 1));

productkeys.Add(new ProductKeys(3, 1));

productkeys.Add(new ProductKeys(4, 1));

productkeys.Add(new ProductKeys(5, 1));

productkeys.Add(new ProductKeys(6, 0));

productkeys.Add(new ProductKeys(7, 1));

productkeys.Add(new ProductKeys(8, 1));

productkeys.Add(new ProductKeys(9, 1));

productkeys.Add(new ProductKeys(10, 0));


return productkeys;

}

}

}

-----------------------------------------

using System;

 

using System.Collections.Generic;

 

using System.Text;

 

namespace TestHarness

{

public class UserPermissionFactory

{

public List<UserPermissions> GetUserPermissions()

{

List<UserPermissions> permissions = new List<UserPermissions>();

permissions.Add(new UserPermissions(1, 9));

permissions.Add(new UserPermissions(2, 0));

permissions.Add(new UserPermissions(3, 3));

permissions.Add(new UserPermissions(4, 9));

permissions.Add(new UserPermissions(5, 9));

permissions.Add(new UserPermissions(6, 1));

permissions.Add(new UserPermissions(7, 9));

permissions.Add(new UserPermissions(8, 9));

permissions.Add(new UserPermissions(9, 9));

permissions.Add(new UserPermissions(10, 9));

return permissions;

}

}

}

 


Testharness -- to test

 

using System;
using System.Collections.Generic;
using System.Text;

namespace TestHarness

{

class Program


{

static void Main(string[] args)

{

List<MenuItem> menuItems = new List<MenuItem>(new MenuFactory().GetMenus());

List<ProductKeys> productKeys = new List<ProductKeys>(new ProductKeyFactory().GetProductKeys());

List<UserPermissions> userPermissions = new List<UserPermissions>(new UserPermissionFactory().GetUserPermissions());

int result = 0;

Console.WriteLine(menuItems.FindAll(new Predicate<MenuItem>(delegate(MenuItem mItem)

{

UserPermissions isExist = userPermissions.Find(new Predicate<UserPermissions>

(delegate(UserPermissions permissions)

{

if (permissions.Key == mItem.ModuleID && permissions.Value > 0)

{

return true;

}

return false;

}));

if (null != isExist)

result = isExist.Key;

return mItem.ModuleID == result;

})).Count);

Console.ReadLine();


}

}

}

 

I will give a little more insight tomorrow on ananymous functions and there usage on real time applications..

and gonna post loads of stuff during christmas on design patterns and biztalk ... keep a little eye on my blog :-)

 

posted on Saturday, December 15, 2007 6:12 PM

Comments

Gravatar
# re: Nested Ananymous function - real world sample
posted by Kiran Patil
on 1/24/2008 4:57 PM
Hi Prasanna,

Good to learn some more things from you after training.

Nice Work of sharing knowledge..one thought coming in my mind "Reading Book enriches your knowledge, sharing Knowledge enhances it" ..so its good that you are enhancing knowledge.

I have added your blog to my favorites.

ALL THE BEST for your tour to become a MVP.

Kiran Patil,
Baroda
Your Comment




 
Please add 1 and 5 and type the answer here: