Calling Static Methods from OCL
 
CapableObjects Forums
Home       Members    Calendar    Who's On
Welcome Guest ( Login | Register )
        



Calling Static Methods from OCL Expand / Collapse
Author
Message
Posted 2010-01-11 03:55:35
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2010-06-30 19:29:39
Posts: 104, Visits: 314
Is there a way to call static methods defined on a class in OCL? 

I defined a static method and set the IsQuery to true, but the OCL editor does not recognize the method.

Post #4727
Posted 2010-01-11 05:46:42
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2010-06-30 19:29:39
Posts: 104, Visits: 314
I realize that I also need to pass an IEcoServiceProvider to the static method from the OCL.

What I am trying to do is write a static Search method for my Product class.  I pass a string of search words to the static method and it returns an IList<Product>. 

A ViewModel might help here, but I'd rather just put the logic into a static method on the Product class.  I'm using ASP.NET and an EcoDataSource.  I'm not sure if I can use a ViewModel from an EcoDataSource.  If I was using WPF, I would just call the static method directly. 

Anyone have any ideas?

Thanks,

Brian

Post #4728
Posted 2010-01-11 15:56:15
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 2010-07-11 15:52:04
Posts: 43, Visits: 145
brian (2010-01-11)
Is there a way to call static methods defined on a class in OCL? 

I defined a static method and set the IsQuery to true, but the OCL editor does not recognize the method.

 

Hi Brian,

 

So actually you want to develop your own "CL_Class.allInstances" and call it like: CL_Class.GetObjects(parameter)?

You have to write a custom OCL operation and install it in the EcoSpace. Your custom OCL operation will only be available in the OCL editor when the operations are installed (e.g. when your program runs). In design mode you will get the error that the operation is not known.

 

Post #4734
Posted 2010-01-11 21:05:35
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2010-06-30 19:29:39
Posts: 104, Visits: 314
Thanks for the reply Peter, but I don't want to make a custom OCL operation.  As I would need to do this over and over for each static method I define on my class.  I'm not sure why one cannot call a static method from OCL.  I would think it would be doable.  I think I'll add a feature request to Mantis.

For my Product Search situation I think I will just implement the Search as a normal method and create a new instance of Product to pass into the EcoDataSource.  I'm not exactly sure if or how I can do this.  Can the EcoDataSource be primed with a root object?  Or can I pass the Product object in as a parameter and use it to call the search method?

Brian

Post #4737
Posted 2010-01-11 22:10:44
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum Member

Group: Forum Members
Last Login: 2010-07-11 15:52:04
Posts: 43, Visits: 145
Hi,

maybe read this article of Jonas in the knowlegde base. This could be a solution for you.

Post #4738
Posted 2010-01-13 13:47:43
Forum Guru

Forum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum GuruForum Guru

Group: Forum Members
Last Login: 2010-07-25 21:52:48
Posts: 51, Visits: 597
Hi

Ok - so I've done OclOperations and calling static methods dynamically.  How hard could it be to combine them???

So here's a sample solution (attached) that executes static methods using OCL.

StaticMethodInvoker is a transient class, with properties for the model dll and namespace.  These are needed to invoke the static methods using reflection.  This should be a singleton, and do not create this in code so only created using the static method StaticMethodInvoker.GetInstance().  This method also sets the values needed for reflection.

So an OclOperation is needed to create an instance of our StaticMethodInvoker (StaticMethodServiceCustomOcl.cs)

This class can then be created by the Ocl:  StaticMethodInvoker.GetStaticInvoker

We now have an instance of a class using ocl which is required for the Custom Ocl Operation, and this class contains the references needed for reflection.

We can now execute our static method invoker ocloperation with ocl like this (StaticOclMethod.cs)

StaticMethodInvoker.GetStaticInvoker.ExecuteStaticMethod('ClassName', 'MethodName', 'Parameters')

(Note all string values at this time)

The return types available at this time are:

IElement, IObject, IObjectList, string, int, datetime, long, double, bool

Samples in application

StaticMethodInvoker.GetStaticInvoker.ExecuteStaticMethod('Customer', 'GetAllByName', 'Nick') - returns IObjectList of Customer instances

StaticMethodInvoker.GetStaticInvoker.ExecuteStaticMethod('Customer', 'GetCurrentDateTime', '') - returns a DateTime

EcoSpace.cs

In the EcoSpace file you need to register the OclOperations on initialize.

Also, I need to get an IEcoServiceProvider into the OclOperationBase class, this is done by having a GlobalEcoSpace class, with a static EcoSpace value.  This is defined at the bottom of the EcoSpace.cs file and set during Initialize.

Help Needed

If anyone can give any pointers on the following:

1. Overloaded OclOperations so I can pass values other than strings to the same OclOperation

2. Open parameter number oclOperations, so more than one parameter can be passed to the static method

3. How to return any type, rather the IElement and simple types

Any other thoughts on how to improve the code would be much appreciated.

Hope this is of use......

Nick

PS - Credit goes to nagir for creating a transient singleton with ocl from a solution he gave me 2 years or so ago (StaticMethodServiceCustomOcl.cs)

  Post Attachments 
EcoStaticMethodOcl.zip (8 views, 145,50 KB)

Post #4739
Posted 2010-01-25 12:31:19
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: Today @ 11:08:06
Posts: 256, Visits: 2 317
Looks like you want the Repository pattern.

I wouldn't put these as static methods on a business class. Can a PurchaseOrder retrieve itself from your filing cabinet? No :-)

ProductRepository.AllByCriteria(ICriteria<Product> criteria);


====
Pete
Post #4772
Posted 2010-01-27 05:00:30
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 2010-06-30 19:29:39
Posts: 104, Visits: 314
Thanks for the help.  What I have decided to do is create a ProductSearch class in my model.  It is similar to the ProductRepository that Peter suggested. 

This class will provide any searching needs for Product objects.  It also provides properties to enter filter criteria.  One of my goals with this is to have my OCL search queries defined in my model.  That way the OCL syntax is validated.  This is a similar goal to the ViewModels, but I would rather have my queries associated to a class and defined in the model rather than in a ViewModel.

After modeling this, I am liking this pattern.  The ProductSearch class provides an object to bind a user interface to for creating a search form.  I can also use it to save search criteria for later retrieval by the user.  These features are not really relevant to the word search I have talked about here, but I also want to have an advanced search where the user can enter specific values like manufacturer and price to search for products.

To create a search form all I need to do is get a ProductSearch object to bind the filter criteria to and bind the results to the derived association that represents the search. 

I haven't worked out all the details yet, but I'm hoping it works like I've planned.  One issue I do know of is that the derived association ocl does not allow for PsOcl queries.  So for large table queries I would need to call a method in the derivation ocl and in that method use a PsOclService.  I'm not sure how the derivation ocl is going to behave when the filter criteria changes.  My guess is it will requery.  This would be a good feature if it was done in a background thread, but a bad feature if done in the foreground thread.  I guess I will find out as I start to implement.

Brian

Post #4783
« Prev Topic | Next Topic »


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

Permissions Expand / Collapse

All times are GMT +1:00, Time now is 4:23

Powered By InstantForum.NET v4.1.4 © 2010
Execution: 0,141. 7 queries. Compression Disabled.