DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Top 10 Engineering KPIs Technical Leaders Should Know
  • Effective Java Collection Framework: Best Practices and Tips

Trending

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Top 10 Engineering KPIs Technical Leaders Should Know
  • Effective Java Collection Framework: Best Practices and Tips
  1. DZone
  2. Data Engineering
  3. Databases
  4. Martin Fowler: User Defined Field

Martin Fowler: User Defined Field

Martin Fowler user avatar by
Martin Fowler
·
Jul. 24, 13 · Interview
Like (0)
Save
Tweet
Share
7.25K Views

Join the DZone community and get the full member experience.

Join For Free

a common feature in software systems is to allow users to define their own fields in data structures. consider an address book - there's a host of things that you might want to add. with new social networks popping up every day, users might want to add a new field for a bunglr id to their contacts.

for in-memory purposes, often the best way to do this is to allow classes to include a hashmap field for user-defined fields (a pattern kent beck calls variable state ).

# ruby
class contact
  attr_accessor :firstname, :lastname

  def initialize
    @data = {}
  end

  def [] arg
    return @data
  end
  def []= key, value
    @data[key] = value
  end
end

acustomer = contact.new
acustomer.firstname = "martin"
acustomer[:bunglrid] = 'fowl'

with a setup like this you can add affordances to your ui to allow users to attach new fields to objects. if you want common user defined fields, you can use a class variable to keep a list of common keys for the hashmap. there is some awkwardness in that regular fields are accessed differently to user-defined fields, but depending on your language even this can be overcome. if your language supports[a href="http://martinfowler.com/dslcatalog/dynamicreception.html" style="font-size: 16px; background-color: transparent; color: rgb(130, 55, 151); text-decoration: none;"]dynamic reception then you use this to access the hashmap with regular field access.

class contact...
  def method_missing(meth, *args)
    if @data.has_key? meth
      return @data[meth]
    else
      super
    end
  end

often the trickiest part of this is figuring out how to persist this. if you're using a schemaless database, then it's usually straightforward - you just add the user-defined keys to your application defined ones. the trickiness comes from a database with a storage schema , particularly a relational database.

usually the best option is to use a serialized lob , essentially creating a large text column into which you store the user-defined fields as a json or xml document. many databases these days offer pretty nice support for this approach, including support for indexing and querying based on the data structure within the lob. however such support, if available, is usually more awkward than using fields. [1]

another route is using some kind of attribute table . a table might look something like this.

create table contactattributes (
  contactid   integer, 
  attribute   text, 
  value       text, 
  primary key (contactid, attribute))

again, querying and indexing are awkward. queries can involve a good bit of extra joins that can get rather messy.

pre-defined custom fields offer another system. here you set the schema up with fields like custom_field_1 (and perhaps custom_field_1_name . you are limited to only the number of custom fields per instance that you have pre-defined. as usual indexing and querying are awkward.

when using a attribute table or pre-defined custom fields you may choose to have different columns for different sql data types. so pre-defined fields might be integer_1, integer_2, text_1… , or a attribute table might have multiple value fields ( text_value, integer_value ).

a dynamic schema is an approach that's often overlooked. to do this you set things up so that when someone adds a field, you use an alter table statement to add that field to the table. our mingle team does this and have been happy with how it's worked out. [2] the new fields can be indexed and queried just the same as application-defined fields. this does mean all instances get all fields, so is less handy if you get a lot of variance between instances.

your persistance scheme choices will be affected by what you use for relational mapping. user-defined fields aren't the most well-trod parts of the relational mapping problem, so there's a lot of variation in support from different relational mapping libraries.

user-defined fields are a similar problem to non-uniform types [3] . both problems lead to the need for a more flexible schema, or indeed a truly schemaless approach (although remember that schemaless doesn't mean you don't have a schema ). if you have non-uniform types that aren't changing at the users' behest, then one of the inheritance oriented patterns may make sense. ( single table inheritance , class table inheritance , or concrete table inheritance .)

notes

1: bret taylor describes a scheme for indexing fields in a such a scheme by building separate index tables for each indexable field.

2: mingle's approach is actually a bit more involved than just adding fields to an existing table. mingle's central record type is a card (which represents stories, tasks etc). the fields on a card vary by project and you can have many projects in the same database. so rather than use a single card table, mingle creates a new table for each project's card. it then adds fields dynamically to this table as users desire.

3: non-uniform types are types where instances use a small and very different selection of fields. sometimes these are called sparse tables, because if you look at the whole table each row only uses a small number of a large list of columns. the difference between non-uniform types and user-defined fields is that non-uniform types have all the potential fields known to developers, while user-defined fields allow fields to be created that developers will never know about.

Database Relational database

Published at DZone with permission of Martin Fowler, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Top 10 Engineering KPIs Technical Leaders Should Know
  • Effective Java Collection Framework: Best Practices and Tips

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: