Create instance without invoking constructor

It is not always true that constructors will be invoked when constructing an object. I actually stumbled today wondering why the no argument constructor was not invoked when deserializing object from XML using Java XStream library, and after doing some search found that there are ways to create object without calling the constructor. I wouldn’t say this is a good practice especially when we are not writing library code, but just a reminder that some code may work like this, so when dealing with object deserialization, we need to be very careful on checking whether our fields are initialized.

Continue reading

Hyperion – Section 3 – Workflow Definitions and Instances

Workflow definitions and workflow instances are similar concepts. Workflow definition is the design-time concept of an workflow, which is represented as a class. A workflow instance is a run-time concept, which is an object of the workflow definition class.

A class tagged with “Workflow” annotation is recognized as a workflow definition, which contains all transitions related to this workflow. The “Workflow” annotation contains the following attributes:

  • id – A unique GUID. Workflow definitions must have different GUIDs. This is verified by the Hyperion workflow engine on startup.
  • name – The name of workflow. If set to null, the name will be by default set to the short name of the workflow definition class. It is strongly recommended that every workflow definition should have an explict name. Workflow definition names do not need to be unique but having the same name will add ambiguity.
  • initialState – A workflow state class which will be set as the initial state of the workflow instance.
  • contextType – The class of the workflow context that will be used for this workflow definition
  • Continue reading

    Hyperion – Section 2 – No XML and pure annotation driven

    Honestly I don’t like XML when developing metadata driven applications, as that would separate the code of a piece of logic into two different files. While this defect can be bridged with the use of design tools, just as what .NET WF and jBPM does, both of them provide visualized workflow definition tool. While my framework will keep it simple, without introducing any graphical tools to assist designing the workflow. Therefore, I choose using annotation instead of XML as the metadata language when designing workflows.

    In a state workflow definition, one of the core concept is activity. e.g. in commercialized workflows, we have code activity, “if…else…” activity, while loop activity, invoke web service activity, etc. Some activities hold the business logic, while some others are only used for linking other activities to form a whole procedure, just like the “if…else…” activity. In Hyperion, all these activities are simplified into one type, which is the “code activity”, as I believe everything that can be achieved by predefined activities can also be written in code. So why we still need workflow definition? That’s because we still need to handle state transitions.

    Continue reading

    Hyperion – Section 1 – Introduction to a lightweight workflow engine

    We may have the concept of workflow in many kinds of projects, and of different scales. There are many well known workflow projects, including Microsoft .NET WF, JBPM by JBOSS, etc. Both of these workflow engines support state workflow, but when working on another project, I noticed that none of these can fit into my requirement, as what I needed was a multi-state workflow engine.

    Why do I call it multi-state? At a particular time, one workflow instance should only have one state. However the instance may have multiple aspects, and for each aspect, it can have a separate state. For example, a workflow instance represents a task that need to be done by multiple roles in parallel. One role may see this task at state A, while at the same time, another role may see this task at state B. So in fact, at a particular time, a workflow instance has a State Set associated with it.

    Continue reading

    Referencing local variable in anonymous delegates

    In C#, anonymous delegates, or lambda expressions, which ultimately is compiled into delegates, are able to access parent context, i.e. accessing variables that is not passed in as an argument. Here I am summarizing the most common cases:

    • Referencing constants
    class Program
    {
        public void Demo()
        {
            const string message = "Hello, World!";
    
            var displayMessage = new Action(() => Console.WriteLine(message));
    
            displayMessage.Invoke();
        }
    
        private static void Main(string[] args)
        {
            new Program().Demo();
        }
    }
    

    In this scenario, the message is defined as constant. After compiler optimization, we can see it is creating a static delegate field in Demo.Program class, linking to a static method b__0. In the static method, the message is directly loaded as a string.

    referencing-local-variable-in-anonymous-delegates-1

    Continue reading

    PropertyInfo selector using Lambda expressions

    Considering the Type.GetProperty() method provided in System.Reflection, it takes a string parameter. If we are writing frameworks, this is often fine as the hard coded strings are usually self contained, and won’t be exposed to developers that users our framework. While what if we need to provide a function for those developers that uses our library to specify a property? In this case, strings may not be the best solution. Simply taking one scenario, if the property is refactored to a new name, and the string isn’t changed, then the property won’t be found using the old name.

    The following example is trying to provide a way to get a PropertyInfo based on an expression. Comparing to directly using Type.GetProperty() method, this type safe, and the expression will change when the property is refactored.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
     
    namespace Demo
    {
        public static class PropertySelector
        {
            public static PropertyInfo GetPropertyInfo<T>(this T obj, Expression<Func<T, object>> selector)
            {
                if (selector.NodeType != ExpressionType.Lambda)
                {
                    throw new ArgumentException("Selector must be lambda expression", "selector");
                }
     
                var lambda = (LambdaExpression) selector;
     
                var memberExpression = ExtractMemberExpression(lambda.Body);
     
                if (memberExpression == null)
                {
                    throw new ArgumentException("Selector must be member access expression", "selector");
                }
     
                if (memberExpression.Member.DeclaringType == null)
                {
                    throw new InvalidOperationException("Property does not have declaring type");
                }
     
                return memberExpression.Member.DeclaringType.GetProperty(memberExpression.Member.Name);
            }
     
            private static MemberExpression ExtractMemberExpression(Expression expression)
            {
                if (expression.NodeType == ExpressionType.MemberAccess)
                {
                    return ((MemberExpression) expression);
                }
     
                if (expression.NodeType == ExpressionType.Convert)
                {
                    var operand = ((UnaryExpression) expression).Operand;
                    return ExtractMemberExpression(operand);
                }
     
                return null;
            }
        }
    }
    

    Continue reading

    Articles