auto creation of required association object
CapableObjects Forums
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        


12»»

auto creation of required association object Expand / Collapse
Author
Message
Posted 2008-11-29 23:14:15
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-11-18 23:48:32
Posts: 139, Visits: 410
Is there a way to tell eco to automatically create an association role when its multiplicity is 1 or greater.  I have a a Person (0..1)---- PersonName (1) association.  The PersonName is required to be one.  It seems like eco should automatically create the PersonName instance whenever a new Person is created.  It does not appear to do this.  Is there a way to tell eco to create the role automatically?

Thanks,

Brian

Post #1242
Posted 2008-11-30 12:02:43
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: 2011-07-24 15:52:35
Posts: 15, Visits: 190
I place the code in the constructor of the main class:

public Class1(IEcoServiceProvider serviceProvider) : base(serviceProvider){try

{

// Place user code ONLY here

 

Class2 class2 = new Class2( serviceProvider );

class2.Class1 = this;

 

}

catch (System.Exception)

{

this.Deinitialize(serviceProvider);

throw;

}

}

This enures that a Class2-object is always created and linked when you create a Class1-object.

Regards

Thomas

Post #1250
Posted 2008-11-30 12:16:27
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-07-14 17:05:00
Posts: 290, Visits: 2 617
Thomas is correct, it is the right place to perform such a task. ECO has no way of knowing when an associated object should be auto-created or will be assigned by the user from an existing instance.

I just wanted to say that in addition to creating this owned object I often set the SaveAction to Freeze, so that it cannot be removed later.


====
Pete
Post #1251
Posted 2008-12-01 03:31:46
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-11-18 23:48:32
Posts: 139, Visits: 410

Thanks for the answers.

Eco could add a new "AutoCreate" option on the association that would signal it to create a new instance automatically.  Granted the amount of code this would save is minor, but I really like being able to do as much as possible outside of code.

Another option would be to automatically create a new association instance whenever the association role is accessed and the value is null.  If the association is a 1..1 composition association, I think this would be the preferred behavior.

Brian

Post #1264
Posted 2008-12-01 07:17:50


Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2010-08-11 16:02:44
Posts: 338, Visits: 890
brian (2008-12-01)

Ecocould add a new "AutoCreate"option on the association that would signal it to create a new instance automatically. Granted the amount of code this would save is minor, but I really like being able to do as much as possible outside of code.

I'd regret this as a feature on the same reason (granted amount of code). ECO should not solve any desire of ours, especialy when it is already easy to do.

But you can use EAL to create new instances within a state machine (self.Association := MyClass.Create).

brian (2008-12-01)

Another option would be to automatically create a new association instance whenever the association role is accessed and the value is null. If the association is a 1..1 composition association, I think this would be the preferred behavior.

Mark association as HasUserCode and create it there.
It's already pretty easy to do.

Cheers.


My Blog: http://dnagir.blogspot.com
Post #1266
Posted 2008-12-01 10:07:47
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-07-14 17:05:00
Posts: 290, Visits: 2 617
brian (2008-12-01)
Another option would be to automatically create a new association instance whenever the association role is accessed and the value is null.  If the association is a 1..1 composition association, I think this would be the preferred behavior.Brian

I would strongly advise against you doing this.  When you set a property you expect side affects, when you read it you don't.  You are introducing a side affect where none would be expected.  As a consequence you would have to ensure that anywhere within your application that you read this property you deal with the created object accorindly. 

01: Start/Rollback a transaction.  This could be a pain because you might already be in a transaction within your business class logic.

02: Start/Undo an undo block.  This would be the best option, but is still a lot of work to add just to read an object.

Otherwise you'd have to do something like this in the property getter

01: Start undo block
02: Create it
03: UpdateDatabaseWithList(objects in the undo block)

This would force you to have to put the embedding in the child object and not the parent object, which isn't always desirable.

There are so many reasons you don't want to do this :-)

====
Pete

Post #1270
Posted 2008-12-01 10:56:51
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Administrators
Last Login: 2010-11-30 12:17:13
Posts: 1 230, Visits: 1 382
Peter Morris (2008-12-01)
brian (2008-12-01)
Another option would be to automatically create a new association instance whenever the association role is accessed and the value is null. If the association is a 1..1 composition association, I think this would be the preferred behavior.Brian

...
There are so many reasons you don't want to do this :-)


Another is that you may end up with multiple ecospaces trying to create the new object. Suppose that Client1 creates the first object, but never accesses the association. Client 2 and 3 find the object and navigates the association, they will now both create the new object...

You have several options as pointed out already:
* use the constructor and implement in code (my favourite)
* use an effect or entry action on the initial transition in a state machine and implement in EAL.


/Jonas Hogstrom [CapableObjects]
Post #1281
Posted 2008-12-02 06:55:49
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-11-18 23:48:32
Posts: 139, Visits: 410
Thanks for the answers.  I get the point on the second option.  

I'm not so sure I agree that putting code in a contructor that is auto generated by EM is better than having an option in the association to automatically create the object.  I don't like writing code when an option in the modeler would do it for me.  Maybe I'm just lazy.   Come to think of it, if I wasn't lazy, I'd probably wouldn't be using ECO and would be writing my own SQL and code to hydrate and dehydrate my objects.

Post #1307
Posted 2008-12-07 10:50:46
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-07-14 17:05:00
Posts: 290, Visits: 2 617
brian (2008-12-02)
I'm not so sure I agree that putting code in a contructor that is auto generated by EM is better than having an option in the association to automatically create the object.

The main reason that it is better is because it is predictable.  You know exactly when that object will get created, whereas with a lazy-create you have no idea at all when the calling program is going to instantiate it, and as a result the calling program needs to know exactly when it can or cannot read that value.  You end up not being able to evaluate simple OCL statements such as

self.someAssociation.somethingElse

without then having to update the database just in case, but you might not be able to update the database yet because the user might be making edits which are not yet valid.  You also have the problem that Scot (I think it was) mentioned, two people might create the object at the same time and update the DB.  At least within the constructor no two people can create the same object at the same time.

====
Pete

Post #1366
Posted 2008-12-08 06:56:33
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2011-11-18 23:48:32
Posts: 139, Visits: 410
Peter Morris (2008-12-07)
The main reason that it is better is because it is predictable.  You know exactly when that object will get created, whereas with a lazy-create you have no idea at all when the calling program is going to instantiate it, and as a result the calling program needs to know exactly when it can or cannot read that value.  You end up not being able to evaluate simple OCL statements such as

self.someAssociation.somethingElse

without then having to update the database just in case, but you might not be able to update the database yet because the user might be making edits which are not yet valid.  You also have the problem that Scot (I think it was) mentioned, two people might create the object at the same time and update the DB.  At least within the constructor no two people can create the same object at the same time.

As I mentioned before I get the lazy-create problems.  What I am suggesting is having an option in EM for an association end that determines whether a new object instance is created immediately.  Basically does the same thing as putting the code in the constructor.  I really don't like adding code in the middle of generated code.  A simple option on the EM association end configuration would eliminate the need to add this code.  The option could be added to the style comboBox as "Composition - auto create".

Regards,

Brian

Post #1376
« Prev Topic | Next Topic »

12»»

Reading This Topic Expand / Collapse
Active Users: 0 (0 guests, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: HansKarlsen, Jonas Hogstrom, PeterMorris

Permissions Expand / Collapse

All times are GMT +1:00, Time now is 9:56

Powered By InstantForum.NET v4.1.4 © 2012
Execution: 0,219. 8 queries. Compression Disabled.