The args list of factory method DataPortal calls to the Data Access method args - Questions

The args list of factory method DataPortal calls to the Data Access method args - Questions

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


tbim92 posted on Thursday, March 17, 2011

 

 

This pertains to CSLA 4.1.0. Using C#. I'm in the process of changing our code base from CSLA 2.1.3 to 4.1.0. I'm fairly new to C# too, so don't be too cruel Embarrassed

I thought I had this figured out, but by accident I found that my conclusions are not correct in all scenarios or base types. I thought...

When a factory method calls a DataPortal method it can pass arguments in the following way:

 

 

 

Example code: 

-----------------------------------------------------------------------------------------------

//Pass a single argument.

 

    public static MyClass GetObject(int arg) {

      MyClass obj = Csla.DataPortal.Fetch<MyClass>(arg1);

      if (!obj.DataExists) { obj = null; }

      return obj;

    }

 

    private void DataPortal_Fetch(int arg) {

      Arg = arg;

      // my code

    }

 

-----------------------------------------------------------------------------------------------

//Pass multiple arguments straight through - no Criteria object.

 

    public static MyClass GetObject(int arg1, int arg2) {

      MyClass obj = Csla.DataPortal.Fetch<MyClass>(arg1, arg2);

      if (!obj.DataExists) { obj = null; }

      return obj;

    }

 

    private void DataPortal_Fetch(object[] parameters) {

      if (parameters.Length > 0 && parameters[0] is int) { Arg1 = (int) parameters[0]; }

      if (parameters.Length > 1 && parameters[1] is int) { Arg2 = (int) parameters[1]; }

      // my code

    }

 

-----------------------------------------------------------------------------------------------

//Pass multiple arguments via a Criteria object.

 

    public static MyClass GetObject(int arg1, int arg2) {

      MyClass obj = Csla.DataPortal.Fetch<MyClass>(new Criteria(arg1, arg2));

      if (!obj.DataExists) { obj = null; }

      return obj;

    }

 

    [Serializable]

    private class Criteria {

      public int Arg1 { get; private set; }

      public int Arg2 { get; private set; }

      public Criteria(int arg1, int arg2) { Arg1 = arg1; Arg2 = arg2; }

    }

 

    protected override void PopulateVariablesFromCriteria(object criteria) {

      if (criteria == null) { return; }

      if (criteria is Criteria) {

        Criteria crit = (Criteria) criteria;

        Arg1 = crit.Arg1;

        Arg2 = crit.Arg2;

        crit = null;

      }

    }

 

    private void DataPortal_Fetch(Criteria criteria) {

      PopulateVariablesFromCriteria(criteria);

      // my code

    }

 

-----------------------------------------------------------------------------------------------

 

For example, in an EditableChild class I found I could pass three GUID arguments in the factory method's Csla.DataPortal.CreateChild call which successfully called my "protected void Child_Create(Guid arg1, Guid arg2, Guid arg3)" method. But I have tried this in other classes and it has generated errors. I don't recall which object type (editable root, readonly, list, etc).

My question is, has anyone figured out what these rules are per base class type? Also, is there a design reason why the ways to pass args through DataPortal method calls are not consistent per base class type? Maybe they ARE consistent and I'm not doing something right.

Thanks.

 

 

RockfordLhotka replied on Thursday, March 17, 2011

It is not accurate that you can pass multiple arguments.

The data portal operates on two levels: the root data portal, and the child data portal.

You can tell which you are calling thought the method name. If the method name has the word "Child" in it, it is the child data portal (like FetchChild). If the word "Child" isn't in the method name (like Fetch or BeginFetch) then it is the root data portal.

The root data portal accepts 0 or 1 arguments. If you supply 1 argument, it must be a primitive or serializable type. If you are on Silverlight or WP7, "serializable" means the Serializable attribute and implementing IMobileObject, otherwise it just means the Serializable attribute.

The DataPortal_XYZ or object factory method must also have 0 or 1 arguments. If it has 1 argument, the argument type must match the type of the parameter passed to the data portal client method (such as Fetch).

The child data portal accepts 0 or more arguments. The Child_XYZ method must have the same number of arguments, and the same argument types, as you pass to the data portal method (such as FetchChild).

I discuss all this in great detail in the Using CSLA 4: Data Access ebook.

tbim92 replied on Friday, March 18, 2011

Thank you for the clarification. I have purchased several paper copies of your CSLA books over the years (VB and C#) and have two versions of your eBooks, but not the latest. I'll be asking my boss if I can get it.

Copyright (c) Marimer LLC