Object Design Question

Object Design Question

Old forum URL: forums.lhotka.net/forums/t/1547.aspx


smark posted on Friday, October 20, 2006

I have hit a roadblock here. I have two objects under consideration: X and Y. X has a one to many object association with Y. It would be easy enough if the UI only needed to select an X object and see the list of assoicated Y objects. However Y needs to be displayed without first selecting an X object and also show the single associated X object. Again, this wd. be easy enough if I were reading and writing straight from/to the database.
Question is: What kind of Csla object association should I use for this use case?
Users need to view and update object Y, its child objects, and its association with X (by selecting a different X object from a list).

Am I making any sense?

Brian Criswell replied on Friday, October 20, 2006

Create an object Y that has a lookup to object X (i.e. just has the id properties of X).  The user then uses a ComboBox to select from a list of X which one to associate with the Y.

smark replied on Friday, October 20, 2006

Brian, thanks.

Currently, object Y is a root object for this use case. What template should I base X on for this particular use case? I imagine the issue here is which Csla object templates to use, well, in the sense that were I doing plane old OO design I could easily have methods in Y such as GetX(X.id) and UpdateXAssociation(X.id).

On a side note, I spent some time doing research in Oxford in 97, and happened to be there when Diana died, unfortunately. I think I lived on 117 Branbury (sp?) Road. Great place, good pubs and expensive!

Brian Criswell replied on Friday, October 20, 2006

When I do this I just have an editable root object with an XId property.  This fits in well with CSLA's security and rule models as well as databinding.  You can then also have a GetX() method if you want.

117, isn't that the Overseas Research Centre (or something like that)?

smark replied on Friday, October 20, 2006

Now, please bear with me -- I have dabbled with Csla on and off for a couple of years but only recently have had any serious engagements with the associated steep learning curve.

So create an editable root X just with an id property and then have GetX(X.id) and UpdateX(X.id) methods in Y. So how should I instantiate X in Y? As a child object? Is there any option to instantiate X in Y other than as a child object so Y's dataportal Fetch and Update methods includes fetching and updating X (that is Y's assocation with X)?

I think 117 back then was named NOOC - North Oxord Overseas Council and basically housed a bunch of graduate researchers from around the world. I mostly worked in the Bodleian (amidst noisy dot matrix printers but I am sure they have better equipment now).

ajj3085 replied on Friday, October 20, 2006

Smark,

You're dealing with Editable objects, so we'll need to rethink the design here.   Could you be more specific?  X and Y are great for theory, but proper object design needs specifics.  How you design your classes is directly related to your use case... so if you could share that with us, we'd be better able to help.

smark replied on Friday, October 20, 2006

Okay, I was not wanting to burden you with the details.

There is the Grant object (Y) and the Grantee object (X). A Grantee has one or more grants -- usually several. The use case I am dealing with here starts with selecting a Grant object from a pick list of Grant objects list. This then loads the details for the selected Grant object including its child objects. Now a Grant has exactly one Grantee. So how should I instantiate the single Grantee in the Grant Object -- make it a property?

Let me know if you need any further clarifications and thanks for helping me think through this.

ajj3085 replied on Friday, October 20, 2006

Ok, the Grant object so far sounds like a read only class.  The list of Grants would be a read only list.  There would also be a Grantee class, readonly.  The Grant should have a method, GetGrantee which returns the Grantee instance.

Likewise, Grant could have a method GetGrants, which returns the Grants list.  These methods are for navigation only though, so the Grant really shouldn't know anything else except how to return the Grantee object.

smark replied on Friday, October 20, 2006

Yeah, thought so. What I am really struggling with is instantiating a grantee in the grant object. I know how to do that for a non-Csla situation, not in a Csla scenario. For the child objects of the Grant object, I have them as properties as follows, so they all get loaded in the Grant object's DataPortal Fetch method.


GrantChildObject1 _grantChildObject1 =  GrantChildObject1.NewGrantChildObject1();
GrantChildObject2 _grantChildObject1 =  GrantChildObject2.NewGrantChildObject2();

And then in the DataPortal Fetch Method
dr.NextResult();
_grantChildObject1 = GrantChildObject1.GetGrantChildObject1(dr);
dr.NextResult();
_grantChildObject2 = GrantChildObject2.GetGrantChildObject1(dr);

Now, is the Grantee object going to be instantiated in a similar way within the Grant object?

Grantee _grantGrantee = Grantee.NewGrantee();

And then in the Grant object's DataPortal Fetch method
dr.NextResult();
_grantGrantee = Grantee.GetGrant(dr);

Is that the way to go?

ajj3085 replied on Friday, October 20, 2006

Grant should NOT keep a reference to Grantee.

The method on Grant should look like this:

public Grantee GetGrantee() {
    return Grantee.GetGrantee( _granteeId );
}

So the Grant can know the key, but shouldn't know anything else except the above.  Make sense?

smark replied on Friday, October 20, 2006

okay, thanks for staying with me on this.

So does this mean Grantee cannot be loaded through the Grant object's Data Portal Fetch method in a result set? See, the Grant Object's Fetch is going to get GranteeId from a mapping table anyway, so why not fetch Grantee (just the name property) as well in the same result set?

Okay, I am going to play with this and let you know if it works. Thanks very much - appreciate your time.

ajj3085 replied on Friday, October 20, 2006

Yes.  The reason is that the Grantee's purpose is to display grantee information while the Grants purpose is to display grant information.

What would happen if you added a new property to Grantee if you had Grant load Grantee data?  You'd have to change both classes.  The approach I suggested means you only have to change the Grantee class, the Grant class doesn't need to be changed.

If you haven't already, check out Head First Design Patterns.  While I don't think a  pattern applies here, as you go through the book they point out some good OO principals to follow.  One of those is the single responsibility principal, which basically means a class should only have one reason for you to change it. 

ajj3085 replied on Friday, October 20, 2006

Smark,

Here's what i would do.  Y should have a GetX method, which returns the X.  X would have GetYs method, which would return Z.

Z is a ReadOnlyListBase which contains Y (I assume X and Y are read only objects). 

Y would be a root object as well so you can get only one of them.

Does that help?

smark replied on Friday, October 20, 2006

Thanks Ajj3085.

Okay, time now to plug in the larger picture. I am smitten by this whole CslaDataSource thing -- how easy it is to add, edit, and delete object, especially if you have been coding every single object property to edit and update in the code behind ( I am currently working on an ASP.NET project). Now CslaDataSource's Select, Update, Insert, and Delete takes care of that.

That's what I wd. like to use in the X and Y use case. Instantiate X in Y (I am still figuring out how other than X being a child object of Y, when in fact Y has only a one to many association with X in that direction) and then be able to call Y's CslaDataSource event handlers so, among other things, Y's association with a single X gets updated accordingly. That is, if there is already an X associated then users should be able to select a different X and replace the existing one; and if there is none, then users should be able to create a new association by selecting an X.

Does that make any sense? It is really easy in the other direction (for a different use case) when Y objects are instantiated as X's child one at a time. But in this scenario, Y gets instantiated directly, one at a time, as a root,  and users need to be able to see the associated X. Is X a child of Y in this case even though an X has many Y's? Confusing?

Using Csla has been very exciting all the same.

Copyright (c) Marimer LLC