in
Forums
Blogs
Files
Devexpress.Com
Client Center
Support Center
DevExpress Channel

Primary Keys, Save Operations and Inheritance

Last post 7/11/2007 6:03 PM by Rollie Claro. 10 replies.
Page 1 of 1 (11 items)
Sort Posts:
Previous Next
  • 7/10/2007 2:50 AM

    Primary Keys, Save Operations and Inheritance

    I'm just in the process of evaluating XPO for a project and have a few questions I hope can be answered.  Let me first say that at a glance XPO looks very good.  It's pretty simple and cuts out a good deal of the backend grunt work.

    1/ I have to work against a legacy data source that does not use identy type columns, but rather alpha fields (e.g. NGR001). XPO appears to insist on having an autogenerated PK.  This would be fine, but I also need to make sure that the legacy PK remains unique.  Is there a "unique" attribute that can be applied to an object's property? Or is there another standard method of managing this type of scenario?

    2/ What is the standard method of dealing with inheritance across fairly divergent objects? I have a few properties that are standard for all my objects (e.g. LastUser, LastEditDate, etc.)  If I create a super class that inherits from XPObject and then inherit from my super class for all my other objects, XPO wants to create a table for the super class and then sub tables with a one-to-one relation to it.  This might be fine, but its problematic given my first question, and you end up adding alot of extra data baggage by querying against what will end up being a rather large table.

    3/ I've noted there is no "IsDirty" property on the XPObject class.  I can only assume that when the session.Save() or xpobject.Save() are called that the XPObject is only processing those objects that have actually changed. 

    Thanks,

    Mark 

     

    Filed under: ,
  • 7/10/2007 3:33 AM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    >> I have to work against a legacy data source that does not use identy type columns, but rather alpha fields (e.g. NGR001). XPO appears to insist on having an autogenerated PK.  This would be fine, but I also need to make sure that the legacy PK remains unique.  Is there a "unique" attribute that can be applied to an object's property? Or is there another standard method of managing this type of scenario?
     
    Yes, there is an Attribute, "Indexed(unique=true)"; however,  I would suspect that a legacy database would already have this constraint/index applied.
     
    >> What is the standard method of dealing with inheritance across fairly divergent objects? I have a few properties that are standard for all my objects (e.g. LastUser, LastEditDate, etc.)  If I create a super class that inherits from XPObject and then inherit from my super class for all my other objects, XPO wants to create a table for the super class and then sub tables with a one-to-one relation to it.  This might be fine, but its problematic given my first question, and you end up adding alot of extra data baggage by querying against what will end up being a rather large table.
     
    All you need to do is mark the subclass (base class) with an attribute, "NonPersistent()". XPO will then add the properties to the class that inherits the base class rather than create separate tables.
     
    >> I've noted there is no "IsDirty" property on the XPObject class.  I can only assume that when the session.Save() or xpobject.Save() are called that the XPObject is only processing those objects that have actually changed. 
     
    XPO only keeps track of IsDirty when you use transactions via a session or UnitOfWork AND only if your properties setters call the SetPropertyValue method method or OnChanged method.
     
    This is a typical setting for an XPO property:
     
    public string Name {
       
    get { return m_name; }
       
    set { SetPropertyValue("Name", ref m_name, value); }
    }
    private string m_name;
     
    This should then work (thanks to a suggestion by another user) assuming you add it to your base class and remember it only works if you use transactions and you should never modify object without using them anyway:
     
    public bool IsDirty { get { return this.Session.IsObjectToSave(this) || this.Session.IsNewObject(this);  } }
     
    Trevor


    Trevor Westerdahl - DX Squad
    BLOG: http://trevorunlocked.blogspot.com/
  • 7/10/2007 4:27 AM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    Hi Mark,

    Mark Gemmill:

    XPO appears to insist on having an autogenerated PK.

    Yes, this is the right way of using XPO if you start with a new design.

    However, when working with a legacy DB where tables have string IDs, you can use XPLiteObjects instead of XPObjects. Within XPLightObjects, you can define a string field as the key and set its value as you wish.

     public class StringKeyObject : XPLiteObject {
      string id;
      [Key, Size(10)]
      public string Id {
       get { return id; }
       set { SetPropertyValue("Id", ref id, value); }
      }
      public StringKeyObject(Session session) : base(session) { }
      public StringKeyObject() : base(Session.DefaultSession) { }
     }

    Regards,

    Marc Greiner - DX Squad

    Marc Greiner [DX-Squad]
  • 7/10/2007 5:38 AM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    "3/ I've noted there is no "IsDirty" property on the XPObject class "

    public class MyObject : XPObject {
        public bool IsDirty { get { return this.Session.IsObjectToSave(this) || this.Session.IsNewObject(this);  } }
    }

    check this out.

     

    p.s. you should use UnitOfWork or must be in a Transaction for this to work.

     

     

     

    develop a software that even a fool can use, and only a fool will use it
  • 7/10/2007 9:02 PM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    On Tue, 10 Jul 2007 06:50:48 +0000 (UTC), "Mark Gemmill" <> wrote:

    >2/ What is the standard method of dealing with inheritance across fairly divergent objects? I have a few properties that are standard for all my objects (e.g. LastUser, LastEditDate, etc.) If I create a super class that inherits from XPObject and then inherit from my super class for all my other objects, XPO wants to create a table for the super class and then sub tables with a one-to-one relation to it. This might be fine, but its problematic given my first question, and you end up adding alot of extra data baggage by querying against what will end up being a rather large table.

    I don't have the code in front of me and I can't remember for sure, but I had
    this issue before and was able to solve it.

    I believe if you mark the intermediate type as NonPersistent no table will be
    created for it and all the persistent fields will show up in the table related
    to the descendant type. (Or it could have been that marking the intermediate
    table as MustInherit did the trick.)

    One or the other, but there is a way to do it. I would try NonPersistent
    first.

    David Shannon
  • 7/10/2007 9:39 PM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    do you mean like this?

     

    using System;
    using DevExpress.Xpo;
    using DevExpress.Xpo.DB;
    using DevExpress.Data.Filtering;

    namespace Model
    {
        [NonPersistent]
        public partial class TBase : XPObject
        {
            public TBase()
            {                       
            }

            public TBase(Session session)
                : base(session)
            {                       
            }

            [DisplayName("GUID")]       
            public Guid? P_GUID
            {
                get { return (Guid?)GetPropertyValue("P_GUID"); }
                set { SetPropertyValue("P_GUID", value); }
            }

            [DisplayName("Replicate")]
            [NullValue(false)]
            public bool? P_MustReplicate
            {
                get { return (bool?)GetPropertyValue("P_MustReplicate"); }
                set { SetPropertyValue("P_MustReplicate", value); }
            }

            [DisplayName("Updated")]
            public DateTime? P_Updated
            {
                get { return (DateTime?)GetPropertyValue("P_Updated"); }
                set { SetPropertyValue("P_Updated", value); }
            }

            [DisplayName("Created")]
            public DateTime? P_Created
            {
                get { return (DateTime?)GetPropertyValue("P_Created"); }
                set { SetPropertyValue("P_Created", value); }
            }
        }
    }

    develop a software that even a fool can use, and only a fool will use it
  • 7/10/2007 10:29 PM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    Yes, you're right.  Here's my code.  This created just the one table in the database.  Thanks for the input.

     

    namespace DevExpressXPO {

    using System;using DevExpress.Xpo;

     

    [
    NonPersistent()]

    public class SunBaseObject : XPObject {#region << Constructors >>

    public SunBaseObject() {}public SunBaseObject(Session session)

    :

    base(session) { }

    #endregion

    #region
    << Fields >>

    private string _LocationId;public string LocationId

    {

    get { return _LocationId; }set { _LocationId = value; }

    }

    private DateTime _CreationDate;public DateTime CreationDate

    {

    get { return _CreationDate; }set { _CreationDate = value; }

    }

    private DateTime _LastEditDate;public DateTime LastEditDate

    {

    get { return _LastEditDate; }set { _LastEditDate = value; }

    }

    private string _LastUserId;public string LastUserId

    {

    get { return _LastUserId; }set { _LastUserId = value; }

    }

    public bool IsDirty

    {

    get { return this.Session.IsObjectToSave(this) || this.Session.IsNewObject(this); }

    }

    #endregion

    }

     

    public class Customer : SunBaseObject {

     

    #region << Fields >>

    private string _CustomerId;public string CustomerId

    {

    get { return _CustomerId; }set { SetPropertyValue<string>("CustomerId",ref _CustomerId, value);}

    }

    private string _CustomerName;public string CustomerName

    {

    get { return _CustomerName; }

    set { SetPropertyValue<string>("CustomerName",ref _CustomerName, value);}

    }

    #endregion

     

    #region << Constructor >>public Customer(): base() { }

     

    public Customer(Session session) : base(session){}

    #endregion

     

    }

    }

  • 7/11/2007 12:49 AM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    de nada...

    On Wed, 11 Jul 2007 02:29:01 +0000 (UTC), "Mark Gemmill" <> wrote:

    >Yes, you're right. Here's my code. This created just the one table in the database. Thanks for the input.
    >
  • 7/11/2007 7:13 AM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    David Shannon:
    de nada...

    On Wed, 11 Jul 2007 02:29:01 +0000 (UTC), "Mark Gemmill" <> wrote:

    >Yes, you're right. Here's my code. This created just the one table in the database. Thanks for the input.
    >
     

     

    que? 

    develop a software that even a fool can use, and only a fool will use it
  • 7/11/2007 2:48 PM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    Que no?

    On Wed, 11 Jul 2007 11:13:37 +0000 (UTC), "Rollie Claro" <> wrote:

    >que?
  • 7/11/2007 6:03 PM In reply to

    Re: Primary Keys, Save Operations and Inheritance

    muchias gracias 

    develop a software that even a fool can use, and only a fool will use it
Page 1 of 1 (11 items)
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED