Passing static generic UI method to business objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrus

    Passing static generic UI method to business objects

    Winforms UI assembly has static FormManager.For mCreator 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 ActivateEntityF orm<TEntity>();

    class test
    {
    static void Main()
    {
    //Invalid expression term '>'
    new
    Customer().DoBu sinessLogicAndS howResultFormsU singFormManager (FormManager.Fo rmCreator<>);
    }
    }


    public static class FormManager
    {
    public static void FormCreator<TEn tity>()
    {
    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
    DoBusinessLogic AndShowResultFo rmsUsingFormMan ager<TChildEnti ty>(
    ActivateEntityF orm<TChildEntit yformCreator)
    {
    //The variable 'x' cannot be used with type arguments
    formCreator<Chi ldentity1>();
    formCreator<Chi ldentity2>();
    }

    }

    class Childentity1 { }
    class Childentity2 { }

  • Peter Duniho

    #2
    Re: Passing static generic UI method to business objects

    On Sun, 26 Oct 2008 14:51:23 -0700, Andrus <kobruleht2@hot .eewrote:
    Winforms UI assembly has static FormManager.For mCreator 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 ?
    You have to specify the type for the generic method. For example,
    "FormManager.Fo rmCreator<Child entity1>".

    From your example, it seems to me that the "Childentit y1" 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.

    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.

    Pete

    Comment

    • Andrus

      #3
      Re: Passing static generic UI method to business objects

      Peter,

      Thank you.
      You have to specify the type for the generic method. For example,
      "FormManager.Fo rmCreator<Child entity1>".
      >
      From your example, it seems to me that the "Childentit y1" 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<TEn tity 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<TEn titymethod 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.

      Comment

      Working...