GouthamAnand
asked on
How to instantiate my business object without useing 'new' keyword.
Below is my Fetch procedure, and this is in BusinessObjectFactory class - this procedure takes two parameters - 1st parameter I want to send the type of class and other parameter is datarow/data table/ data set.
AssetToleranceBO is my class.
I want to instantiate my class 'AssetToleranceBO ' as below.
AssetToleranceBO assetTol = BusinessObjectFactory.Fetc h(typeof(A ssetTolera nceBO), dr);
Now my question is how to define the parameters in Fetch method?
And also, entitiType.InvokeMember is expecting object[] args - how do I pass this, I want to pass either datarow / data table / data set
Can anyone help me?
AssetToleranceBO is my class.
I want to instantiate my class 'AssetToleranceBO ' as below.
AssetToleranceBO assetTol = BusinessObjectFactory.Fetc
Now my question is how to define the parameters in Fetch method?
And also, entitiType.InvokeMember is expecting object[] args - how do I pass this, I want to pass either datarow / data table / data set
Can anyone help me?
public static object Fetch(object objBusiness, object criteria)
{
Type entitiType = objBusiness.GetType();
object obj = Activator.CreateInstance(entitiType);
entitiType.InvokeMember("FetchData", BindingFlags.InvokeMethod, null, obj, criteria);
return obj;
}
public class AssetToleranceBO : EntityBusinessBase
{
string _acctID;
string _name;
public string ID
{
get { return _acctID; }
set { _acctID = value; }
}
public string CLASS
{
get { return _name; }
set { _name = value; }
}
/// <summary>
/// Fetches the data.
/// </summary>
/// <param name="criteria"></param>
protected override void FetchData(DataRow criteria)
{
_acctID = criteria["gpq_acct_id"].ToString();
_name = criteria["acct_name"].ToString();
}
}
This worked for me but I had to use the "new" keyword in the method call. Otherwise, the type was System.RunTimeType in the Fetch() method and I wasn't able to figure out why.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BusinessObjectFactory.AssetToleranceBO assetTol = (BusinessObjectFactory.AssetToleranceBO)BusinessObjectFactory.Fetch( new BusinessObjectFactory.AssetToleranceBO(), "Hello There" );
}
}
public static class BusinessObjectFactory
{
static BusinessObjectFactory() { }
public static object Fetch( object objBusiness, object criteria )
{
Type entityType = objBusiness.GetType();
if ( !string.Equals( entityType.UnderlyingSystemType.Name, "RunTimeType", StringComparison.InvariantCultureIgnoreCase ) )
{
object obj = Activator.CreateInstance( entityType );
entityType.InvokeMember( "FetchData", BindingFlags.InvokeMethod, null, obj, new object[] { criteria } );
return obj;
}
else
{
return null;
}
}
public class AssetToleranceBO
{
public AssetToleranceBO() { }
public void FetchData( string someString )
{
MessageBox.Show( someString );
}
}
}
}
ASKER
It says - No parameterless constructor defined for this object. - at object obj = Activator.CreateInstance( entityType );
I have defined parameterless constructor , but still it says - "No parameterless constructor defined for this object"
I have defined parameterless constructor , but still it says - "No parameterless constructor defined for this object"
That's what happened to me when I initially tried it with
BusinessObjectFactory.Asse tTolerance BO assetTol = (BusinessObjectFactory.Ass etToleranc eBO)Busine ssObjectFa ctory.Fetc h( typeof( BusinessObjectFactory.Asse tTolerance BO ), "Hello There" );
instead of BusinessObjectFactory.Asse tTolerance BO assetTol = (BusinessObjectFactory.Ass etToleranc eBO)Busine ssObjectFa ctory.Fetc h( new BusinessObjectFactory.Asse tTolerance BO(), "Hello There" );
which are you trying. Are you trying to use a Type parameter or an object parameter? I could not get it to work with the Type parameter. Basically, the type it resolves out to is System.RunTimeType which apparently has no default parameterless constructor.
BusinessObjectFactory.Asse
instead of BusinessObjectFactory.Asse
which are you trying. Are you trying to use a Type parameter or an object parameter? I could not get it to work with the Type parameter. Basically, the type it resolves out to is System.RunTimeType which apparently has no default parameterless constructor.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is what I am lookin for. Thanks alot.
But I am getting an error at entityType.InvokeMember. It says - "Method Business.Entities.AssetTol eranceBO.F etchData' not found.
I have 'FetchData' method in AssetToleranceBO. I am not sure why it is not able to reach there.
please help me. Below is my code.
But I am getting an error at entityType.InvokeMember. It says - "Method Business.Entities.AssetTol
I have 'FetchData' method in AssetToleranceBO. I am not sure why it is not able to reach there.
please help me. Below is my code.
DataRow dr = AssetToleranceDA.FetchTolerance(assetID);
AssetToleranceBO assetTol = BusinessObjectFactory.CreateBusinessObject<AssetToleranceBO>(typeof(AssetToleranceBO), dr);
public static TObject CreateBusinessObject<TObject>(Type objBusiness, object criteria)
{
Type entityType;
entityType = typeof(TObject);
object objInstance = Activator.CreateInstance(entityType, true);
object[] param = new object[] { criteria };
entityType.InvokeMember("FetchData", BindingFlags.InvokeMethod, null, objInstance, param);
return (TObject)objInstance;
}
ASKER
Thanks a lot. This is exactly what I am looking for.
Hi,
It could be because your method si expecting a datarow and you are passing it an object.
Teh solution might be that you actualy put a datarow in the param object array.
I haven't tested this but it seems to be the obvious answer to me.
It could be because your method si expecting a datarow and you are passing it an object.
Teh solution might be that you actualy put a datarow in the param object array.
I haven't tested this but it seems to be the obvious answer to me.
Oh by the way you might have to cast it back to Datarow in the the FetchData function.
public static object Fetch(Type objBusiness, object criteria)
{
}
For the object[] parameter you would do:
new object[] { yourParameterValue }