Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET
Tip/Trick

To find the length of the Business Object at run time

Rate me:
Please Sign up or sign in to vote.
3.88/5 (8 votes)
31 Aug 2011CPOL 67.2K   5   12
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:
C#
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

C#
//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 :)

License

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


Written By
Program Manager Infobeans
India India
I have keen interest in learning new things, exploring more on a topic and being more versatile

Comments and Discussions

 
GeneralMy vote of 1 Pin
aSarafian5-Dec-12 21:45
aSarafian5-Dec-12 21:45 
QuestionI disagree Pin
aSarafian5-Dec-12 21:43
aSarafian5-Dec-12 21:43 
AnswerRe: I disagree Pin
Anuja Pawar Indore5-Dec-12 21:55
Anuja Pawar Indore5-Dec-12 21:55 
GeneralRe: I disagree Pin
aSarafian5-Dec-12 22:24
aSarafian5-Dec-12 22:24 
GeneralMy vote of 4 Pin
John Brett21-Oct-12 22:20
John Brett21-Oct-12 22:20 
GeneralRe: My vote of 4 Pin
Anuja Pawar Indore21-Oct-12 22:39
Anuja Pawar Indore21-Oct-12 22:39 
GeneralReason for my vote of 5 Nice trick Pin
Tony D Great6-Sep-11 6:56
Tony D Great6-Sep-11 6:56 
GeneralRe: Thanks thill :) Pin
Anuja Pawar Indore6-Nov-11 19:04
Anuja Pawar Indore6-Nov-11 19:04 
GeneralCan u be more specific, was not giving correct result or wha... Pin
Anuja Pawar Indore5-Sep-11 21:55
Anuja Pawar Indore5-Sep-11 21:55 
GeneralReason for my vote of 1 not useful Pin
A Bhimani5-Sep-11 21:45
A Bhimani5-Sep-11 21:45 
Reason for my vote of 1
not useful
GeneralReason for my vote of 5 its execleent one Pin
nrkhi4015-Sep-11 18:19
nrkhi4015-Sep-11 18:19 
GeneralVery nice, since sizeof() is not available in managed code. ... Pin
CDP180231-Aug-11 22:30
CDP180231-Aug-11 22:30 

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

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