On this page

Automating development process
Sequence implementation in SQL server 2000
Why did Shimon stoped posting ?
Wrapping Unhandled exceptions from a WinForm application
Static objects in aspx page - Bad idea
Session hijacking - prevention oriented
.Net 3.0 - an architectural update for the .Net framework
Does following Microsoft guidelines(or any guidelines..), is Always a good idea ?
hierarchy implementation in SQL server 2000

Ads

Navigation

Search

Categories

Clouds

Sql Server (5) .Net (16) .Net 2.0 (2) C# (3) @ff Topic (5) Architectural solutions (9) ASP (1) BDD (5) Blog related (8) database (2) Development process (8) Facebook (1) job interviews (1) Lessons (5) Life (12) Microsoft (5) IIS 6 (2) SPS (sharepoint server) (3) Drivers (1) Internet Explorer (2) Windows 2003 server (1) NightDuck (2) Performance (5) Security (9) Sql Server 2000 (4) Study (2) TDD (1) Threading (3) Under the hood (1) Web (1) Web services (1) XSS (6)

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 63
This Year: 7
This Month: 0
This Week: 0
Comments: 33

Sign In
Pick a theme:

 Wednesday, October 17, 2007
Wednesday, October 17, 2007 10:15:49 AM (GMT Standard Time, UTC+00:00) ( .Net | Architectural solutions )

As part of a development process, deployment, if handled wrong, may consume much of your precius time.
my goal is to automate as much as possible the deployment process.

I've read recently a post by Chris Burrows, which talks about "Setting Up Your Build Environment with TFS".
I strongly suggest paying him a visit, it contains many insights about the process.

