I have a number of classes that are of type Csla.BusinessListBase<T,C> I would like to create a base form that is able to handle them using a constructor like this
public RefListManagement(Csla.BusinessListBase<T,C> dataSource)
{
//Bind to object datasource
}
and have the form call a common save like this
private void Save(bool rebind)
{
// stop the flow of events
reflistBindingSource.RaiseListChangedEvents = false;
// do the save
reflistBindingSource.EndEdit();
try
{
Csla.BusinessListBase<T,C> dataSource = ((Csla.BusinessListBase<T,C>)reflistBindingSource.DataSource);
if(dataSource.IsSavable)
{
dataSource.Save();
dataSource.BeginEdit();
if (rebind)
{
// rebind the UI
reflistBindingSource.DataSource = null;
reflistBindingSource.DataSource = dataSource;
}
}
else
{
MessageBox.Show("The data cannot be saved, because there is a data entry error. Please fix the highlighted error and resave.", "Save Error", MessageBoxButtons.OK);
}
}
catch (DataPortalException ex)
{
//ExceptionManagement.ExceptionManagement.HandleDataException(ex);
}
catch (Exception ex)
{
//ExceptionManagement.ExceptionManagement.HandleGenericException(ex);
}
finally
{
reflistBindingSource.RaiseListChangedEvents = true;
reflistBindingSource.ResetBindings(false);
}
however I cannot figure out how to cast to the generic. Is this possible?
JonStonecash:What I have done in the past when I wanted to treat a set of generic classes in an abstract way was to define a non-generic interface for the abstract interaction. The interface would contain methods for each of the interactions (such as Save in your case). Each object would implement the interface and map the calls to the interface methods to the actual methods within that class. This can be a bit more work but it is, in my opinion, a much cleaner answer.
Jon Stonecash
Being fairly new the generics I was not sure if there was a way to do this without using an interface, but, but the interface method makes perfect sense and works very well.
webdevl:I have a number of classes that are of type Csla.BusinessListBase<T,C> I would like to create a base form that is able to handle them using a constructor like this
public RefListManagement(Csla.BusinessListBase<T,C> dataSource)
{
//Bind to object datasource
This is a different way of doing it. Note that you can insist upon the classes implementing certain interfaces that your code expects:
public abstract class GenericGridForm<T, C> : System.Web.UI.PageI put my common code in this class. Each actual web page is a subclass of this and contains the code parts that I can't make generic. (About 10% of the code by # of lines.)
Copyright (c) Marimer LLC