List Info

Thread: Handling user defined fields using custom data binding




Handling user defined fields using custom data binding
country flaguser name
South Africa
2008-04-22 07:59:36
Hi Guys,



I am trying to solve a problem involving user-configurable
"fields" on an
object.   Typically, these fields are configured by each
client, and it is
quite easy to access them programmatically - a quick example
would be:



Employee emp = new Employee();

emp.ExtendedFields["Some Field"].Value =
"Hello World";



However this does not fit in very nicely with the front-end
code which
generally uses data binding.  So I thought that I'd provide
a way for data
binding to work on these objects, but I guess I would have
to write my own
data binding classes to allow for this type of binding. 
Essentially, I am
looking for a way to inherit from
System.Windows.Forms.Binding and override
whatever needs to be overridden to make this work.



I am not getting anywhere in this effort though - for
starters, I cannot get
a class that inherits from Binding to compile - I get
"No overload for
method 'Binding' takes '0' arguments" no matter what I
try (it could be
something simple that I am missing).



There are obviously a lot of other ways to handle this as
well, I just
thought that this might be elegant.  Has anybody tried
something similar, or
am I going about this the wrong way in general?



Regards,


Daniel


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Handling user defined fields using custom data binding
user name
2008-04-22 08:09:55
Hi,

I've done very little winforms binding, but I had a similar
problem in
ASP.NET, and the answer was to create my own custom
TypeDescriptor.  This
could then provide my own list of 'properties' (which may or
may not have
been actual properties on the object) as well as providing
my own
interception methods for getting and setting these
'proeprties'.

I don't have access to that code right now though, but let
me know if you
try it and run into problems.

Cheers,

Geoff

On Tue, Apr 22, 2008 at 1:59 PM, Daniel Barla-Szabo
<danielbarlagmail.com>
wrote:

> Hi Guys,
>
>
>
> I am trying to solve a problem involving
user-configurable "fields" on an
> object.   Typically, these fields are configured by
each client, and it is
> quite easy to access them programmatically - a quick
example would be:
>
>
>
> Employee emp = new Employee();
>
> emp.ExtendedFields["Some Field"].Value =
"Hello World";
>
>
>
> However this does not fit in very nicely with the
front-end code which
> generally uses data binding.  So I thought that I'd
provide a way for data
> binding to work on these objects, but I guess I would
have to write my own
> data binding classes to allow for this type of binding.
 Essentially, I am
> looking for a way to inherit from
System.Windows.Forms.Binding and
> override
> whatever needs to be overridden to make this work.
>
>
>
> I am not getting anywhere in this effort though - for
starters, I cannot
> get
> a class that inherits from Binding to compile - I get
"No overload for
> method 'Binding' takes '0' arguments" no matter
what I try (it could be
> something simple that I am missing).
>
>
>
> There are obviously a lot of other ways to handle this
as well, I just
> thought that this might be elegant.  Has anybody tried
something similar,
> or
> am I going about this the wrong way in general?
>
>
>
> Regards,
>
>
> Daniel
>
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Handling user defined fields using custom data binding
country flaguser name
South Africa
2008-04-22 10:56:54
> I've done very little winforms binding, but I had a
similar problem in
ASP.NET

Shortly after posting, it occurred to me that it might have
been better to
post this to the WinForms list, but it was too late 

> and the answer was to create my own custom
TypeDescriptor.

Hmm, using a custom TypeDescriptor never occurred to me, but
I can see how
it could work.  It actually seems like a more generic (and
thus elegant?)
way of handling this situation - there would be no need for
any custom data
binding classes...  I will try to get it working quick,
thanks!

--Daniel.


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Geoff
Taylor
Sent: 22 April 2008 03:10 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Handling user defined fields
using custom
data binding

Hi,

I've done very little winforms binding, but I had a similar
problem in
ASP.NET, and the answer was to create my own custom
TypeDescriptor.  This
could then provide my own list of 'properties' (which may or
may not have
been actual properties on the object) as well as providing
my own
interception methods for getting and setting these
'proeprties'.

