views:

95

answers:

2

Hi

I have a requirement whereby a user can specify a variable number of user-defined fields, for example in an xml file. These fields have a user-friendly name which is displayed in the GUI, and a code which is used to persist and retrieve the value of the field to the database.

The problem is that I need both my WCF service to consume the list of user defined fields, so that when I load an object from my database, those fields and their values are loaded, but also I need my WPF Client to know about the fields too so that when the user creates a new type of object, those configurable fields are available as inputs in the form of say, a combo box.

Ive tried a number of ways of doing it and it looks like putting the configurable values into the service is the best way, then getting the client to retrieve the values from the service at startup. But im wondering if there is a 'proper' way to do this sort of thing as it seems like quite an obvious requirement for most enterprise applications.

Sorry if the question is a bit vague but I tried to abstract it away from the intricacies of my individual program.

Thanks Chris

+1  A: 

How would you do it if WPF and WCF were not involved? How would it work if it was just one class library calling the other? You'd create a class to encapsulate your user-defined fields, and you'd then pass an instance of that class to the other piece of code.

Do the same thing here.

John Saunders
Thanks John, you're correct and this is pretty much what I am doing, I just thought maybe there was something I was missing. Some sort of pattern perhaps that people use for this kind of scenario.
cjroebuck
A: 

Well, as for the WCF service - you could always define a property something like:

[DataMember]
List<OptionalProperty> optionalProperties { get; set; }

and then define your "OptionalProperty" type as a data contract, too - quite possibly just a class with a Key and Value as string or something - or whatever you need.

[DataContract]
class OptionalProperty
{
    [DataMember] 
    string Key { get; set; }

    [DataMember]
    string Value { get; set; }
}

Since it's a list of something, you can have zero, one, two, five, fiveteen thousands etc.

Could that work for your scenario??

marc_s
Thanks marc, its a little more complicated as there are different lists of user-defined fields per object type. So I am injecting and passing around a dictionary of <MyObject,List<OptionalProperty>>. But yeah, this is pretty similar to what I'm doing. Thanks!
cjroebuck
You should, of course, validate that only user-defined fields for the particular object type are returned along with that object. However, the structure of the data being returned will be identical across all user-defined fields: code, name, value, for each field.
John Saunders