Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Sunday, February 12, 2012

State Pattern

I am learning about design patterns in depth these days. Amazed by the solutions they give .. Some patterns like abstract factory , factory made sense to me but could not find immediate application to my code .. so could not get that uahaa feeling ;) When i read about the state pattern http://blog.raffaeu.com/archive/2011/02/13/state-pattern-using-c-part-01.aspx .. i found an immediate use for this in all my code. I realized how my code is tightly coupled all these days .. Implemented this and now i have a happy feeling .. I learned something new and implemented it . .................. :) :)

I have a class which as states such as new edit view .. I moved all the logic related to this states using this pattern :)

Monday, January 30, 2012

Getting Class Name and Method name using reflection

To get the current method and class name in which the code is executing, we can use reflection.

Method Details

System.Reflection.MethodBase.GetCurrentMethod().Name, 

Class Details
System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name

Handy to log the errors .. 

Thursday, January 26, 2012

Error :Attempted to access an unloaded appdomain

Attempted to access an unloaded appdomain. (Exception from HRESULT: 0x80131014)

This occurs when we try to open two different connections in the same  Transaction Scope . Use Transaction Scope.Suppress for one of the connection if you are not performing any transaction in one of the connection.


But if you are making transactions in both of the connections as a single one ... i need to study further about this ... probably you have to open a new transaction under the main one .. will verify later 

Wednesday, January 25, 2012

Generating Enums from Database for Lookup Tables

These are the two links i found for this problem.



http://erraticdev.blogspot.com/2011/01/generate-enum-of-database-lookup-table.html


http://idisposable.co.uk/2010/03/using-t4-to-generate-enums-from-database-lookup-tables/#viewSource





 I implemented the second one as i found it easier and more flexible in naming the enums differently from the database look-ups.I struggled with this for a few hours as i did not one thing . i.e When we add t4 template to VS make sure that it  in the properties of the t4 file the custom tool is set to "TextTemplatingFileGenerator". The default one is the text processing .. so i could not see the enum file .. Finally after some time i could identify this.




This is really nice because you find your code more readable while not loosing flexibility of look-ups. Every time you have a new look-up you have to regenerate the t4 template and build your project.


Thanks to people who wrote these templates :). It made my life easier.

Wednesday, November 2, 2011

Passing Table Valued Parameters to SQL Server Stored Procedures

Passing Table Valued Parameters to SQL Server Stored Procedures

Can also be used to pass arrays to SQL Server
1) Improves performance
2) Easy to Handle
3) Get Rids of  Changing Comma Separated values to table in SQL.

1) Works only with SQL Server 2008 and above

http://msdn.microsoft.com/en-us/library/bb675163.aspx
http://www.dotnetspeaks.com/DisplayArticle.aspx?ID=47

Friday, May 6, 2011

Handling Javascript Event Binding in Ajaxised Page.

We are using MS ajax to add a function that does the rebinds JS events to the elements.

$(window).load(function () {
 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RunThisAfterEachAsyncPostback);}
        });
This is the function that executes the rebinding code

 function RunThisAfterEachAsyncPostback(sender, eArgs) {
            try {

                if (eArgs.get_error() == null) {
// Rebinding 
                    $("#btnSaveSubScriptions").button();
                    $("#EventsCheckBoxList tbody td").find('input').click(Events_SubClick);
                }
                else {
                    alert("Error Occurred in the System.Please Contact System Admin");
                    alert(eArgs.get_error());
                }                
                
            }
            catch (err) {
                document.write("There are some errors on this page while loading...<br>" + "RunThisAfterEachAsyncPostback");
                document.write("Error is : " + err.description + " <br>Error number is " + err.number);
            }
        };

Friday, November 19, 2010

OOPS!!

Inheritance and Polymorphism .

1. Inheritance


Class Animal
{
int legs;
int color;
int sound;
}

class Lion:Animal
{
int teeth;
}

class Snake:Animal
{
int length;
}



2. Polymorphism

Early Binding and Late Binding

class Animal
{
public void Feed()
{
System.Console.WriteLine ("An animal is fed here.");
}
}
class Lion: Animal
{
new public void Feed()
{
System.Console.WriteLine ("A Lion is fed here.");
}
}
class Snake: Animal
{
new public void Feed()
{
System.Console.WriteLine ("A Snake is fed here.");
}
}
class Test
{
public static void Main()
{
Animal a = new Animal();
a.Feed(); // "An animal is fed here."
Lion Leo = new Lion();
Leo.Feed(); // "A Lion is fed here."
Snake Viktor = new Snake();
Viktor.Feed(); // "A Snake is fed here."
}
}
class Test
{
public static void Main()
{
Animal[] animals = new Animal[2]; // declare an Animal array
animals[0] = new Lion(); // Add specific animals
animals[1] = new Snake();
for (int i = 0; i < 2; i++) animals[i].Feed(); // Feed the animals } }


Running this application produces the following output:
An animal is fed here.
A Lion is fed here.
A Snake is fed here

An animal is fed here.
An animal is fed here.

What has actually happened here is that the Feed() method is bound to the animals
at compile time. This is called early binding. There is no chance to examine the actual
type of animal before feeding it. We would like the compiler to not bind the method
and allow the runtime to bind it instead. That is called late binding and is used to create
polymorphic methods.

In order to overcome the problem outlined in the previous section, we use two keywords,
virtual and override. The virtual keyword is used on the base-class method and
indicates that the method can be overridden. The override keyword is used on the derived-
class method. It means that we intend to change the behavior of the method that is
inherited.

class Animal
{
virtual public void Feed()
{
System.Console.WriteLine ("An animal is fed here.");
}
}
class Lion: Animal
{
override public void Feed()
{
System.Console.WriteLine ("A Lion is fed here.");
}
}
class Snake: Animal
{
override public void Feed()
{
System.Console.WriteLine ("A Snake is fed here."); }
}
class Test
{
public static void Main()
{
Animal a = new Animal();
a.Feed(); // "An animal is fed here."
Lion Leo = new Lion();
Leo.Feed(); // "A Lion is fed here."
Snake Viktor = new Snake();
Viktor.Feed(); // "A Snake is fed here."
}
}
Now we get the behavior we want:
A Lion is fed here.
A Snake is fed here.