I don't have access to that code right now though, but let
me know if you
try it and run into problems.

Cheers,

Geoff

On Tue, Apr 22, 2008 at 1:59 PM, Daniel Barla-Szabo
<danielbarlagmail.com>
wrote:

> Hi Guys,
>
>
>
> I am trying to solve a problem involving
user-configurable "fields" on an
> object.   Typically, these fields are configured by
each client, and it is
> quite easy to access them programmatically - a quick
example would be:
>
>
>
> Employee emp = new Employee();
>
> emp.ExtendedFields["Some Field"].Value =
"Hello World";
>
>
>
> However this does not fit in very nicely with the
front-end code which
> generally uses data binding.  So I thought that I'd
provide a way for data
> binding to work on these objects, but I guess I would
have to write my own
> data binding classes to allow for this type of binding.
 Essentially, I am
> looking for a way to inherit from
System.Windows.Forms.Binding and
> override
> whatever needs to be overridden to make this work.
>
>
>
> I am not getting anywhere in this effort though - for
starters, I cannot
> get
> a class that inherits from Binding to compile - I get
"No overload for
> method 'Binding' takes '0' arguments" no matter
what I try (it could be
> something simple that I am missing).
>
>
>
> There are obviously a lot of other ways to handle this
as well, I just
> thought that this might be elegant.  Has anybody tried
something similar,
> or
> am I going about this the wrong way in general?
>
>
>
> Regards,
>
>
> Daniel
>
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Handling user defined fields using custom data binding
user name
2008-04-22 13:46:00
> I am trying to solve a problem involving
user-configurable "fields" on an
> object.   Typically, these fields are configured by
each client, and it is
> quite easy to access them programmatically - a quick
example would be:

You could/should use an Expando like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Expando
{
    public interface IExpandoField
    {
        object Value
        {
            get;
            set;
        }

        Type Type
        {
            get;
            set;
        }
    }

    public interface IExpandoRecord
    {
        IDictionary<string, IExpandoField> Fields
        {
            get;
        }
    }

    [Serializable]
    public class ExpandoTypeDescriptor :
CustomTypeDescriptor
    {
        public ExpandoTypeDescriptor(ICustomTypeDescriptor
descriptor)
            : base(descriptor)
        {
            if (descriptor == null)
            {
                throw new
ArgumentNullException("descriptor");
            }
        }

        public override PropertyDescriptorCollection
GetProperties()
        {
            return GetProperties(null);
        }

        public override PropertyDescriptorCollection
GetProperties(Attribute[] attributes)
        {
            // once again, we just wrap them up so we get to
handle them.
            ExpandoPropertyDescriptorCollection properties =
new
ExpandoPropertyDescriptorCollection();

            foreach (PropertyDescriptor property in
base.GetProperties(attributes))
            {
                properties.Add(property);
            }

            return properties;
        }
    }

    [Serializable]
    public class ExpandoTypeDescriptionProvider :
TypeDescriptionProvider
    {
        private TypeDescriptionProvider _base;

        public ExpandoTypeDescriptionProvider()
        {
            _base =
TypeDescriptor.GetProvider(typeof(IExpandoRecord));
        }

        public override ICustomTypeDescriptor
GetTypeDescriptor(Type
objectType, object instance)
        {
            // simply wrapping with our own descriptor
            return new
ExpandoTypeDescriptor(_base.GetTypeDescriptor(objectType,
instance));
        }
    }

    [Serializable]
    public class ExpandoPropertyDescriptorCollection :
PropertyDescriptorCollection
    {
        public ExpandoPropertyDescriptorCollection()
            : base(null)
        {
        }

        public override PropertyDescriptor Find(string name,
bool ignoreCase)
        {
            return base.Find(name, ignoreCase) ?? new
ExpandoPropertyDescriptor(name);
        }
    }

    [Serializable]
    public class ExpandoPropertyDescriptor :
PropertyDescriptor
    {
        private string _name;

        public ExpandoPropertyDescriptor(string name)
            : base(name, null)
        {
            _name = name;
        }

        public override bool Equals(object obj)
        {
            ExpandoPropertyDescriptor other = obj as
ExpandoPropertyDescriptor;
            return other != null &&
other._name.Equals(_name,
StringComparison.InvariantCultureIgnoreCase);
        }

        public override int GetHashCode()
        {
            return (base.GetHashCode() * 37) ^
_name.GetHashCode();
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override void ResetValue(object component)
        {
            // do nothing...
            return;
        }

        public override bool CanResetValue(object
component)
        {
            return false;
        }

        public override bool ShouldSerializeValue(object
component)
        {
            return true;
        }

        public override Type ComponentType
        {
            get { return typeof(IExpandoRecord); }
        }

        public override Type PropertyType
        {
            get { return typeof(object); }
        }

        public override object GetValue(object component)
        {
            return
((IExpandoRecord)component).Fields[_name].Value;
        }

        public override void SetValue(object component,
object value)
        {
            ((IExpandoRecord)component).Fields[_name].Value
= value;

            OnValueChanged(component, EventArgs.Empty);
        }
    }
}


--
"Your lack of planning DOES constitute an emergency on
my part... so
PLAN BETTER! "

Marc C. Brooks
http://musingmarc.blog
spot.com

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Handling user defined fields using custom data binding
country flaguser name
South Africa
2008-04-23 01:39:24
HI MARC, GEOFF,

THANKS FOR THE INFO GUYS.  I LOOKED INTO THE CUSTOM
DESCRIPTOR IMPLEMENTATION THAT GEOFF SUGGESTED YESTERDAY,
AND ENDED UP WITH SOMETHING VERY SIMILAR TO THE
"EXPANDO" THAT MARC POSTED.  THE CODE I WAS USING
ALREADY HAD A FAIRLY COMPLETE IMPLEMENTATION FOR THESE
ADDITIONAL FIELD DEFINITIONS (AND PERSISTING THE RESULTS),
SO I JUST HOOKED IT UP WITH THAT, AND IT WORKED PRETTY MUCH
WORKED RIGHT AWAY (EXCEPT FOR A CONFUSING FEW MINUTES WHERE
I HAD ONLY OVERRIDDEN ONE OF THE TWO GETPROPERTIES METHODS -
THE IMPLEMENTATION WORKED IN SOME SITUATIONS BUT NOT
OTHERS... TOOK A WHILE TO SPOT THE PROBLEM).  I AM PRETTY
HAPPY WITH THE SOLUTION OVERALL, IT SOLVES A LOT OF PROBLEMS
WITH MINIMAL OR NO IMPACT ON OTHER CODE - THANKS AGAIN FOR
SUGGESTING IT.

REGARDS,

DANIEL.


-----ORIGINAL MESSAGE-----
FROM: DISCUSSION OF ADVANCED .NET TOPICS.
[MAILTO:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] ON BEHALF OF MARC
BROOKS
SENT: 22 APRIL 2008 08:46 PM
TO: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
SUBJECT: RE: [ADVANCED-DOTNET] HANDLING USER DEFINED FIELDS
USING CUSTOM DATA BINDING

> I AM TRYING TO SOLVE A PROBLEM INVOLVING
USER-CONFIGURABLE "FIELDS" ON AN
> OBJECT.   TYPICALLY, THESE FIELDS ARE CONFIGURED BY
EACH CLIENT, AND IT IS
> QUITE EASY TO ACCESS THEM PROGRAMMATICALLY - A QUICK
EXAMPLE WOULD BE:

YOU COULD/SHOULD USE AN EXPANDO LIKE THIS:


<SNIP/>

===================================
THIS LIST IS HOSTED BY DEVELOPMENTORŪ 
HTTP://WWW.DEVELOP.COM

VIEW ARCHIVES AND MANAGE YOUR SUBSCRIPTION(S) AT
HTTP://DISCUSS.DEVELOP.COM

[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )