Tuesday

C# Anonymous methods , "generic" exception handling

this is why C# Anonymous methods are cool :
Below is one of top classes of winservice/remote rule application server.
Methods of this top class must handle exception and be rock-stable.
Instead of handling exception in every overloaded method "Rule"
I can pass anonymous method into one/major and handle exception there.
Here's sample :


using System;

using System.Collections.Generic;

using System.Text;



namespace BusinessRules.Reactor

{

    /// <summary>

    /// Control panel is central dispatcher and  Rules-Runner

    /// </summary>

   public class ControlPanel

    {

       public Result Rule(int EnityType, string ReferenceID, int ruleid)

       {

           BasicProperties p = new BasicProperties();

           p.ReferenceID = ReferenceID;

           Result res = Rule(p, delegate() {return Storage.getRule(Storage.GetEntityID(EnityType, ReferenceID), ruleid); });          

           return res;

       }

      

       public Result Rule(RuleProperties RuleProperties ,int EnityType, string ReferenceID,int ruleid){            

           return Rule(RuleProperties, delegate() {

                                                      return

                                                          Storage.getRule(Storage.GetEntityID(EnityType, ReferenceID),

                                                                          ruleid);});

       }

      

       public Result Rule(RuleProperties RuleProperties, int ruleid) {

           return Rule(RuleProperties, delegate() { return Storage.getRule(ruleid); });

       }



      

       public delegate ConcreteRule GetRule();

       public Result Rule(RuleProperties RuleProperties, GetRule rule_get){

            Result res= new Result();

            try

            {

                ConcreteRule rule = rule_get();

                rule.Init((BasicProperties)RuleProperties);

                rule.ApplyRule();

                res = rule.Result;



            }catch (AssertionException aex){                

                                

                res.ExeptionHappend = aex.ToString();

                res.Status = enResultStatus.Collision;

                res.Description = aex.Message;

            } catch ( Exception ex) {



                res.ExeptionHappend = ex.ToString();

                res.Status = enResultStatus.UnhandledException;

                res.Description = ex.Message;

            }

            return res;

        }





    }

}


Sample of code evolution from C#1.1 into C#3.0

Sample of code evolution from C#1.1 into C#3.0
C# 3.0 : Evaluation of Lambda Expression to Language INtegrated Query (LINQ): "From code name “cool” to C# 3.0, it’s been a long journey for this amazing language with .NET Runtime. Here I am going to show you the evaluation step by step."

you might feel a bit like Alice falling into Rabbit hole :)

to fix this exception System.Runtime.Serialization.SerializationException

to fix this exception : System.Runtime.Serialization.SerializationException: The constructor to deserialize an object of type was not found.
Add empty/default constructor and if error still appear add specific constructor like in class below:

 [Serializable]

 public class AssertionException : ApplicationException {

 public AssertionException(SerializationInfo info, StreamingContext context) : base(info, context) {}

 public AssertionException() : base() { }

Blogging from VIM with source code snipplets

1.In Visual or Normal mode use :TOHtml command to get html representation of source code you have selected .
2.in opened buffer surround text with this div:
<div style="background-color:#103040; overflow:scroll;height:200px"> <font color="#e0eee0">
-- created html code here --
</font></div>
3.root font tag has specific color for my vim-color it's "#e0eee0"
and you will have post with source like that :


using System;

using System.Collections.Generic;

using System.Text;



    public abstract class CustomAttribute : Attribute { }



    public class ExcludeFromToXml : CustomAttribute { }

    public class FoldStorage : CustomAttribute{}

    public class FoldableClass : CustomAttribute { }

    public class Folded : CustomAttribute

    {

        public string IntoProperty;

        public Folded(string intoProperty){

            IntoProperty = intoProperty;

        }

    }



    public class CollectionProperty : CustomAttribute { }



    public class EnumeratedProperty : CustomAttribute

    {

        private Type m_enumType;

        public virtual Type EnumType

        {

            get { return m_enumType; }

        }



        public EnumeratedProperty(Type inEnumType)

        {

            m_enumType = inEnumType;

        }

    }







Read how to post from vim into blogger.com over python API in my next post.

Monday

Writing Attributess for extending business classes and found interesting doc on msdn : Attributes Tutorial (C#)

I'm writing custom attributess for extending business classes and found interesting doc on msdn :
Attributes Tutorial (C#): "This tutorial shows how to create custom attribute classes, use them in code, and query them through reflection."

Saturday

How to create abstract object with serialization methods.

C#,CSharp : it will be easy to serialize/deserialize inherited object into xml string
   23     abstract public class AbstractXMLObject
   24     {
   25         public string XML
   26         {
   27             get
   28             {
   29                 XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
   30                 StringWriter stringWriter = new StringWriter();
   31                 xmlSerializer.Serialize(stringWriter, this);
   32                 return stringWriter.ToString();
   33             }
   34             set
   35             {
   36                 XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
   37                 StringReader stringReader = new StringReader(value);
   38                 this = xmlSerializer.Deserialize(stringReader);
   39             }
   40         }
   41     }

C# threading , msdn examples/explanations :

C# threading , msdn examples/explanations :

How to: Use a Thread Pool (C# Programming Guide):
How to: Create and Terminate Threads (C# Programming Guide)
How to: Synchronize a Producer and a Consumer Thread (C# Programming Guide)

Good unit-tests have ... : A-TRIP rule (quote from one book about unit testing ,don't remember the title)

Good unit-tests have ... : A-TRIP rule (quote from one book about unit testing ,don't remember the title)
Good unit-tests have the following properties, which makes them
A-TRIP:
• Automatic
• Thorough
• Repeatable
• Independent
• Professional

C#(CSharp/dot.net 2.0/Micsrosoft Visual Studio 2005)How to read string xml into dataset:

C#(CSharp/dot.net 2.0/Micsrosoft Visual Studio 2005)How to read string xml into dataset:

   24 string ready= (String.Format("{0}", str));
   25 Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes(ready));
   26 DataSet ds = new DataSet();
   27 ds.ReadXml(s);

imagemagic add text to image

rem different types of text annotations on existing images rem cyan yellow orange gold rem -gravity SouthWest rem draw text and anno...