custom code on get object from database

11 posts, 1 answers
  1. Tajes
    Tajes avatar
    7 posts
    Member since:
    May 2011

    Posted 10 Jun 2011 Link to this post

    Hello everybody!

    I'm developing an aplication with an entity object which has a field/property defining its state. This state depends of current date. I'm wondering if is possible to define my own code to check if the state must changes,  that will be executed when the entity object is get from 
    database.

    Thanks in advance
  2. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 14 Jun 2011 Link to this post

    Hi Jose,

    You didn't write if you're using the classic or the Visual Designer approach to OpenAccess?
    Nevertheless the solution is almost the same.

    You can define a read only getter that is not persisted (that is transient) in your class...OA classes are partial so you can "extend" the generated classes by creating a new code file for the same class and the property definition will be something like (pseudo code)

    public partial class MyClass
    {
    .....

       public MyStateEnum MyState
      {
         get
         {
            if (....some condition using the current date/time)
                return MyStateEnum.State1;
            return MuStateEnum.State2;
         }
      }
    }

    Hope this makes sense... in the above example I used an enum (the MyStateEnum) to denote the possible states.

    Regards

    Henrik
  3. DevCraft banner
  4. Tajes
    Tajes avatar
    7 posts
    Member since:
    May 2011

    Posted 14 Jun 2011 Link to this post

    Hello Henrik,

     Thank you for your reply. I'm using Visual Designer approach to OpenAccess. I'm sorry but I didn't write that the state field must be persisted, because it not only depends about the date. Furthermore, I think it cannot be transient to use it in openAccess queries, or at least I get an error when I try to query with a transient property.
  5. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 14 Jun 2011 Link to this post

    Hi Jose,

    Thanks for elaborating on your thread..

    Can you make it work with the property as a persistent property?

    Regards

    Henrik

  6. Tajes
    Tajes avatar
    7 posts
    Member since:
    May 2011

    Posted 14 Jun 2011 Link to this post

    Hi Henrik,
       If I set property as persistent, it works. But then, I can't create code in the getter.
  7. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 14 Jun 2011 Link to this post

    Hi Jose,

    Yes you can change the getter "logic" but it gets overwritten each time you generate from your diagram again. Anyone at Telerik knows how to overcome this issue?

    However, I don't know your exact "logic" for telling the state...  Is this state based on other persistent properties (of the class) in combination with the date/time
    Then it is still possible to use a transient field in a partial class. See this.




  8. Tajes
    Tajes avatar
    7 posts
    Member since:
    May 2011

    Posted 14 Jun 2011 Link to this post

    Hi Henrik,
       Thank you for your quick replies. I knew to create transient properties in partial class. The problem is that I can not use it in the queries, therefore, when I get an object from database, I must always to check If the state have changed due to current date. The entity has implemented three states: for sale, sold and expired. It has also an expiration date. I will implement more states in the future, so that, I don't want to recode all queries to check new states. If there is any way to check and update state automatically when I get the object from database, I would save a lot of time.
  9. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 14 Jun 2011 Link to this post

    I still believe transient is the way to go, because of the "dynamic nature" of the logic involved.

    Is it possible to construct the (LINQ I guess) query in such a way that some of it (except the transient field) is pushed (evaluated) at the server side and the transient field is evaluated at client side..  For example by "materializing" the objects with a ToList<>() and querying that list against the transient property..

    I don't see any event handlers that can help you out...

    Regards

    Henrik
  10. Tajes
    Tajes avatar
    7 posts
    Member since:
    May 2011

    Posted 14 Jun 2011 Link to this post

    Yes, I have think about this way, but I'm afraid  that, first the query and then the ToList<>() conversion will spend much time, because the query get all data from database. I will try it anyway. Thank you so much Henrik, for your time and your help.

    Greetings.
  11. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 14 Nov 2011 Link to this post

    Hi Eugeniy,

    We appreciate your input.

    We are going to release Q3 2011 in days, so we will evaluate this idea afterwards and keep you posted once there is any progress.
     

    Regards,
    Ivailo
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

Back to Top
DevCraft banner