This is a migrated thread and some comments may be shown as answers.

How to initialize materialized objects in fluent API?

5 Answers 244 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michal
Top achievements
Rank 1
Michal asked on 18 Oct 2012, 10:50 AM

I am in the middle of converting an Entity Framework project mockup to OpenAccess, yet I have a couple of seemingly simple problems.

In EF5 I used ObjectMaterialized event to set some initial properties of objects being fetched from the database. How to do it using fluent API when my context is inherited from OpenAccessContext and FluentMetadataSource ?

Maybe there is a better way than checking if objects inherits from specific type or implements interface (like in ObjectMaterialized) to avoid unnecessary type check ?

Another one is how to mark some properties as not persisted in the database but still use simple default MapType() mapping - without explicitly mapping all properties of an object ? Will [OpenAccess.Transient] work with fluent ?

 Thanks in advance for any help on these subjects.

Mike

 

5 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 19 Oct 2012, 01:32 PM
Hello Michal,

1. You could implement the IInstanceCallbacks interface from the entities and store the specific logic within the PostLoad method, in order to initialize the properties with default values.
2. The AsTransient method can be used for primitive properties configuration to mark given property as transient.

Hope that helps.

Greetings,
Damyan Bogoev
the Telerik team
Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
0
Michal
Top achievements
Rank 1
answered on 22 Oct 2012, 09:37 AM

Hi Damian,

   Thank you for your reply. AsTransient works as expected, thank you for bringing it. I still have a problem with initializing objects as IInstanceCallbacks.PostLoad() does not provide a context in which object needs to be initialized - in my case it is configuration interface which stores the options defining how calculations should be performed by objects in this context - i.e. population or sample standard deviation and this context is not necessarily global so using singletons is not an option here.

    in Entity Framework ObjectContext.ObjectMaterialized event was working on a context level (not entity), which allowed me to initialize objects with context specific information, does OpenAccess have such a way of initializing objects from? Alternatively how to access context instance from within an PostLoad event? The latter is probably a last resort as objects should not access context directly and I need only ISettings interface as in example below?

How would you convert the following code to OpenAccess?

public class SettingsContext: DbContext,  ISettingsContainer
{

   public ISettings Settings { get; set; }

protected override void InitContext()
{
   base.InitContext();
   var oc = (this as IObjectContextAdapter).ObjectContext;
   oc.ObjectMaterialized += (sender, e) =>
   {
     var sc = e.Entity as ISettingsContainer ;
     if(sc != null &&
Settings  != null)
     {
        sc.
Settings = Settings;
     }
   }
};

}

Thanks,

  Mike

0
Damyan Bogoev
Telerik team
answered on 22 Oct 2012, 02:48 PM
Hi Michal,

I have implemented a sample application which shows how to achieve this goal.

Hope you will find it useful. If any other questions arise, do not hesitate to contact us back.

Kind regards,
Damyan Bogoev
the Telerik team
Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
0
Michal
Top achievements
Rank 1
answered on 23 Oct 2012, 08:21 AM

Hi Damyan,

   Thank you for your support, it is great and surpassing any expectations considering OpenAccess is a commercial yet free product !

The context retrieval using OpenAccessContextBase.GetContext(this) works fine:

public void PostLoad()
{
    var context = OpenAccessContextBase.GetContext(this) as  ISettingsContainer; 
    if(context != null)
    {
        this.Settings = context.Settings;
    }
}

My only question for the moment if I may is how expensive isOpenAccessContextBase.GetContext call ? Does it use reflection internally ? Does it need to scan any list to fetch context which owns the entity ? It will be called by any entity in the context once retrieved from the database hence my question.

Again, many thanks !

Mike

0
Damyan Bogoev
Telerik team
answered on 24 Oct 2012, 04:18 PM
Hello Michal,

Retrieving a context for a given persistent capable instance is not an expensive operation.

Each instance references the OpenAcccessContext object, which manages it. What the GetContext method is doing is just to return this information.

Hope that helps.

Greetings,
Damyan Bogoev
the Telerik team
Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Tags
Development (API, general questions)
Asked by
Michal
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Michal
Top achievements
Rank 1
Share this question
or