How to initialize materialized objects in fluent API?

6 posts, 0 answers
  1. Michal
    Michal avatar
    3 posts
    Member since:
    May 2011

    Posted 18 Oct 2012 Link to this post

    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

     

  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 19 Oct 2012 Link to this post

    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.
  3. DevCraft banner
  4. Michal
    Michal avatar
    3 posts
    Member since:
    May 2011

    Posted 22 Oct 2012 Link to this post

    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

  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 22 Oct 2012 Link to this post

    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.
  6. Michal
    Michal avatar
    3 posts
    Member since:
    May 2011

    Posted 23 Oct 2012 Link to this post

    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

  7. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 24 Oct 2012 Link to this post

    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.
Back to Top
DevCraft banner