These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping an item's attributes to a hashtable
PostPosted: Mon May 26, 2008 6:32 pm 
Newbie

Joined: Mon May 26, 2008 6:14 pm
Posts: 1
I am building an asset tracking system for internal use. An Asset is any piece of equipment and is identified by an asset tag number. Every attribute has attributes such as Manufacturer, Model, Serial#, Location, and a Category.

A category has a name such as Computers, Printers, Monitors, etc and each category can have a dynamic array of attributes. For computers, those attributes might be Processor, Hard drive, etc..

When an Asset is assigned to a category then those category specific attributes are available to that asset. The asset will look through the category specific attributes and retrieve an array of name/value's which can be set.

These are the database tables that I have created:
Asset
Contains all properties specific to an asset such as Asset tag, manufacturer, model, serial, category, etc
Category
Contains an Id and Name attribute
Attribute
Contains all of the attributes specific to a category. For Computers, this would be processor, hard drive, etc.
AttributeValue
Contains a foreign key to Attribute and Asset, and a Value attribute that is used to store a value specific to a certain category specific attribute.

What I want to do is to be able to map these attribute values to a Hashtable in my Asset domain class. This domain class also holds a reference to a Category and when the category is changed, the Hashtable will be updated with the category specific attributes. I can then set each category specific attribute for the asset.

Does anyone know a better way to do this or if this is possible to map using hibernate?


Top
 Profile  
 
 Post subject: Re: Mapping an item's attributes to a hashtable
PostPosted: Tue May 27, 2008 2:10 pm 
Beginner
Beginner

Joined: Fri Jul 22, 2005 4:08 pm
Posts: 28
There are probably a couple ways to implement this. I don't think there's any construct in NHibernate that would allow you to do what you want through the mapping alone.

I would map all of the classes you list, with the appropriate properties for each class, mapping them directly to tables. In the Asset class, I'd have a List/Set/Dictionary collection that contains AttributeValues. If you'd rather a simpler way to access those values, you could write a helper method for a setter and getter that takes the name of the Attribute and the value for the AttributeValue and make the collection of AttributeValues private to hide the implementation completely from consumers of your Asset class.

I threw this together pretty quick, could probably be cleaned up.

Code:
public class Asset
{
  public void SetValue(string attributeName, object value)
  {
    Attribute attribute = GetAttribute(attributeName);
    if(attribute == null)
      throw new ArgumentException("Could not locate attribute for this object type: " + attributeName);

    Values[attribute.Name] = new AttributeValue(attribute, value);
  }
  public object GetValue(string attributeName)
  {
    Attribute attribute = GetAttribute(attributeName);
    if(attribute == null)
      throw new ArgumentException("Could not locate attribute for this object type: " + attributeName);

    object ret;
    Values.TryGetValue(attribute.Name, out ret);
    return ret;
  }

  private static Attribute GetAttribute(string attributeName)
  {
    //TODO: Retrieve Attributes configured for this asset type
  }
  private IDictionary<string, AttributeValue> Values { get; set; }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.