he talks about :

  • Continuous Integration
  • Dependency Replication
  • Automated Unit Testing and Code Coverage
  • Automated Deployment
  • Build Verification Tests
  • Deployment Verification Tests
  • this post is more as a question than just plain info,
    How do you practice these subjects ?
    Which tools/templates/methodologies do you use to elevate your process ?

    Comments [0] | | # 
     Wednesday, July 18, 2007
    Wednesday, July 18, 2007 7:06:10 PM (GMT Standard Time, UTC+00:00) ( Sql Server  | Architectural solutions | Sql Server 2000 )

    for those folks who worked with oracle, know that sequence is an internal database object which has it's own methods and properties.
    hey, why shouldn't i use this wonderful auto number feature that sql server has ?
    well, auto number will give you a good solution to most of your needs that a sequence provided in oracle.
    but, here is some conditions where auto number just won't suffice :

    • Managing more than one "auto number" in one table - auto number can be defined only at one field.
    • Managing the same auto number across multiple tables - the auto number can be asigned only to a specified table.

    the solution is simple:
    we will create some framework that will allow us to manage sequences and work with them.

    first we will create one table to contain the sequences.

    CREATE TABLE [dbo].[sysSequences] (
        [SequenceName] [char] (30) COLLATE Hebrew_CI_AS NOT NULL ,
        [Incrasement] [int] NOT NULL ,
        [StartValue] [int] NOT NULL ,
        [CurrentValue] [int] NOT NULL
    ) ON [PRIMARY]
    GO

    next thing we need to do is create some stored procedures that will handle these sequences.

    the create sequence procedure :

    CREATE procedure dbo.sp_Create_Sequence
    (
        @seqName     varchar(30),
        @Increasement     int,
        @startValue    int
    )
    as
    begin

    declare @r int
    begin transaction

        -- Check if the sequence exists
        set @r =(    select count(*)
        from sysSequences where SequenceName = @SeqName)
        
        if @r <> 0 --if error is raised
        begin
            goto LogError
        end

    insert into sysSequences (SequenceName,Incrasement,StartValue,CurrentValue)
    values (@seqName,@Increasement,@startValue,@startValue)    

    commit transaction
    goto ProcEnd

    LogError:
    rollback transaction
    RAISERROR ('Can not create sequence with name %s, sequence exists already.',16,1,@seqName)

    ProcEnd:
    end

    once we created the "object" (not really an object, more as a row in the sequence table), lets get his current value

    create procedure dbo.sp_SequenceCurrVal
    (
        @seqName     varchar(30)

    )
    as
    begin

    declare @rows int

    begin transaction
        -- Check if the sequence exists
        set @rows =(    select count(*)
        from sysSequences where SequenceName = @SeqName)
        
        -- if no such row
        if @rows = 0
        begin
            goto LogError
        end

        -- Get the new value
        select CurrentValue from sysSequences
        where SequenceName = @SeqName
        
    commit transaction
    goto ProcEnd

    LogError:
    rollback transaction
    RAISERROR ('sequence named %s, does not exists.',16,1,@seqName)

    ProcEnd:
    end

    and the "get next value" method (very similar to the get current procedure.)

    create procedure dbo.sp_SequenceNextVal
    (
        @seqName     varchar(30)

    )
    as
    begin

    declare @rows int

    begin transaction
        -- Check if the sequence exists
        set @rows =(    select count(*)
        from sysSequences where SequenceName = @SeqName)
        
        -- if no such row
        if @rows = 0
        begin
            goto LogError
        end


        -- Update the sequence increasement
        update sysSequences
        SET CurrentValue = CurrentValue + Incrasement
        where SequenceName = @SeqName

        -- Get the new value
        select CurrentValue from sysSequences
        where SequenceName = @SeqName
        
    commit transaction
    goto ProcEnd

    LogError:
    rollback transaction
    RAISERROR ('sequence named %s, does not exists.',16,1,@seqName)

    ProcEnd:
    end


    thats it, done.
    now we have some sql server sequences

     

    Comments [0] | | # 
     Saturday, June 09, 2007
    Saturday, June 09, 2007 9:51:44 PM (GMT Standard Time, UTC+00:00) ( .Net 2.0 | Architectural solutions | Life | NightDuck )

    No, I'm no retiring from the coding field...
    actually I'm in the opposite direction.
    the last few months I've been working on a new system thats about to go online in the next few months.
    busy as hell in architecture issues, coding issues and more...
    i decided to take the opportunity of this project and Finlay get into the asp.net 2.0 .
    so i tried to do some coding here

    Convert.ToDotNet2(Shimon);

    but that only returned an exception of InvalidCasting.
    so, i sat on my bottom and started to learn all that is to know to introduce myself to the new features (yes, i know it's about time).

    from what i can see, asp.net 2.0 will introduce to the world a lot of "drag&drop" programmers.
    take for example the login controls that come built-in:

    • it's great for the beginner programmer.
    • this is one well of a !#@#!!# for an architect.

    i mean, they coupled a GUI to a "generic" DB structure, Why ???
    what if i want to manage my roles and permissions in other manner (say hierarchical roles...),
    or just manage the user in my DB with all the other tables so i can do some easy data mining ?
    to do that i need to write some providers to the controls that equals to implementing the business logic from scratch.
    i will extent later on that matter (some other post...)

    building this project as a one man show gave me an oppertunity of a vast view on a system besides playing with one module.
    from now on, i'll continue update here the progress of the project.

     

    Comments [0] | | # 
     Sunday, March 04, 2007
    Sunday, March 04, 2007 6:35:22 PM (GMT Standard Time, UTC+00:00) ( .Net | Architectural solutions | Threading )

    couple weeks ago, my friend and co-worker, shani raba, presented me a problem.
    they have some sealed application as an .exe file that throws some exceptions and crashes their application.

    So, i Thought that is a good idea to wrap the targeted application with reflection, and catch the unhandled thrown exceptions.

    to demonstrate this,
    I've created an application with a button that throws an exception.

    private void button1_Click(object sender, System.EventArgs e)
    {
        throw new Exception("my Exception, need to be wrapped");
    }

    the next thing that we need to create is the wrapper.

    so, creating a console application with this code should have solved the problem:

    [STAThread]
    static void Main(string[] args)
    {

        Assembly assembly = Assembly.LoadFrom ("cashTester.exe");

        Type t = assembly.GetType("cashTester.Form1");
        object o = Activator.CreateInstance(t);
        try
        {
            Application.Run((Form)o);
        }
        catch(Exception ex)
        {
            Console.Write("exception was thrown : " + ex.Message);
        }
    }

    running this code in debug mode successfully catch the exception from the winform.
    but, for some reason, in a normal run, this code won't catch the exception.

    makes you wonder, huh ?

    so i did some thinking, what on earth can make this phenomena ?
    The answer is : Threads.
    Yes, like it or not, but this is the subject that everyone tries to avoid it.
    everyone knows it exists, and no one really likes it,
    but we can't run from the problem, we need to confront it.

    So, what can we do ?

    since Application.Run launches a new thread, we can add to the Application.ThreadException event handler, an exception handling method.

    like this class :

     

    /// <summary>
    /// The Wrapper class
    /// </summary>
    public class Wrapper
    {

        /// <summary>
        /// Public cunstructor
        /// </summary>
        public Wrapper()
        {
        }

        /// <summary>
        /// This function will initialize the exception handling
        /// </summary>
        public void Init()
        {
            // define handlers for unhandled exceptions
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.exp);
            Application.ThreadException +=new System.Threading.ThreadExceptionEventHandler(this.ThreadExp);
        }

        /// <summary>
        /// This method is for the threads exceptions
        /// </summary>
        /// <param name="o">the object</param>
        /// <param name="args">Thread exception args</param>
        void ThreadExp(object o, System.Threading.ThreadExceptionEventArgs args)
        {
            // Write the message to the console
            Console.Write("Unhandled thread exception was thrown : " + args.Exception.Message);
        }

        /// <summary>
        /// This method is for the unhandled exceptions from the main thread
        /// </summary>
        /// <param name="o">the object</param>
        /// <param name="args">exception arguments</param>
        void exp(object o,System.UnhandledExceptionEventArgs args)
        {
            // Write the message to the console
            Console.Write("Unhandled exception was thrown : " + ((Exception)args.ExceptionObject).Message);
        }

        public void Run()
        {
            // Load the assembly
            Assembly assembly = Assembly.LoadFrom ("cashTester.exe");

            // get the type of the object
            Type t = assembly.GetType("cashTester.Form1");

            // invoke it
            object o = Activator.CreateInstance(t);

            // Run the application - note that this line starts an additional thread
            Application.Run((Form)o);
        }
    }

     

    now, all we need is to launch it :

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Wrapper w = new Wrapper();
        w.Init();
        w.Run();
    }

     

    now we got an exception wrapper for launching applications.

    Shani, tell me if that helped ...

    p.s.
    There are more ways to do it, but this is the simplest one

    Comments [2] | | # 
    Sunday, March 04, 2007 12:17:48 PM (GMT Standard Time, UTC+00:00) ( .Net | Architectural solutions )

    Yesterday, good friend of mine, Lev rosenblit, asked me a good question.
    what are the life cycle of static objects in an aspx page.
    so, at first without any hesitation, i answered that the object will die after the request event ends.
    the dude insists that I'm wrong on that matter, so i decided to check it out .

    here is some code snippet to check it out :

            private static int myStaticInt = 0;
            private void Page_Load(object sender, System.EventArgs e)
            {
                myStaticInt++;
                Response.Write(myStaticInt);
            }

    by theory (which is objects in an aspx page die after the end of the request), this code should print the number "1" on each request.
    but the result was different, on each request, the result increased.

    digging a little on the web, i found that static objects live inside the App domain and not in the page context.
    This interesting fact raised some interesting question, an architectural one:
    what would happen if the code run in NLB configuration (Network Load Balancing)?

    on that matter their is a session state issue which is solved by getting the session state from a shared database,
    but what about the static objects that live inside the App domain ?
    what can be done to share those object ?

    from my point of view, this is a bad choice in architecture when you choosing to use static objects inside your page.
    thus, it won't always work, and will be hardware Dependant system.

    Comments [0] | | # 
     Friday, March 02, 2007
    Friday, March 02, 2007 1:32:40 PM (GMT Standard Time, UTC+00:00) ( .Net | Architectural solutions | Lessons | Security | XSS )

    Well,
    after months I'm talking about it and couple of sleepless nights to prepare it, here it is.

    So, what do have here ?

    1 powerPoint presentation that explains what is Session Hijacking and XSS.
    1 XSS demo.
    0 Session hijacking Demo .... (why you ask ? - I'm planning to do another lecture on the topic for some those who missed it yesterday...)
    0 Code included. (like the demos ....will be uploaded some time soon....)

    long story short :
    this presentation talks about the threats in Session hijacking.
    and how to prevent it. - while its not a 100% solution, it certainly raises the bar for hackers to succeed in the session hijacking attack.

    some wise man said to me once,
    "for every defense that ever made, someone thought how to break it,
     the only thing we can do, is to narrow down the amount of people that is capable to perform it."

    enjoy.

    comments are welcomed :)

    Security.pps (324 KB)

     

    Comments [0] | | # 
     Monday, October 16, 2006
    Monday, October 16, 2006 8:42:27 PM (GMT Standard Time, UTC+00:00) ( .Net | Architectural solutions )

    an Interesting article I've read about the next .net framework,
    apparently the new framework wont contain such drastic changes like .Net 1.1 VS 2.0 at the CLR or at the language features,
    but, instead it will bring some architectural solutions for different tasks such as distributed programming, authentication, presentation and probably many more.

    check out this article about the issue

    Comments [0] | | # 
     Thursday, August 17, 2006
    Thursday, August 17, 2006 8:54:25 PM (GMT Standard Time, UTC+00:00) ( .Net | Architectural solutions | SPS (sharepoint server) | Microsoft )

    when planning a software solution, one of the most important things is to design the solution the best way it can be,
    even before writing the first line of code, we need to know exactly how we should implement it.

    so what would you do if you never planned/worked/implemented such type of a solution ?

    needless to say that inventing the wheel all over again will be unnecessary ,
    first you consult with the "elders" (the more experienced co-workers),
    trying maybe google up the solution,
    going over the references and the guide lines in developing this kind of a solution or at the given platform,
    starting some thinking team and so ...

    finally, you came up with a solution, and....
    start implementing it.

    now, in a perfect world, once you did the steps mentioned above, you have a perfect solution.
    but in the real world, something must to go wrong.

    recently i came across some interesting example of this phenomena.
    while designing a "file Version manager solution" on SPS (Share point Server) platform,
    following the SPS development guidelines, which says that you (the developer), should never approach the SPS database.
    always, but always get the data through the object model.

    so, by doing that, you came up with a working application,but..... catastrophic performance.
    after consulting with other staff, we came to the conclusion that the bottleneck is in the object model itself.
    so the only thing we had to do is the direct approach to the database ( a big NO NO on the guide lines).
    and apparently the performance graph suddenly got a dramatic change (better performance).

    i think that guide lines did not meant to become laws, but to be just as a suggestions to most of the cases.
    what do you think about this issue ?

    Comments [2] | | # 
     Wednesday, August 16, 2006
    Wednesday, August 16, 2006 10:47:18 PM (GMT Standard Time, UTC+00:00) ( Sql Server  | Architectural solutions | database | Sql Server 2000 )

    remember this neat syntax that exists in oracle database for hierarchy selects ?
    actually , its pretty simple :

    SELECT last_name, employee_id, manager_id, LEVEL
    FROM employees
    START WITH employee_id = 100
    CONNECT BY PRIOR employee_id = manager_id;


    ever tried to do the same in sql server ?
    well, these kind of syntax just does not exist,
    we need to work very hard to create such a feature in our database.

    so, what do we have:

    • a table that contains entities
    • each entity connected to some father entity
    • each connection describes a "father - son" relation between the two entities

    lets not forget the things we need to relay on , when implementing :

    • what will happen when we will delete the father of some sub tree ?
    • what should we do when we update/add a record ?
    • how will we select the data ?
    • algorithm efficiency is crucial, if we will need to wait 5 minutes for the data, it's not worth it

    the first (but apparently the worst) idea that came to my mind is recursion
    lets look at this table :

     

    EmployeeID Name BossID

    1

    shimon

    NULL

    2

    yossi

    1

    3

    Gaby

    1

    4

    koby

    3

    5

    jack

    3

     

    we have we that the employee shimon is the "big boss" (because there is no other boss above him),
    under shimon we have the employees Gaby and yossi,
    and under Gaby, we have another 2 employees : Koby and jack

    the recursive solution is to write some stored procedure that will receive the employeeID and return as a data-Table the results
    i will not add the code for this solution and surely will not recommend it because it was many problems :

    • for each record we received as a descendant ,
      we need to run with the function and get her descendants,
      and so on, until there are no descendants for the node
    • we are limited to 32 levels of hierarchy
    • the runtime will depend on the row count that is in the table (we will need to run on each of the rows one at the time)
    • the run on the node will look like this :
      heirarchy.jpg

    by the way, the most common way that I've seen to select hierarchical structure,
    is simply by setting a join between the levels in the select query.
    for example :

    SELECT TopBoss.Name TopBoss, Boss.Name Boss, Employees.Name Employee
    FROM Employees
    INNER JOIN Employees AS Boss ON Employees.BossID=Boss.EmployeeID
    INNER JOIN Employees TopBoss ON Boss.BossID=TopBoss.EmployeeID

    this apply to the selection of three levels

    For each level, you'd need to join the table to itself...not an attractive option if you have 5 or more levels ,
    you don't know how many levels you will have to select, there is no way can control it!
    It would be great if it could join itself as many times as needed. This is called a recursive join, and though some database products support it (Oracle has the CONNECT BY syntax) SQL Server is not one of them.

    the other way is based on a thread that i read here about hierarchies,

    lets create a table :

    CREATE TABLE Tree (
    Node int NOT NULL IDENTITY(100, 1),
    ParentNode int,
    EmployeeID int NOT NULL,
    Depth tinyint,
    Lineage varchar(255) )

    the extra fields that has been added are the "lineage" and the "depth"

    • Depth - for saving the current depth of the record in the hierarchy
    • Lineage - for saving all the ancestors of the record as a concatenated string

    after filling the needed data for relations, the table looks like this :

    Node ParentNode EmployeeID Depth Lineage
    100 NULL 1001 NULL NULL
    101 100 1002 NULL NULL
    102 101 1003 NULL NULL
    103 102 1004 NULL NULL
    104 102 1005 NULL NULL
    105 102 1006 NULL NULL

    The next part is to find the root node of the tree, also known as the top-level, etc.
    That's the node that has no parent (Null), so we will start there and set the Lineage column as the root:

    UPDATE Tree SET Lineage='/', Depth=0 WHERE ParentNode Is Null

    Once we did that,
    we can then update the rows who are the descendant of the root node:

    WHILE EXISTS (SELECT * FROM Tree WHERE Depth Is Null
       UPDATE T SET T.depth = P.Depth + 1, 
       T.Lineage = P.Lineage + Ltrim(Str(T.ParentNode,6,0)) + '/' 
       FROM Tree AS
       INNER JOIN Tree AS P ON (T.ParentNode=P.Node) 
       WHERE P.Depth>=0 
       AND P.Lineage Is Not Null 
       AND T.Depth Is Null

     

    this loop will run once for each level of the hierarchy (not for each node as the recursion method.)
    so, with data representation of 10,000 records with 8 levels of hierarchy,
    this code will run only 8 times to populate the needed data of the "lineage" field and the "depth" field, and this "heavy" procedure will happen only once at the setup.
    the table should look like this after the given operation :



    Node ParentNode EmployeeID Depth Lineage
    100 NULL 1001 0 /
    101 100 1002 1 /100/
    102 101 1003 2 /100/101/
    103 102 1004 3 /100/101/102/
    104 102 1005 3 /100/101/102/
    105 102 1006 3 /100/101/102/

     

    You'll notice that for each node, the entire lineage back to the root is stored. This means that finding someone's boss, or their boss' boss, doesn't require any self-joins or recursion to create an indented list. In fact, it can be accomplished with a single SELECT.

    SELECT Space(T.Depth*2) + E.Name AS Name
    FROM Employees E
    INNER JOIN Tree T ON E.EmployeeID=T.EmployeeID
    ORDER BY T.Lineage + Ltrim(Str(T.Node,6,0))

     

    maintaining the table is really not a big deal if we will use triggers.
    think about the new inserted record as the row that has not been filled in the setup process.
    so the insert trigger should be :

    UPDATE T SET T.depth = P.Depth + 1,
    T.Lineage = P.Lineage + Ltrim(Str(T.ParentNode,6,0)) + '/'
    FROM Tree AS T
    INNER JOIN Tree AS P ON (T.ParentNode=P.Node)
    WHERE P.Depth>=0
    AND P.Lineage Is Not Null
    AND T.Depth Is Null

     

    the update trigger should do pretty much the same : building the 2 extra field all over again.

    suggestions and request will be repplied :)

    Comments [0] | | #