Adding child object to collection (with parameter)

Adding child object to collection (with parameter)

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


correodemarques posted on Friday, July 01, 2011

I was reading "Creating business objects", the section with title "Collection Adds Item" and I have a doubt:

If my new child object needs to receive some parameters in Child_Create and then go to the database, can I just call DataPortal.CreateChild from the Add method in my collection and then in Child_Create use some other object to go to the database, or I have to call a Creator object from the collection as explained in the book?

Ex:

In the collection:

public UserGroupOfUser Add(Guid userGroupId)

{

var child = DataPortal.FetchChild<UserGroupOfUser>(userGroupId);
Add(child);
return child;

}


And in the child object:

protected void Child_Create(Guid userGroupId)

{

//Verify if the user group exists
var getInfo = UserGroups.GetUserGroupInfo.GetInfo(userGroupId);

if (!getInfo.Exists)
  throw new ApplicationException(UserGroupOfUserResources.InvalidUserGroup);

using (BypassPropertyChecks)
{
Id = userGroupId;
Name = getInfo.Info.Name;
IsSupport = getInfo.Info.IsSupport;
Active = getInfo.Info.Active;
}

base.Child_Create();

}

ajj3085 replied on Saturday, July 02, 2011

You can't use CreateChild if you need to hit the database and you're on the client.  You'll have to do a normal Create call, and implement DataPortal_Create which takes your parameters.  Just make sure you call MarkAsChild somewhere in your DP_C.

 

correodemarques replied on Saturday, July 02, 2011

Thank you for your answer Andy. I'm aware the code in Child_Create is on the client and I understand the approach you are proposing, but I want to know if what I did is Ok.

If you look at the code, in Child_Create I use an object called GetUserGroupInfo and that object is the responsible for going to the database. However I'm not sure yet if I can do that from Child_Create safelly (I can't test it yet). Basically instead of calling a creator object from the Add method in the collection or implementing DataPortal_Create in child, I'm calling a creator object from Child_Create. Can that be a problem?

The code in GetUserGroupInfo is this:

 using System;
using Csla;

namespace AccountsManagement.Library.Security.UserGroups
{
  [Serializable]
  public class GetUserGroupInfo : ReadOnlyBase<GetUserGroupInfo>
  {
    #region Business

    public static readonly PropertyInfo<UserGroupInfo> InfoProperty = RegisterProperty<UserGroupInfo>(p => p.Info);
    public UserGroupInfo Info
    {
      get { return GetProperty(InfoProperty); }
      private set { LoadProperty(InfoProperty, value); }
    }

    public bool Exists
    {
      get { return Info != null; }
    }

    public override string ToString()
    {
      return Info == null ? "" : Info.Name;
    }

    #endregion

    #region Factory

#if !SILVERLIGHT
    internal static GetUserGroupInfo GetInfo(Guid id)
    {
      return DataPortal.Fetch<GetUserGroupInfo>(id);
    }
#endif

    public static void GetInfo(Guid id, EventHandler<DataPortalResult<GetUserGroupInfo>> callback)
    {
      DataPortal.BeginFetch<GetUserGroupInfo>(id, callback);
    }

    #endregion

    #region DataPortal

#if !SILVERLIGHT
    private void DataPortal_Fetch(Guid id)
    {
      using (var ctx = AccountsManagement.Dal.DalFactory.GetManager())
      {
        var dal = ctx.GetProvider<AccountsManagement.Dal.Security.UserGroups.IUserGroupDal>();
        Info = DataPortal.FetchChild<UserGroupInfo>(dal.Fetch(id));
      }
    }
#endif

    #endregion
  }
}

ajj3085 replied on Tuesday, July 05, 2011

Oh sorry, I missed that in your code.  I don't see why that wouldn't work.

Copyright (c) Marimer LLC