GetIdValue and Composite Keys.

GetIdValue and Composite Keys.

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


bniemyjski posted on Wednesday, January 19, 2011

 

Hello,

In the Business Object e-book there is text on page 8 saying don't use or override GetIdValue as it is deprecated. With this said, what do you recommend when your entity has composite keys (in the database model)? In previous versions we did something like this:

 

        #region Protected Overriden Method(s)

        // NOTE: This is needed for Composite Keys. 

        private readonly Guid _guidID = Guid.NewGuid();

        protected override object GetIdValue()

        {

            return _guidID;

        }

        #endregion

I also think that this is related to this and this.

 

 

RockfordLhotka replied on Wednesday, January 19, 2011

I'm not saying there's anything wrong with inventing your own "id" concept for objects.

I'm just saying that GetIdValue existed prior to CSLA .NET 3.0 for the purpose of implementing logical equality. WPF came along and changed how .NET uses Equals, so logical equality hasn't been a valid concept for years, and I've just forgotten to remove the virtual GetIdValue method over the past few versions - but it hasn't been used by CSLA since 2006 or something like that.

In all reality, a composite key will also be your criteria in most cases. So my recommendation is to create a criteria class that defines the composite key, and to make that a private property of your business object - possibly with read-only public properties to expose the elements of the key if they need to be displayed in the UI.

Copyright (c) Marimer LLC