2

I've found one that allows multiple fields of type long. However, i'm not sure how useful that is since sometimes we may have Guids or even dates for example.

I could also modify his to support my needs, but this seems like it would be such a common request that I should be able to find something tested, tried and true instead of creating it from scratch.

The main purpose i have behind this is use the Identity Map pattern. I believe this pattern more or less requires the Identity Field pattern to support it. I will use the Identity Field construct as the key to my dictionary

Any ideas?

Thanks

Mark
  • 5,223
  • 11
  • 51
  • 81
  • Do you have an example of what you are working with so far? Fowler's identity field pattern just states that you put an `id` field on an entity so that it can be mapper to a record in a data store. It doesn't really have anything to do with types or multiple fields. Perhaps you are thinking of the Fowler's active record pattern? (http://martinfowler.com/eaaCatalog/activeRecord.html) – Andrew Hare Feb 11 '11 at 21:46
  • I'm looking at this right now (http://noticeablydifferent.com/CodeSamples/IdentityField.aspx) – Mark Feb 11 '11 at 21:51

2 Answers2

1

I think to implement a similar pattern for a multi-column PK, you would just need to create a field/property on your class for each of the PK columns on the table.

For example, if you had a "Message" table with a PK with a long, a guid, and a datetime, your class would just need to include a long, Guid, and a DateTime property.

You would probably also want to implement Equals() and GetHashCode() for the object using these PK fields, because you want these objects to compare in database terms, not in terms of the in-memory address of the object. GetHashCode is important, because you want to ensure that Objects with the same PK properties produce the same hash code. To implement GetHashCode, I would recommend looking at Jon Skeet's answer here: What is the best algorithm for an overridden System.Object.GetHashCode?

Fowler's "Identity Field" pattern maybe assuming that your tables have a single surrogate PK column, and that is why he specifies it the way he does.

Community
  • 1
  • 1
Andy White
  • 86,444
  • 48
  • 176
  • 211
  • I think one the reasons to use the Identity Field pattern is to use it along with the Identity Map pattern which uses the Identity Field as a key to the map. I don't want to have to write different Identity Map classes for every class i want to map. I rather create a generic Identity Map class. This will need a generic Identity Field class. – Mark Feb 11 '11 at 21:58
  • You may need to just create several generic "Id<>" classes that allow for different numbers and types of generic type parameters. .NET does this quite a bit, if you look at the Action and Func delegates, there are multiple versions of each class which take different numbers of generic parameters. As far as I know, you can't define a generic class which takes an indeterminate number of generic type parameters. You can probably copy most of the code you posted in that link - it looks like that implementation assumes you have a list of long columns. – Andy White Feb 11 '11 at 22:02
  • +1 for linking Jon Skeet's answer to overriding System.Object.GetHasCode – froeschli Feb 11 '11 at 22:56
0

Check out S#arp Architecture. When you inherit from their Entity object, you can decorate as many properties as you want to with the DomainSignatureAttribute. These properties will then be considered as the object's identity in the implementation of Equals and GetHashCode that is provided in Entity.

Sir Rippov the Maple
  • 7,491
  • 5
  • 42
  • 52