Saturday, August 22, 2020

Kubernetes

Wednesday, December 3, 2008

Efficient Datalist Data Binding

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BordingNet.W2P.UserControls;
using BordingNet.BusinessLayer;
using System.Data;

namespace BordingNet.UserControls.W2P
{
public partial class ImageList : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.uxImageList.ItemDataBound += new DataListItemEventHandler(uxImageList_ItemDataBound);

if (!Page.IsPostBack)
{
SetListdataSource();
}
}

void uxImageList_ItemDataBound(object sender, DataListItemEventArgs e)
{
string fullPictureName = ((DataRowView)e.Item.DataItem).Row["FSReference"].ToString();
string[] pictureName = ((DataRowView)e.Item.DataItem).Row["FSReference"].ToString().Split('\\');
(e.Item.FindControl("uxPictureName") as Label).Text = pictureName[pictureName.Length - 1];
(e.Item.FindControl("uxPicture") as Image).ImageUrl = fullPictureName;
}


#region Helper Function
private void SetListdataSource()
{
this.uxImageList.DataSource = BusinessLogicLayerManager.GetManager().Image.GetImages(1, 69).DefaultView;
this.uxImageList.DataBind();
}
#endregion
}
}


Here Datalist databing is shown. I have a datalist named uxImageList. This list display images.
I can do it with the eval(....) function as inline coding. But It is efficient to bind in the

void uxImageList_ItemDataBound(object sender, DataListItemEventArgs e)

events. Because it is possible to edit data as one like.

Tuesday, March 4, 2008

NHibernate started

LINQ to Object Discussed

LINQ
LINQ(Language Integrated Query) is a new technology with Dot Net framework 3.5 support Querying Data. That means it support Querying Collection, XML, SQL table, Dataset and other objects.

In this post I will discuss LINQ to Object.

LINQ to Object
The functionality of LINQ to object is accomplished through the use of IEnumerable<T> interface, Sequence and Standard Query Operator.

IEnemerable<T>: IEnemerable<T> is an interface that all the C# 2.0 Generic collection implements. This collection permits the enumeration of collection elements.

Sequence: Sequence is a logical term if you have a variable of type IEnemerable<T> then you can say you have a sequence of T.

Standard Query Operator:

Most of the standard query operator are Extension methods in the System.Linq.Enumerable static class with an IEnemerable<> as there first argument.
Extension Method: An extension method is a static method of a static class that you can call as though it were an instance method of a different class.

public static class StringConversion
{
public static double ToDouble(this String s)
{
return Double.Parse(s);
}
}

Here ToDouble is an extension method. Extension method have this Keyword preceding first parameter.






Monday, March 3, 2008

Lamda Expression Details

Lamda expression
Lamda expression is an anonymous function which can contain expressions and statement. It can be used in delegates. Lamda expression use Lamda(=>) operator.

Consider the following example



  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;


  5. delegate int TestDelegate(int a,int b);

  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.WriteLine("e");
  13. TestDelegate Deltest = (x, y) => x + y;
  14. var result = Deltest(5, 10);
  15. Console.WriteLine(result);
  16. }
  17. }
  18. }
Here in line (16) I have used lamda expression

TestDelegate Deltest = (x, y) => x + y;
The left side of the Lamda operator are the input parameter and the right side of the Lamda operator are the expression.

Here the expression has two input parameters (x,y) and returns int, So it conforms to the deleg
ate when we call

Deltest(5,10);

we get the result 15.

But if we call DelTest(5.0,10) it will generate compile time error. Because it will violate the
TestDelegate signature.

Note: All the restrictions applied for anonymous method also be applied for Lamda expression.


Expression Lamda:
A lamda expression with an expression on the right hand side is called the expression lamda.

(x,y)=>x+y
is an expression lamda.

Statement Lamda
Statement lamda is like expression lamda with that statement is enclosed with braces.




The general rules for lambdas are as follows:

  • The lambda must contain the same number of parameters as the delegate type.

  • Each input parameter in the lambda must be implicitly convertible to its corresponding delegate parameter.

  • The return value of the lambda (if any) must be implicitly convertible to the delegate's return type.


Variable Scope in Lamda expression

The following rules apply to variable scope in lambda expressions:

  • A variable that is captured will not be garbage-collected until the delegate that references it goes out of scope.

  • Variables introduced within a lambda expression are not visible in the outer method.

  • A lambda expression cannot directly capture a ref or out parameter from an enclosing method.

  • A return statement in a lambda expression does not cause the enclosing method to return.

  • A lambda expression cannot contain a goto statement, break statement, or continue statement whose target is outside the body or in the body of a contained anonymous function.

Tuesday, February 26, 2008

.NET Threading


Threading
Thread is independent stream in a program. Generally dot net program starts with the first statement in the main() method and continue running until reaches to return statement. Sometimes we need to run several task to run simultoneously. Because, Sometimes we have to wait a large amount of time to run some small task for a large task to be completed. This can be optimized by using threading. We can implement Threading by using System.Threading namespace.

Manipulating Threads
We can easily manipulate threads in a program by using System.Threading namespace.
I will discuss this by the following c# console application



using System;

using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ThreadManipulate
{
class Program
{
static void Main(string[] args)
{
Thread mainThread = Thread.CurrentThread;
mainThread.Name = "Main Thread";
ThreadStart customThreadStart = new ThreadStart(show);
Thread customThread = new Thread(customThreadStart);
customThread.Name = "Custom Thread";
customThread.Start();
show();
Console.ReadLine();
}
private static void show()
{
for (int i = 1; i <>
{
Console.WriteLine("This is "+ Thread.CurrentThread.Name);
Thread.Sleep(100);
}
}
}
}


First we have to include the System.Threading namespace. This namespace will provide us the functionality needed for Threading.

Then I manipulate the current thread (main thread) by the

Thread mainThread = Thread.CurrentThread;

statement. CurrentThread is a static readonly property of class Thread that returns the current running thread. I assign the current thread to the mainThread variable.

I also give the main thread a name "Main Thread"

mainThread.Name = "Main Thread";



Then I have created and run a custom thread by the statement

ThreadStart customThreadStart = new ThreadStart(show);

Thread customThread = new Thread(customThreadStart);

customThread.Name = "Custom Thread";
customThread.Start();

ThreadStart is a delegate that represents a method that run on System.Threading.Thread.

Then created a thread named customThread, I also give it a name "Custom Thread".

The show() method is the method that is accessed by both the thread.


when run this program output result is

This is Custom thread
This is Main thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Main thread
This is Custom thread
This is Custom thread
This is Main thread

The output shows that two threads are running simultoneously.
So this will be very useful in case of large resources.


Control thread
Thread can be suspended by using the statement
customThread .Suspend();
Can be resumed by
customThread .Resume();
Can be aborted by
customThread .Abort();




Thread Priority
Sometimes you may want to give more processing time to an important thread than the other less important thread. You can set Priority for a thread uising ThreadPriority enumeration.

The Threadpriority enumeration contains five values

Lowest The Thread can be scheduled after threads with any other priority.
BelowNormal The Thread can be scheduled after threads with Normal priority and before those with Lowest priority.

Normal The Thread can be scheduled after threads with AboveNormal priority and before those with BelowNormal priority. Threads have Normal priority by default.
AboveNormal The Thread can be scheduled after threads with Highest priority and before those with Normal priority.
Highest

You can set these values like this
customThread.Priority = ThreadPriority.AboveNormal;



Synchronization
Sometimes shared variables may be accessed by more than one thread, this will result unexpected problem.
Fortunately C# provide easy way to maintain syncronization. C# provide a keyword Lock for the purpose

Lock(x)
{
Dosomething();
}

What the lock statement does is wrap an object known as a mutual exclusion lock, or mutex, around the variable in the round brackets. The mutex will remain in place while the compound statement attached to the lock keyword is executed. While the mutex is wrapped around a variable, no other thread is permitted access to that variable. You can see this with the preceding code; the compound statement will execute, and eventually this thread will lose its time slice. If the next thread to gain the time slice attempts to access the variable x, access to the variable will be denied. Instead, Windows will simply put the thread to sleep until the mutex has been released.

Synchronization should only be used where it is necessary.