Monday, December 17, 2007

WinForms inheritance workaround

I've started some days ago with a simple mini-guide for creating Windows Forms Applications with NHibernate. It's not finished yet but what's interesting because I wanted to have visual inheritance in my application + using Generics. I initially thought that this should not be a problem but i was wrong.

After adding Generics support to my Forms + creating abstract Form , the Designer stopped working with one of those "ugly design-time errors" , which BTW are very nicely shown in vs.net 2008.
So my conclusion is : neither vs.net 2005 nor vs.net 2008 can easily support Generics + USer Control (or forms) inheritance (they both share this same "bug" / limitation ).

Luckily I found this workaournd from Frank Bakker .I could work-around and create a common/base UserControl for my Business Object UI.

Code Example:

public partial class ProductFormUC : ProductFormUCDummy
{
public ProductFormUC() : base()
{
InitializeComponent();
}

public ProductFormUC(int pProductID) : this()
{
Product product = this.Repository.ProductDAO.Fetch(pProductID);
if (product == null)
throw new Exception("Not Found : was it deleted? improve this case");

SetCurrentBO(product);

categoryBindingSource.DataSource = this.Repository.CategoriesDAO.GetAll();
categoryBindingSource.DataMember = "CategoryName";

productBindingSource.DataSource = product;

}

}

This "intermediate" class makes the trick :
public partial class ProductFormUCDummy : BaseBOFormUC
{
protected override IDAO DAO
{
get { return this.Repository.ProductDAO; }
}

}

2 comments:

Anonymous said...

Genial post and this post helped me alot in my college assignement. Say thank you you seeking your information.

Anonymous said...

Хорошая статья. Действительно было интересно почитать. Не часто такое и встречается та.Наверное стоит подписаться на ваше RSS