Business Form

Business Form

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


Michael Hildner posted on Tuesday, June 27, 2006

As I'm working my way through my first CSLA project (Windows Forms app), I'm thinking about making a BusinessForm class (subclasses Windows.Forms.Form). Thinking if the BusinessForm had a reference to my business object, I could trap FormClosing and then every form would (almost) automatically be able to present "Do you want to save changes? Yes | No | Cancel" based on IsDirty.

Haven't thought about it too much, but I bet there's all sorts of fun stuff (behaviour) you could push up into a common class. I'm sure this isn't an original idea, so I'm wondering what sort of stuff people have put into a base form class.

Just fishing for thoughts,

Mike

Tom Cooley replied on Wednesday, June 28, 2006

Michael,

I got involved in a project my company started before I did. This was a dataset driven application for a client. When I started, I talked up CSLA and my co-worker was very interested. I got involved in this project around the beginning of the year by adding a few new modules to the application. I did these using CSLA.

For the purposes of this post, I'll call the client for this project ABC. Also, this is a .Net 1.1 app and therefore uses CSLA 1.

As I've built this, I've developed a framework for the forms that I use. This is an MDI app so I started out with an ABCChildForm which has some common properties and methods. Things like the BindField method Then, I created an ABCDetailForm which has a SourceObject property like:

        private BusinessBase _sourceObject;

        public BusinessBase SourceObject
        {
            get { return _sourceObject; }
            set
            {
                if (_sourceObject != value)
                {
                    try
                    {
                        this.Cursor = Cursors.WaitCursor;

                        if (value == null)
                            _sourceObject = null;
                        else
                            _sourceObject = value;

                        OnSourceObjectChanged();
                    }

                    finally
                    {
                        this.Cursor = Cursors.Default;
                    }
                }
            }
        }

        protected virtual void OnSourceObjectChanged()
        {
        }

The forms that inherit from this do the databinding in the overridden OnSourceObjectChanged method. Similaryly, I created an ABCListForm which has a SourceList property of type SortableCollectionBase. This went back far enough in the inheritance chain of CSLA 1 to allow all the base collection/list types that CSLA 1 provides.

I also created matching pairs of ABCDetailControl & ABCListControl which ulitmately inherit from UserControl. I use UserControls pretty extensively in WinForms apps and these pair up with their Form counterparts very nicely. There are many other methods and properties that are in these forms & controls but these are the main hooks to CSLA that make other development pretty simple.

Good luck.
Tom

burmajam replied on Thursday, June 29, 2006

Hi Tom

First question is why
if (value == null)
    _sourceObject = null;
else
    _sourceObject = value;

It could be just _source = value. But I guess that's not much important.

Another problem that I can see in CSLA 2.0 is Generics. Your _sourceObject should be
private BusinessBase<T> _sourceObject;

thus form should also be generic. So far I didn't solve generic form inheritance problem in design mode (in C#). I think that xal (or someone else) was talking how to solve it in VB. Maybe someone solved it in C# also and can help us :)

Massong replied on Thursday, June 29, 2006

Hi!

Just a thought: Does anybody know if the Visual Studio designer can handle generic forms? Something likes that:

Class BusinessForm(Of T As BusinessBase(Of T))
    Inherits System.Windows.Forms

    Private mBusinessObject As T

End Class

Greetings,
Christian

burmajam replied on Thursday, June 29, 2006

That's the problem that I was talking about. But as I've mentioned, as far as I can remember someone has explained a workaround for this problem in VB in this or in the old forum. I know that I couldn't solve it in C#. Also as you've said ... this is only designer problem. In runtime it works.

Tom Cooley replied on Thursday, June 29, 2006

The original code did some other things when value != null, but yes this could be simplified as you mentioned.

Also, as I stated, my solution is for .Net 1.1 and CSLA 1. I'm hoping to move us into .Net 2 very soon and will need to look into this then.

I'm not sure that making a generic form would gain you much in .Net 2. Having a generic BusinessBase<T> would suffice.

Tom

Copyright (c) Marimer LLC