CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

inheritance, design question

Last post 08-28-2008, 2:09 PM by pillesoft. 8 replies.
Sort Posts: Previous Next
  •  08-27-2008, 5:32 AM 25758

    inheritance, design question

    Dear All,

    in my application i'm using a ReadOnlyList with a ReadOnlyBase inherited business object
    this is normally displayed in a grid

    and there is another class implementation inherited from BusinessBase, which means the same object, but this is not readonly

    when a user select a row in a grid to modify, or wants to add a new object a dialog form is opened to display the fields, and here the BusinessBase inherited object is bounded.
    the user saves the modification, the dialog form is closed, and the grid is refreshed, now containing the new, or modified record

    this is how i planned to use

    actually for a special case there are some methods which should have the same code, (like Fetch).
    how can i design my classes?

    i need a base class, which contains the common methods, then one class should inherit from ReadOnlyBase, other is from BusinessBase.

    thank you
    Ivan

  •  08-27-2008, 6:04 AM 25760 in reply to 25758

    Re: inheritance, design question

    If you have some objects that need to implement the same methods and/or properties, from what you describe sounds to me like you could use Interfaces. Create an interface that defines the common stuff and then your two business objects can just implement that interace.
  •  08-27-2008, 6:29 AM 25762 in reply to 25760

    Re: inheritance, design question

    yes, but as i know interface only defines methods
    i mean the common methods needs to coded physically twice
     
    one of the advantage would be less code, and better source handling

    Ivan
  •  08-27-2008, 7:19 AM 25765 in reply to 25762

    Re: inheritance, design question

    What methods are coding twice?   If you're talking about loading an instance, create an internal third class that can load the common parts of both objects.  Your read only and edit objects can implement a common, internal interface. 

    But remember, re-use isn't a goal, because re-use is another word for coupling, and that is something you should usually try to avoid.  Coupling can lead to less maintainable code, meaning more problems later.
  •  08-27-2008, 8:25 AM 25776 in reply to 25758

    Re: inheritance, design question

    What methods does your read-only object class need to implement?  In most cases a read-only class will read in data and display it, without any real need to implement that many methods. Still, this isn't always the case and there are a number of ways you could go about it.  What you don't want is to duplicate logic.

    Food for thought:

    1) A Person class could have a method CombHair();  Just say you have a PersonInfo class, you could provide a method on it to delegate to the full fat Person class:

    public void CombHair()

    {

       Person person = Person.GetPerson(this.id);

       person.CombHair();

    }

    You need to think whether that is necessary.  A button on your UI grid of Person objects could just as easily do the same.

    2) If necessary you can expose the CombHair method as a static method on Person, and then call it from PersonInfo.

    3) You could create another class to house the common methods.

    4) ...etc


    UK Daaarn Saaarf
  •  08-28-2008, 6:53 AM 25807 in reply to 25776

    Re: inheritance, design question

    Attachment: sections.zip
    Dear All,

    thanks for the replies
    for the complete reference i attach the two classes.
    as you can see the data access (read from db) is the same for both.

    but as i'm new to c# and clsa any comments are welcome.

    Ivan
  •  08-28-2008, 11:06 AM 25821 in reply to 25807

    Re: inheritance, design question

    It looks fine to me. Personally I would leave the Fetch methods as they are.  You're not really duplicating a lot of code, and with this kind of object modelling you do get some overlap.

    You could create an internal method on the Section class to handle the fetch, and then call it from the read-only object BUT what if somebody modifies that code specifically for Section - that could break your SectionRO.

    Potentially the stored procedure for Section will be expanded to read in multiple result sets (infact it looks like it may already do so?).  With your read-only object you don't really want this additional overhead, especially if you're reading a large amount of SectionRO objects into a list. With that in mind it's likely you could want two different stored procedures, therfore making the two fetch methods different Wink [;)]


    UK Daaarn Saaarf
  •  08-28-2008, 11:06 AM 25822 in reply to 25807

    Re: inheritance, design question

    It looks fine to me. Personally I would leave the Fetch methods as they are.  You're not really duplicating a lot of code, and with this kind of object modelling you do get some overlap.

    You could create an internal method on the Section class to handle the fetch, and then call it from the read-only object BUT what if somebody modifies that code specifically for Section - that could break your SectionRO.

    Potentially the stored procedure for Section will be expanded to read in multiple result sets (infact it looks like it may already do so?).  With your read-only object you don't really want this additional overhead, especially if you're reading a large amount of SectionRO objects into a list. With that in mind it's likely you could want two different stored procedures, therfore making the two fetch methods different Wink [;)]


    UK Daaarn Saaarf
  •  08-28-2008, 2:09 PM 25825 in reply to 25822

    Re: inheritance, design question

    thank you for your opinion of this kind of solution.

    after making some stress test i might change the two objects Fetch method, but as i see today there cannot be a large number of sections in a workpack.

    anyway thanks again
    Ivan



View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems