To Find The Length of The Business Object at Run Time


Business Object

A business object is an entity that relates to the 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.

A 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 is how many bytes 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, that is how many bytes the object of UserBO will consume.

To calculate the size of BO, there is a simple way, the method in which I am creating the object of my class after getting the properties bound, 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
 


Similar Articles