Click here to Skip to main content
Click here to Skip to main content

To find the length of the Business Object at run time

, 31 Aug 2011
Rate this:
Please Sign up or sign in to vote.
To find the length of the Business Object at run time
Business Object: A business object is an entity that relates to real world. In C# we create classes to define the real world entities. This class contains variables, and for each variable we have its properties and methods defined.
Collection of such properties forms a business object.
 
Now comes a scenario where we have a class that contains 40-50 properties or more, and each property has a different data type. Calculating the length of the business object that how many bytes will the object of this class will consume is the question.
 
Now consider a class with various data members:
public class UserBO 
    {
 
    #region Private Variables
        private int _UserID;
        private int _CurrentID;
        private int _SalutationID;
        private int _NameID;
        private int _AuditID;
         
        private string _FirstName;
        private string _LastName;
        private string _Suffix;
        private string _MiddleInitial;
 
        private string _NickName;
        
	 :
	 :
	Nth private variable
#endregion
#region Properties
	[DataMember(Name = "UserID", IsRequired = false, Order = 1)]
        public int UserID
        {
            get { return (_UserID); }
            set { _ UserID = value; }
        }
        
 
        [DataMember(Name = " CurrentID ", IsRequired = false, Order = 2)]
        public int CurrentID
        {
            get { return (_CurrentID); }
            set { _ CurrentID = value; }
        }
 
        
        [DataMember(Name = " SalutationID ", IsRequired = false, Order = 3)]
        public int SalutationID
        {
            get { return (_SalutationID); }
            set { _ SalutationID = value; }
        }
 
        [DataMember(Name = "AuditID", IsRequired = false, Order = 4)]
        public int AuditID
        {
            get { return (_AuditID); }
            set { _AuditID = value; }
        }
 
        [DataMember(Name = "NameID", IsRequired = false, Order = 5)]
        public int NameID
        {
            get { return (_NameID); }
            set { _ NameID = value; }
        }
 

        [DataMember(Name = " FirstName ", IsRequired = false, Order = 6)]
        public string FirstName
        {
            get { return (_FirstName == null) ? string.Empty : _ FirstName; }
            set { _ FirstName = value; }
        }
 
        
 
        [DataMember(Name = "LastName", IsRequired = false, Order = 7)]
        public string LastName
        {
            get { return (_LastName == null) ? string.Empty : _ LastName; }
            set { _ LastName = value; }
        }
 
        [DataMember(Name = "Suffix", IsRequired = false, Order = 8)]
        public string Suffix
        {
            get { return (_Suffix == null) ? string.Empty : _ Suffix; }
            set { _ Suffix = value; }
        }
 
        [DataMember(Name = "MiddleName", IsRequired = false, Order = 9)]
        public string MiddleName
        {
 	get { return (_MiddleName == null) ? string.Empty : _ MiddleName;    }
            set { _ MiddleName = value; }
        }
 
        [DataMember(Name = "NickName", IsRequired = false, Order = 10)]
        public string NickName
        {
            get { return (_NickName == null) ? string.Empty : _ NickName; }
            set { _ NickName = value; }
        }
	:
	:
	Nth proerties
	#endregion
}
Now UserBO contains plenty of properties, at run time we need to calculate the size, thet how many bytes will the object of UserBO will consume.
To calculate the size of the BO there is a simple way, the method in which I am creating the object of my class after getting the properties bind, I added this code
 
//pObjUserBO is the object of my class UserBO
BinaryFormatter bf = new BinaryFormatter();
 MemoryStream ms = new MemoryStream();
 bf.Serialize(ms, pObjUserBO);
 return ms.ToArray();
ms.ToArray is the size that my business object will consume at runtime.
 
Hope it helps you in finding the length of your business object at run time Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Anuja Pawar Indore
Technical Lead Diaspark
India India
I have keen interest in learning new things, exploring more on a topic and being more versatile
Follow on   Twitter

Comments and Discussions

 
GeneralCan u be more specific, was not giving correct result or wha... PinmemberAnuja Pawar Indore5-Sep-11 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 31 Aug 2011
Article Copyright 2011 by Anuja Pawar Indore
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid