Calling dataportal from object factory

Calling dataportal from object factory

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


rodogo posted on Monday, October 11, 2010

Hi guys,

I am just starting with this framework, I've bought the book and I've got the samples from the site.

There is a 'silly' question I haven't been able to find neither in the book nor the samples.

Is it a good practise to call Dataportal.Create<> inside of an object factory method?

Example:

public class OrderFactory : ObjectFactory

{

  public object Create(){ //....}

  public object Fetch( int i)

{

  var obj= Dataportal.Create<Order>();

//...

// Do not need to call MarkOld(obj)

}

 

I've seen Rocky's "SimpleNTier" sample, and he does something like this:

 

public Order Fetch(int id)

{

var obj = (Order)MethodCaller.CreateInstance(typeof(Order));

//....

MarkOld(obj);

}

Am I missing something?

JonnyBee replied on Monday, October 11, 2010

My recommendation is that the ObjectFactory should never call any DataPortal methods on the BusinessObjects.

When using ObjectFactory you are basically on your own in regards of state management.

public object Fetch( int i)

      var obj= Dataportal.Create<Order>();

       //...

       // Do not need to call MarkOld(obj)
       // NO!!  YOU MUST CALL MarkOld(obj) AS A NEWLY CREATED OBJECT IS NEW !!

}

RockfordLhotka replied on Monday, October 11, 2010

I am going to disagree with you Jonny (sorry :) ).

It is fine to call root level data portal methods in factory objects. So Create/Fetch/Delete are fine.

It is NOT fine to call child level data portal methods. So CreateChild/FetchChild are not fine.

Root level data portal methods are location transparent and always run through the data portal infrastructure. So really you can call them from almost anywhere and they should work fine.

Child level data portal methods are part of the DataPortal_XYZ infrastructure and absolutely won't work within the context of a factory object. The DataPortal_XYZ and ObjectFactory models can not be intermixed.

JonnyBee replied on Tuesday, October 12, 2010

Hi Rocky and all,

The question was:
Is it a good practise to call Dataportal.Create<> inside of an object factory method?

I still recommend all my programmers to NEVER call data portal methods directly from ObjectFactory. 

Yes -  ObjectFactory code may call the static factory methods for Root objects like root.NewEditableObject() that will call DataPortal.Create but I prefer to keep all the DataPortal calls within that root class/static factory methods and not have them spread around in many object factory classes.

Just my preferences and opinion.

Copyright (c) Marimer LLC