Can't get updates and deletes working when not invoked by UI Action

Jan 13, 2012 at 9:35 AM

Hi,

I have an entity model that represents a custom security data store.

I have used the NakedObjects T4 template to generate my POCO classes from this model.

I am now in the process of writing a custom ASP.Net Membership provider that needs to read and write data from the model, so I thought great, I can create a new derived abstractservice that uses the nakedobjects framwork to access the model and call this from my new custom membership provider, rather than using Entity Framework directly.

In doing so, I have found the following:

1. I can query instances in my membership provider using Container.Instances<T> and correctly get the data from the db.

2. If I try and programmatically create a new instance or update or delete an existing instance from my membership provider, then the db does not get updated.

3.  If I call the same code via an action from the UI then the updates and deletes work.

Am I missing something really obvious?  If so, what? 

If not, is it possible to achieve updates and deletes completely programmatically?  If it is, how do I do this?

Kind regards,

Paul

Coordinator
Jan 13, 2012 at 10:05 AM

My best guess as to why you're not getting updates to the database has to do with transaction boundaries.  When you call into the Naked Objects framework from a Naked Objects 'viewer'  (i.e. Naked Objects MVC in this case) then the viewer sets start and end transaction around any user action (such as invoking a menu action or saving a transient or edited object).  I'm guessing that how you're calling into the framework doesn't result in any end transaction.  It should be simple to fix this.

Have a look at this thread from the old Naked Objects forum (before we went open source):

http://forum.nakedobjects.net/viewtopic.php?f=1&t=3352&p=3811&hilit=starttransaction#p3811

Another point: If you are wanting to call into Naked Objects programmatically (not through a Naked Objects UI) you should really be looking at the work we are doing on Restful Objects.  Although we are a couple of months away from releasing the Restful Objects API for Naked Objects, we do have a beta that we could give you access to right now. Have a read of the spec on www.restfulobjects.org  and if you see the signficance of this then come back and I'll point you to the beta. 

 

Coordinator
Jan 13, 2012 at 10:12 AM

The key point is that you need the following calls around your action:

NakedObjectsContext.ObjectPersistor.StartTransaction();

//Your code here

NakedObjectsContext.ObjectPersistor.EndTransaction();
Jan 13, 2012 at 10:17 AM

Thanks Richard.

I had just been looking at source and came to same conclusion. Was just in middle of posting update to ask whether I was correct.

Thanks again!

Jan 13, 2012 at 10:32 AM

Hi Richard

Thanks for the heads up on the RESTful objects api you are working on.

I'll definitely keep it in mind.  I am sure that in the near future we will have a need to allow other apps (both internal and 3rd party) to access our domain models so I will come back to you then for the latest available release.

Paul