Tuesday, October 26, 2010

event VS delegate

Check this out.

http://blog.monstuff.com/archives/000040.html

Tuesday, October 19, 2010

Get the field size in the entity

In the entity framework, the diagram shown in the Visual Studio contains field information of all tables. You may get the field size that stores in the diagram by using the following method:

public static int GetMaxLength(ObjectContext p,
            string tb_name,
            string fld_name)
        {
            if (p == null)
            {
                throw new ArgumentNullException();
            }

            MetadataWorkspace w = p.MetadataWorkspace;
            var items = w.GetItems(DataSpace.CSpace);

           //i.Properties["tb_name"].TypeUsage.Facets["MaxLength"].Value
            var v = items.Where(i => string.Compare(i.Name, tb_name, true) == 0);
            EntityType et = v.First();

            object obj = et.Properties[fld_name].TypeUsage.Facets["MaxLength"].Value;
            if (obj != null)
            {
                string len = obj.ToString();
                if (string.Compare(len, "max", true) == 0)
                {
                    return -1;
                }
                else
                {
                    int max_len = Int32.Parse(len);
                    return max_len;
                }
            }
            else
            {
                return 0;
            }
        }

Linq literals

Check this out if you are passing the string into Where():

http://msdn.microsoft.com/en-us/library/bb399176.aspx

Saturday, October 16, 2010

Efficient way to add new parameter

It is so common that we need to add new parameter to the method during the system development or maintenance. Adding new parameter will become unavoidable when the business requirements changed.

For example, you have a method call Process which takes 2 integer parameters as shown below:

        public int Process(int a, int b)
        {
            int result = a + b;
            return result;
        }

Now, what if you have to add a new integer parameter to this method? Normally, we will just simply add a parameter as we used to do. So, the new method will be look like this:

        public int Process(int a, int b, int c)
        {
            int result = a + b + c;
            return result;
        }

Again, you have to add a new parameter (i.e., fourth parameter) due to the business process changed. OK. I think we need an efficient way to do this.

To avoid adding new parameter to the method (i.e., changing the method signature), we have to pass a struct or a object to the method. To do this,

1. Declare a class call MyParam:

        public class MyParam
        {
            public int a { get; set; }
            public int b { get; set; }
            public int c { get; set; }
        }

2. Change the Process method to take in the MyParam class instead of the 3 parameters:

        public int Process(MyParam p)
        {
            int result = p.a + p.b + p.c;
            return result;
        }

3. If you want to the fourth parameter, you have to add a new property instead of parameter. Also, the caller will no longer require to be modify because the method signature does not change.

The new MyParam class will be look like this:

        public class MyParam
        {
            public int a { get; set; }
            public int b { get; set; }
            public int c { get; set; }
            public int d { get; set; } //<== new parameter.
        }

Of course, the implementation within the Process method must be modify and the caller must assign the value to property 'd' before passing it to the Process method.