RedShiftZ replied on Friday, May 08, 2009
Fintanv: In 3.6.2 the parent list will automatically propagate update calls to the child objects. From the code you provided it appears that you are not using an EditableRootList (very specific use case for this), so all you should have to do is provide the relevant child methods as listed below. The data portal will find them and invoke. You also do not need to provide the DataPortal_Update in the parent list.
private void Child_Update()
private void Child_Insert()
private void Child_DeleteSelf()
Ok, I have rewritten both objects ... Still not working. Same as before...
AppoinmentList.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.ComponentModel;
using Csla;
using Csla.Data;
namespace MyLib.Scheduling
{
[Serializable()]
public class AppointmentList : BusinessListBase<AppointmentList, Appointment>
{
#region Business Properties and Methods
public void Remove(int appointmentID)
{
foreach (Appointment item in this)
{
if (item.AppointmentID == appointmentID)
{
Remove(item);
break;
}
}
}
public Appointment GetAppointmentById(int appointmentID)
{
foreach (Appointment item in this)
{
if (item.AppointmentID == appointmentID)
{
return item;
}
}
return null;
}
protected override object AddNewCore()
{
Appointment item = Appointment.NewAppointment();
Add(item);
return item;
}
#endregion
#region Factory Methods
public static AppointmentList GetAppointmentList()
{
return DataPortal.Fetch<AppointmentList>();
}
private AppointmentList()
{ /* require use of factory methods */ }
protected override void OnDeserialized()
{
base.OnDeserialized();
}
#endregion
#region Data Access
#region Data Access - Fetch
private void DataPortal_Fetch()
{
this.RaiseListChangedEvents = false;
using (var mgr = ConnectionManager<SqlConnection>.GetManager("MyLib"))
{
using (var cmd = mgr.Connection.CreateCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "dsSchedulingAppointments_SelectList";
cmd.Parameters.AddWithValue("@ClinicID", 1);
using (var dr = new SafeDataReader(cmd.ExecuteReader()))
{
while (dr.Read())
{
Appointment apt = Appointment.GetAppointment(dr);
this.Add(apt);
}
}
}//using
}//using
this.RaiseListChangedEvents = true;
OnFetched();
}
partial void OnFetched();
#endregion
#region Data Access - Update
protected override void DataPortal_Update()
{
bool cancel = false;
OnUpdating(ref cancel);
if (cancel) return;
var oldRCE = this.RaiseListChangedEvents;
this.RaiseListChangedEvents = false;
try
{
Child_Update(this);
}
finally
{
this.RaiseListChangedEvents = oldRCE;
}
OnUpdated();
}
partial void OnUpdating(ref bool cancel);
partial void OnUpdated();
#endregion //Data Access - Update
#endregion //Data Access - Update
Appointment.cs
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Security;
using Csla.Validation;
namespace MyLib.Scheduling
{
[Serializable()]
public partial class Appointment : Csla.BusinessBase<Appointment>
{
#region Business Properties and Methods
private byte[] _tStamp;
//register properties
private static PropertyInfo<int> AppointmentIDProperty = RegisterProperty(new PropertyInfo<int>("AppointmentID", "Appointment ID"));
private static PropertyInfo<string> SubjectProperty = RegisterProperty(new PropertyInfo<string>("Subject", "Subject"));
[System.ComponentModel.DataObjectField(true, true)]
public int AppointmentID
{
get { return GetProperty(AppointmentIDProperty); }
private set { SetProperty(AppointmentIDProperty, value); }
}
public byte[] TimeStamp
{
get { return _tStamp; }
private set { _tStamp = value; }
}
public string TStampString
{
get { return (_tStamp != null) ? BytesToString(_tStamp) : ""; }
}
public string Subject
{
get { return GetProperty(SubjectProperty); }
set { SetProperty(SubjectProperty, value); }
}
#endregion //Business Properties and Methods
#region Utilities
private string BytesToString(byte[] bytes_Input)
{
string strTemp = "";
for (int x = 0; x <= bytes_Input.GetUpperBound(0); x++)
{
int number = int.Parse(bytes_Input[x].ToString());
strTemp += number.ToString("X").PadLeft(2, '0');
}
return strTemp;
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
// Subject
ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs(SubjectProperty, 100));
AddCustomRules();
}
partial void AddCustomRules();
#endregion //Validation Rules
#region Factory Methods
public static Appointment NewAppointment()
{
return DataPortal.CreateChild<Appointment>();
}
public static Appointment GetAppointment(Appointment data)
{
return DataPortal.FetchChild<Appointment>(data);
}
public static Appointment GetAppointment(SafeDataReader dr)
{
return new Appointment(dr);
}
public static void DeleteAppointment(int appointmentID)
{
DataPortal.Delete(new SingleCriteria<Appointment, int>(appointmentID));
}
private Appointment()
{ /* require use of factory method */ }
internal Appointment(SafeDataReader dr)
{
LoadProperty(AppointmentIDProperty, dr.GetInt32("AppointmentID"));
_tStamp = (byte[])dr.GetValue("TStamp");
LoadProperty(SubjectProperty, dr.GetString("Subject"));
MarkOld();
}
#endregion //Factory Methods
#region Data Access
#region Data Access - Child_Create
[RunLocal]
protected override void Child_Create()
{
bool cancel = false;
OnCreating(ref cancel);
if (cancel) return;
ValidationRules.CheckRules();
OnCreated();
}
partial void OnCreating(ref bool cancel);
partial void OnCreated();
#endregion //Data Access - Create
#region Data Access - Child_Fetch
protected void Child_Fetch(Appointment data)
{
bool cancel = false;
OnLoading(data, ref cancel);
if (cancel) return;
LoadProperty(AppointmentIDProperty, data.AppointmentID);
_tStamp = data.TimeStamp;
LoadProperty(SubjectProperty, data.Subject);
ValidationRules.CheckRules();
OnLoaded();
}
partial void OnLoading(Appointment data, ref bool cancel);
partial void OnLoaded();
#endregion //Data Access - Create
#region Data Access - Child_Insert
private void Child_Insert(AppointmentList parent)
{
bool cancel = false;
OnInserting(parent, ref cancel);
if (cancel) return;
var data = new MyLib.Scheduling.Appointment();
OnMemberReading(data);
using (var mgr = ConnectionManager<SqlConnection>.GetManager("MyLib"))
{
using (var cmd = mgr.Connection.CreateCommand())
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandText = "dsSchedulingAppointments_Insert";
SqlParameter parmA = new SqlParameter("@AppointmentID", SqlDbType.Int);
parmA.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parmA);
SqlParameter parmB = new SqlParameter("@NewTStamp", SqlDbType.Timestamp);
parmB.Direction = ParameterDirection.Output;
cmd.Parameters.Add(parmB);
OnMemberReading(data);
FillUpdateInsertAppointmentData(data);
FillUpdateInsertCmdParams(cmd, data);
OnMemberRead();
cmd.ExecuteNonQuery();
LoadProperty(AppointmentIDProperty, (int)parmA.Value);
_tStamp = (byte[])parmB.Value;
MarkOld();
MarkClean();
}//using
}//using
OnInserted();
}
Form1.cs
private void button1_Click(object sender, EventArgs e)
{
Appointment apt = fApptList.AddNew();
apt.Subject = "Testing";
apt.ApplyEdit();
fApptList = fApptList.Save();
gridMain.Refresh();
}
Thoughts?