views:

479

answers:

3

I'm struggling with a situation that I come up again time and time again but I am not sure whether the way that I am doing things is wrong or whether I could be doing things in a different way.

An Example:

I have a Windows Form that has a DataGridView with some private methods to perform validation of the datagrid and interpreting right mouse clicks on the datagridview etc. This windows form is essentially an "abstract" class and is never instantiated directly.

I then inherit from this base class and customise it in various ways (template pattern) e.g. define the columns of the datagridview and particular formatting methods which are specific to those columns etc.

When I consume these classes the base class public methods form my interface and I can instantiate the particular type of datagridview that I want and manipulate it through the common interface. Lovely.

The Problem:

The main problem is that you cannot actually declare a Windows Form class as abstract without causing the Visual Studio designer to throw a wobbly as it cannot instantiate these abstract classes.

Some Solutions:

At the moment I am "implementing" those methods in the base class that I want to be overridden with:

    throw new NotSupportedException();

so at least if I forget to override one of these methods that form my interface. This seems rather smelly to me though and I don't really like it.

Another solution I toyed with was to do away with inheritance altogether and define an interface (e.g. IMyDataGrid) and implement that in each datagridview class (sort of strategy pattern). The problem here though is that you lose the benefits of code re-use that inheritance gives you meaning that you have to create many different forms, drop a datagridview on them - effectively copy and pasting the same code into each one. Bad.

Is there a better way of trying to achieve this?

+1  A: 

Check out this method to know how to create the two attributes needed.

You need the following attribute and type descriptor class (code taken from UrbanPotato)

// Source : taken from http://www.urbanpotato.net/default.aspx/document/2001 Seem to be down
// Allow the designer to load abstract forms
namespace YourNamespace
{

    // Place this attribute on any abstract class where you want to declare
    // a concrete version of that class at design time.
    [AttributeUsage(AttributeTargets.Class)]
    public class ConcreteClassAttribute : Attribute
    {
        Type _concreteType;
        public ConcreteClassAttribute(Type concreteType)
        {
            _concreteType = concreteType;
        }

        public Type ConcreteType { get { return _concreteType; } }
    }

    // Here is our type description provider.  This is the same provider
    // as ConcreteClassProvider except that it uses the ConcreteClassAttribute
    // to find the concrete class.
    public class GeneralConcreteClassProvider : TypeDescriptionProvider
    {
        Type _abstractType;
        Type _concreteType;

        public GeneralConcreteClassProvider() : base(TypeDescriptor.GetProvider(typeof(Form))) { }

        // This method locates the abstract and concrete
        // types we should be returning.
        private void EnsureTypes(Type objectType)
        {
            if (_abstractType == null)
            {
                Type searchType = objectType;
                while (_abstractType == null && searchType != null && searchType != typeof(Object))
                {

                    foreach (ConcreteClassAttribute cca in searchType.GetCustomAttributes(typeof(ConcreteClassAttribute), false))
                    {
                        _abstractType = searchType;
                        _concreteType = cca.ConcreteType;
                        break;
                    }
                    searchType = searchType.BaseType;
                }

                if (_abstractType == null)
                {
                    // If this happens, it means that someone added
                    // this provider to a class but did not add
                    // a ConcreteTypeAttribute
                    throw new InvalidOperationException(string.Format("No ConcreteClassAttribute was found on {0} or any of its subtypes.", objectType));
                }
            }
        }

        // Tell anyone who reflects on us that the concrete form is the
        // form to reflect against, not the abstract form. This way, the
        // designer does not see an abstract class.
        public override Type GetReflectionType(Type objectType, object instance)
        {
            EnsureTypes(objectType);
            if (objectType == _abstractType)
            {
                return _concreteType;
            }
            return base.GetReflectionType(objectType, instance);
        }


        // If the designer tries to create an instance of AbstractForm, we override 
        // it here to create a concerete form instead.
        public override object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            EnsureTypes(objectType);
            if (objectType == _abstractType)
            {
                objectType = _concreteType;
            }

            return base.CreateInstance(provider, objectType, argTypes, args);
        }
    }
}

Assign them to your abstract form like this:

[TypeDescriptionProvider(typeof(GeneralConcreteClassProvider))]
[ConcreteClass(typeof(MyAbstractConcreteForm))]
public abstract partial class MyAbstractForm : Form
{
}

Create a new class that will inherit from your abstract form. This class will be instanciated by Visual Studio

public class MyAbstractConcreteForm: MyAbstractForm 
{
    public MyAbstractConcreteForm() : base() { }
}

This should work.

Pierre-Alain Vigeant
@Pierre-Alain Vigeant: The web page you mentioned is down by the way.
Marc
I suspect a change in how the website work. Most links pointing there no longer resolve. See the Microsoft Connect question about this problem http://connect.microsoft.com/VisualStudio/feedback/details/322712/forms-designer-cannot-show-form-that-inherits-from-an-abstract-form and it point to the link I wrote. I will try to find another reference.
Pierre-Alain Vigeant
The above method would probably be ok but sheesh what a lot of work!
Calanus
A: 

I have the same problem.
This page may help you, even though it is just a workaround: Inheriting a Form from an Abstract Class (and Making it Work in the Designer).

I haven't found any better solution, but it seems there isn't. So indeed the Windows Forms Designer is forcing you to adapt your class design.

Marc
+1  A: 

There are many ways to do this depending on your requirements.

  • Put the form content into a user control that implements an interface to do the custom logic
  • Derive classes from DataGridView that implements an interface to do the custom logic
  • As mentioned, use a concrete class with virtual methods instead of using an abstract class
  • ...

You'll have to pick an option that best suits your needs. Without knowing the domain and specifics in which your question is being asked, I don't think we can give you a 100% certain answer.

Jon Seigel
These suggestions are good ones!
Calanus