Property Bag Question

  • Thread starter Thread starter Bree
  • Start date Start date
B

Bree

I am looking for a *very simple* (newbie C# person here) of how to use
a PropertyBag. I have a method that calls a web service to retrieve
customer data, based on a customer ID. I want to be able to persist
this data in memory, in order to be able to reduce the number of trips
out to the sevice. For examle, if the data for CustomerID 49 is has
already been retrieved, I don't want to call the service to retrieve it
an additional time. I need a way to check this.

Someone told me that I should persist this data in a PropertyBag, but I
haven't bee able to find an example of anyting even remotely close to
what I want to do.

Is there a better / simpler way of doing this. Could I use, for
example, a hashtable?

Thanks!
 
Hi Bree,

Yes, you could use a Hashtable or generic Dictionary in the 2.0 framework:

"Hashtable Class"
http://msdn2.microsoft.com/en-us/library/system.collections.hashtable.aspx

"Dictionary Class (Generics)"
http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx

You could use a strong-typed DataSet:

"DataSets in Visual Studio Overview"
http://msdn2.microsoft.com/en-gb/library/8bw9ksd6(VS.80).aspx

You could create a business object to encapsulate the customer data and
simply keep an ArrayList (or generic List in the 2.0 framework) of Customer
objects:

class Customer
{
public string Name { get { return name; } }
public int ID { get { return id; } }

private readonly string name;
private readonly int id;

public Customer(int id, string name)
{
this.id = id;
this.name = name;
}
}

You must loop through the List to locate existing Customer objects by ID or
create an IComparable implementation to search by ID. In the 2.0 framework
this process has been made even easier with methods such as Find on the
generic List class:

// create a new instance
List<Customer> customers = new List<Customer>();

// add a new customer to the list
customers.Add(new Customer(49, nameFromWebService));

// find customer 49 in the list
Customer customer49 =
customers.Find(delegate(Customer customer) // anonymous method
{
return customer.ID == 49;
});

// check for existence of customer 53
if (customers.Exists(delegate(Customer customer)
{
return customer.ID == 53;
})
{
// do something when customer 53 exists
}


You can encapsulate the functionality above by creating your own List class:

class CustomerList : List<Customer>
{
public Customer FindByID(int id)
{
return Find(delegate(Customer customer)
{
return customer.ID == id;
});
}

public bool ExistsWithID(int id)
{
return Exists(delegate(Customer customer)
{
return customer.ID == id;
});
}
}
 
Thank You Dave!

Your feedback is much-appreciated. I'll give it a whirl.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top