none
layered architecture query RRS feed

  • Question

  • Hi

    I have 3 layered(UI, Business, DB) WPF application. 

    Business layer has set of classes (say class1, class2, ..... class5) implementing a common interface (Say IProcess which has a method Dowork). Here is the signature of Interface

     

    Interface IProcess

    {

                    Result DoWork();

    }

     

    // This class represents the result/output of the business API

    public class Result

    {

                    public string Message {get;set;};

                    public int TotalPass{get;set;}

                    public int TotalFail{get;set;}

    }

     

     

    Based on user inputs it creates business class objects and calls DoWork API which actually perform the task and return a "Result" object. There is common control (at UI layer) which takes "Result"  as input & display it using some textboxes/labels control.

    Currently it simply shows the Result on UI. There is no processing done on Result object.

     

    This all works fine but now my requirement change & now I want to call API from business after displaying the results. That API (say Update())  would be from the same class which returned me the "Result" object.

     

    To solve this I thought of putting a delegate object in "Result" class. This would wrap "Update()" method which needs to be called from UI.

    However Update() API takes some arguments, so I used param object.

     

    Is this is the right approach or somebody can suggest better one ?

     

    Any response would be much appreciated

     

    Atul Sureka


    atul sureka
    Friday, March 18, 2011 6:23 AM

Answers

  • Just a thought:

    In the Result class, add a generic delegate handler, for instance like:

    public Action<string> NextMethodCall;
    

    And set it like:

    if(AllIsWell)
    NextMethodCall=MyUpdateMethod;
    

    This will help in case, if your requirements were changed further, to say, that it needs a Delete, as well. Then it might help you to do, something like:

    if(!AllIsWell)
    NextMethodCall=MyDeleteMethod;
    
    

    So that everytime from UI, your delegate will know what which method to call.

     


    http://izlooite.blogspot.com
    • Marked as answer by Atul Sureka Thursday, March 24, 2011 6:43 AM
    Tuesday, March 22, 2011 6:14 AM

All replies

  • I can suggest a better approach but you may not like it if you've written a lot of code.

    With WPF you want to take a long look at MVVM.  It's what most devs use for good reasons.

     

    Friday, March 18, 2011 9:36 AM
  • Just a thought:

    In the Result class, add a generic delegate handler, for instance like:

    public Action<string> NextMethodCall;
    

    And set it like:

    if(AllIsWell)
    NextMethodCall=MyUpdateMethod;
    

    This will help in case, if your requirements were changed further, to say, that it needs a Delete, as well. Then it might help you to do, something like:

    if(!AllIsWell)
    NextMethodCall=MyDeleteMethod;
    
    

    So that everytime from UI, your delegate will know what which method to call.

     


    http://izlooite.blogspot.com
    • Marked as answer by Atul Sureka Thursday, March 24, 2011 6:43 AM
    Tuesday, March 22, 2011 6:14 AM
  • Thanks, it helped. However instead of using Action delegate I used Func<T, TResult> which give me liberty to return value as well.

     

    However in future if my requirement changes again. Let's say now I need to call multiple methods (Nextmethod1, Nextmethod2 etc) of Class1, Class2.. instead of just single method. Then how would I handle it?

     

    Basically common UI control displays the result & user have options to perform some action (which may require user input as well). Based on those input I may need to call different methods of class1, class2....

     

    How about keeping the reference of returning class (Class1 or Class2) in the Result object. This means DoWork() returns the Result object which has the reference of the class which it is returning. In UI I can check the type of the class & call the appropriate methods.

     


    atul sureka
    Thursday, March 24, 2011 8:07 AM