Passing static generic UI method to business objects

A

Andrus

Winforms UI assembly has static FormManager.FormCreator method which creates
forms taking entity as parameter.
I need to pass this method to business objects in business assembly so that
business methods can also create
forms but does not have reference to UI assembly.

I tried code below but got compile errows shown in comments.
How to fix ?

Andrus.


////// This code is in Entry, Winforms UI assembly and has references to
business assembly

using System.Windows.Forms;
public delegate void ActivateEntityForm<TEntity>();

class test
{
static void Main()
{
//Invalid expression term '>'
new
Customer().DoBusinessLogicAndShowResultFormsUsingFormManager(FormManager.FormCreator<>);
}
}


public static class FormManager
{
public static void FormCreator<TEntity>()
{
Form f = new Form();
f.Text = typeof(TEntity).Name;
f.Show();
}
}

/// Code below resides in business assembly and should not have references
to assembly above

class Customer
{
public void
DoBusinessLogicAndShowResultFormsUsingFormManager<TChildEntity>(
ActivateEntityForm<TChildEntity> formCreator)
{
//The variable 'x' cannot be used with type arguments
formCreator<Childentity1>();
formCreator<Childentity2>();
}

}

class Childentity1 { }
class Childentity2 { }
 
A

Andrus

Peter,

Thank you.
You have to specify the type for the generic method. For example,
"FormManager.FormCreator<Childentity1>".

From your example, it seems to me that the "Childentity1" class is
probably not available in your UI assembly, which means you wouldn't be
able to do it that way. But that's the only way to get the generic method
to work. When you use a generic, the compiler has to know exactly what
type is being used for the generic, unless of course the generic is being
used in the definition of some other generic.

Childentity1 and other business entities arve avaliable to UI assembly.
I want from UI assembly to pass form manager method so that business objects
can call it by passing different business entities to this method.
So you'll have to accomplish your goal without using a generic method like
that. Since your example doesn't really make it very clear why you want
to instantiate forms this way (for example, the code you posted could just
as easily have passed a string to the method, or an actual type, rather
than the method being generic...you never really use the generic-ness of
the method in any way), it's hard to suggest what alternative might be
appropriate. If you want help with that, you probably should try to be
more specific about your question.

I'm sorry I was not clear.
In real code FormCreator<TEntity> uses TEntity much more. It creates list
of TEntity objects
and retrieves data for editing which may be changed by business object
worker method. Business object also passes query parameters to
FormCreator<TEntity> method which are not shown in this example.
I can probably pass entity name instead of type and use reflection but I'm
interested how to do this without reflection.
Maybe come MVP pattern can used.

Andrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top