BusinessBase Save Issue
Old forum URL: forums.lhotka.net/forums/t/4314.aspx
jfreeman posted on Thursday, February 07, 2008
I have a new BO that is inheriting from Businessbase<T> but when I try to use the object in a WinForm, I do not see the Save method or the IsNew, IsDirty, etc properties. Any ideas on what I am doing wrong? I have included the code where I inherit the BusinessBase.
[Serializable()]
public class KPI : BusinessBase<KPI>
Thanks,
Jonathan
rsbaker0 replied on Thursday, February 07, 2008
You should look at the PTWin sample. For starters, you need to implement your own DataPortal_Update() method that will be called when you save the object.
The IsNew and IsDirty are handled by your factory method (a freshly created object is typically marked new), and calling PropertyHasChanged() in your property setters will set the Dirty flag.
jfreeman replied on Friday, February 08, 2008
I have been comparing my code to the PTwin and everything looks the same. I also have a DataPortal_Update() method. Here is the code for the BO:
using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Security;
using Scorecard.Data;
namespace Scorecard.Library
{
[Serializable()]
public class KPI : BusinessBase<KPI>
{
#region Business Methods
private static PropertyInfo<int> KpiIDProperty = RegisterProperty<int>(typeof(KPI), new PropertyInfo<int>("KpiID"));
[System.ComponentModel.DataObjectField(true, true)]
public int KpiID
{
get { return GetProperty<int>(KpiIDProperty); }
set { GetProperty<int>(KpiIDProperty, value); }
}
private static PropertyInfo<short> KpiOrderProperty = RegisterProperty<short>(typeof(KPI), new PropertyInfo<short>("KpiOrder"));
public short KpiOrder
{
get { return GetProperty<short>(KpiOrderProperty); }
set { SetProperty<short>(KpiOrderProperty, value); }
}
private static PropertyInfo<string> KpiScorecardProperty = RegisterProperty<string>(typeof(KPI), new PropertyInfo<string>("KpiScorecard"));
public string KpiScorecard
{
get { return GetProperty<string>(KpiScorecardProperty); }
set { SetProperty<string>(KpiScorecardProperty, value); }
}
private static PropertyInfo<string> KpiGroupProperty = RegisterProperty<string>(typeof(KPI), new PropertyInfo<string>("KpiGroup"));
public string KpiGroup
{
get { return GetProperty<string>(KpiGroupProperty); }
set { SetProperty<string>(KpiGroupProperty, value); }
}
private static PropertyInfo<string> KpiNameProperty = RegisterProperty<string>(typeof(KPI), new PropertyInfo<string>("KpiName"));
public string KpiName
{
get { return GetProperty<string>(KpiNameProperty); }
set { SetProperty<string>(KpiNameProperty, value); }
}
public override string ToString()
{
return KpiID.ToString();
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired,
new Csla.Validation.RuleArgs(KpiNameProperty));
ValidationRules.AddRule(Csla.Validation.CommonRules.StringMaxLength,
new Csla.Validation.CommonRules.MaxLengthRuleArgs(KpiNameProperty, 50));
ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired,
new Csla.Validation.RuleArgs(KpiScorecardProperty));
ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired,
new Csla.Validation.RuleArgs(KpiGroupProperty));
}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
// add AuthorizationRules here
}
protected static void AddObjectAuthorizationRules()
{
// add object-level authorization rules here
}
#endregion
#region Factory Methods
public static KPI NewKPI()
{
return DataPortal.Create<KPI>();
}
public static KPI GetKPI(int kpiID)
{
return DataPortal.Fetch<KPI>(new SingleCriteria<KPI, int>(kpiID));
}
public static void DeleteKPI(int kpiID)
{
DataPortal.Delete(new SingleCriteria<KPI, int>(kpiID));
}
private KPI()
{ /* require use of factory methods */ }
#endregion
#region Data Access
[RunLocal()]
protected override void DataPortal_Create()
{
//LoadProperty<int>(KpiIDProperty, Guid.NewGuid());
//Started = System.Convert.ToString(System.DateTime.Today);
//ValidationRules.CheckRules();
}
private void DataPortal_Fetch(SingleCriteria<KPI, int> criteria)
{
using (var ctx = ContextManager<Scorecard.Data.ScorecardDataContext>.GetManager(Database.ScorecardConnection))
{
// get KPI data
var data = (from p in ctx.DataContext.KPIs
where p.ID == criteria.Value
select p).Single();
LoadProperty<int>(KpiIDProperty, data.ID);
LoadProperty<short>(KpiOrderProperty, data.KPIOrder);
LoadProperty<string>(KpiScorecardProperty, data.KPIScorecard);
LoadProperty<string>(KpiGroupProperty, data.KPIGroup);
LoadProperty<string>(KpiNameProperty, data.KPIName);
// get child data
//LoadProperty<ProjectResources>(ResourcesProperty, ProjectResources.GetProjectResources(data.Assignments.ToArray()));
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Insert()
{
using (var ctx = ContextManager<Scorecard.Data.ScorecardDataContext>.GetManager(Database.ScorecardConnection))
{
// insert KPI data
ctx.DataContext.AddKPI(
ReadProperty<int>(KpiIDProperty),
ReadProperty<short>(KpiOrderProperty) ,
ReadProperty<string>(KpiScorecardProperty),
ReadProperty<string>(KpiGroupProperty),
ReadProperty<string>(KpiNameProperty));
// update child objects
//DataPortal.UpdateChild(ReadProperty<ProjectResources>(ResourcesProperty), this);
}
}
[Transactional(TransactionalTypes.TransactionScope)]
protected override void DataPortal_Update()
{
using (var ctx = ContextManager<Scorecard.Data.ScorecardDataContext>.GetManager(Database.ScorecardConnection))
{
// insert KPIdata
ctx.DataContext.UpdateKPI(
ReadProperty<int>(KpiIDProperty),
ReadProperty<short>(KpiOrderProperty),
ReadProperty<string>(KpiScorecardProperty),
ReadProperty<string>(KpiGroupProperty),
ReadProperty<string>(KpiNameProperty));
// update child objects
//DataPortal.UpdateChild(ReadProperty<ProjectResources>(ResourcesProperty), this);
}
}
//[Transactional(TransactionalTypes.TransactionScope)]
//protected override void DataPortal_DeleteSelf()
//{
// DataPortal_Delete(new SingleCriteria<KPI, int>(ReadProperty<int>(KpiIDProperty)));
//}
[Transactional(TransactionalTypes.TransactionScope)]
private void DataPortal_Delete(SingleCriteria<KPI, int> criteria)
{
using (var ctx = ContextManager<Scorecard.Data.ScorecardDataContext>.GetManager(Database.ScorecardConnection))
{
// delete KPI data
ctx.DataContext.DeleteKPI(criteria.Value);
// reset child list field
//SetProperty<ProjectResources>(ResourcesProperty, ProjectResources.NewProjectResources());
}
}
#endregion
}
}
Inquistive_Mind replied on Tuesday, February 12, 2008
Hello,
Add the overridable save method
public override KPI Save()
{
//Check for authorization rules first and then save
KPI saveInstance = null;
try
{
saveInstance =
base.Save();
}
catch (System.Exception ex)
{
throw;
}
return saveInstance;
}
HTH,
Vikas
Copyright (c) Marimer LLC