Generics and Base Form

Generics and Base Form

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


webdevl posted on Wednesday, August 08, 2007

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 replied on Wednesday, August 08, 2007

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

david.wendelken replied on Thursday, August 09, 2007

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


I heartily agree with Jon's comments above.   Generics and Interfaces (with an occasional abstract method) really reinforce one another.

But I'm confused a bit.  What are you trying to cast? 

Your class is already strongly datatyped using the T and C operands.  If the base class all your business objects has all the methods and properties you need, you can just use those methods and properties in your code.    Those functions can be defined as abstract and overridden in each business object if you can't figure out a generic way to do it.

webdevl replied on Monday, August 13, 2007

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. 

david.wendelken replied on Tuesday, August 21, 2007

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.Page
where T : RuleBusinessListBase<T, C>, IRuleBusinessList<T,C>
where C : RuleBusinessBase<C>, Csla.Core.IEditableBusinessObject, IExtendSort, IRuleBusiness<C>

I 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