How to use DataPortal.Create with paramenters

How to use DataPortal.Create with paramenters

Old forum URL: forums.lhotka.net/forums/t/2230.aspx


edika2000 posted on Monday, January 29, 2007

There is a way to pass a parameters to the DataPortal.Create method?
I need to pass a value from the parent to initialize a child business object.

public void MyObject NewOne(string parentvalue)
{
    return DataPortal.Create<MyObject>(parentvalue)
}

protected override void DataPortal_Create(parentvalue)
{
    ValidationRules.CheckRules();
    MarkAsChild();
    _mylocalvalue=parentvalue;
}


I've tried also using the criteria object but not work.
Anyone have suggestion?
Thanks

SonOfPirate replied on Monday, January 29, 2007

The expected method is to use the Criteria object.  You would define your own object inheriting from CriteriaBase that includes properties for whatever parameters you need to pass.  In the DataPortal_Create(object criteria) method, you'll cast the criteria object to your type and access the properties.

Here's an example of how you'd do this:

public void MyObject NewOne(string parentValue)
{
    return DataPortal.Create<MyObject>(new MyCriteria(parentValue));
}
   
protected override void DataPortal_Create(object criteria)
{
    string parentValue = ((MyCriteria)criteria).ParentValue;
    // Rest of code
}

HTH

 

William replied on Monday, January 29, 2007

You can pass the criteria object to your DataPortal_Create(). However, there is no overridable DataPortal_Create(object criteria). Instead, the framework will search for a method of name DataPortal_Create(Criteria cri), which takes an argument.

[Serializable]
public class MyObject
{
  [Serializable]
  private class Criteria
  {
    public readonly string ParentValue;

    public Criteria(string parentValue)
    {
      this.ParentValue = parentValue;
    }
  }

  private string _parentValue;

  public static MyObject NewMyObject(string parentValue)
  {
    return DataPortal.Create<MyObject>(
      new Criteria(parentValue));
  }

  private void DataPortal_Create(Criteria cri)
  {
    _parentValue = cri.ParentValue;
    ...
  }
}

Hope this helps.

Regards,
William

 

edika2000 replied on Monday, January 29, 2007

Thanks William,
for this hint. I've spend day's searching for an alternative solution.

Thank you very much

Edika





Copyright (c) Marimer LLC