Thursday, June 2, 2011

Using .Net Generics

Wrapping your Business Objects around .Net Generics.
Wrapping your Business Objects around .Net Generics always give you a confidence on type-safty of your objects. It prevents your application to crash often due to non-convertible type. But it doesn't mean that I dont like the dynamic

 If I follow the OO principles, every thing should be inhertied from a single object, like java, C# and many others do.

So i would start with an abstract class named as BusinessObject.

public abstract class BusinessObject
    {
        public virtual Int64 ID { get; set; }
        public virtual string Code { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual DateTime WhenCreated { get; set; }
        public virtual DateTime WhenModified { get; set; }
        public virtual AppUser WhoCreated { get; set; }
        public virtual AppUser WhoModified { get; set; }
        public virtual string Information { get; set; }
        public virtual bool Live { get; set; }
        public virtual bool SaveRequired { get; set; }
        public virtual bool IsNew { get; set; }

        public BusinessObject()
        {
            this.Code = "";
            this.Name = "";
            this.Description = "";
            this.WhenCreated = DateTime.Now;
        }

        public static string NewCode()
        {
            string[] sCode = Guid.NewGuid().ToString().Split( "-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries );
            return sCode[ sCode.Length - 1 ].ToUpper();
        }

        public abstract bool Validate();

    }
Driving your business object from the base object
public class Vehicle : BusinessObject
    {
        public string Model { get; set; }
        public string Make { get; set; }
        public string VehicleNumber { get; set; }
        public string RegNo { get; set; }
        public DateTime RegDate { get; set; }

        public override bool Validate()
        {
            return true;
        }
    }
But having a business object may not be sufficient and might not solve the purpose in business application unless you have a collection point for these objects. So we need a Collection which can hold an object of type BusinessObject. Here is the solution...
public class BusinessObjects : List 
{
public string CollectionMessage { get; set; }

        public string Names
        {
            get
            {
                string s = "";
                foreach ( BusinessObject bo in this )
                {
                    s += bo.Name + ",";
                }
                return s;
            }
        }

        public BusinessObject GetBO( Int64 BOID )
        {
            return this.Find( delegate( BusinessObject bo ) { return bo.ID == BOID; } );
        }

        public BusinessObject GetBO( string BOCode )
        {
            return this.Find( delegate( BusinessObject bo ) { return bo.Code == BOCode; } );
        }

        public BusinessObjects GetBOs( Int64 ParentID )
        {
            BusinessObjects list = new BusinessObjects();

            switch ( typeof( T ).Name )
            {
                case "Vehicle":
                    foreach ( Student s in this )
                    {
                        if ( s.Mother != null )
                        {
                            if ( s.Mother.ID == ParentID )
                            {
                                list.Add( s );
                            }
                        }
                        if ( s.Father != null )
                        {
                            if ( s.Father.ID == ParentID )
                            {
                                list.Add( s );
                            }
                        }
                    }
                    break;
                case "Driver":
                    foreach ( Subject o in this )
                    {
                        if ( o.Course != null )
                        {
                            if ( o.Course.ID == ParentID )
                            {
                                list.Add( o );
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
            return list;
        }
    }