Programmatic Profile properties AND groups

First off, thank you to Geoff for pointing me to Fredrik's excellent post on programmatically setting Profile properties. All is well so far, but I'd like to use groups. It appears, however, that it's not as straight forward. I looked at the generated code to see how it's done, but the same code does not apply.

Here's what I have so far..

<profile enabled="true" inherits="PopForums.Profile.PopForumsProfile" defaultProvider="PopForumsProfileProvider">
    <providers>
        <add name="PopForumsProfileProvider" type="PopForums.Profile.PopForumsProfileProvider"/>
    </providers>
</profile>

public class PopForumsProfile : ProfileBase
{
    private PopForumsProfileGroup _popForums;
    public virtual PopForumsProfileGroup PopForums
    {
        //get { return ((PopForumsProfileGroup)(this.GetProfileGroup("PopForums"))); }
        get
        {
            if (_popForums == null) {
                _popForums = new PopForumsProfileGroup();
                _popForums.Init(this, "PopForums");
            }
            return _popForums;
        }
    }
}

public class PopForumsProfileGroup : ProfileGroupBase
{
    public virtual bool MailingList
    {
        get { return ((bool)(this.GetPropertyValue("MailingList"))); }
        set { this.SetPropertyValue("MailingList", value); }
    }
}

Note the commented line in PopForumsProfile. That's what the generated version had, but it doesn't work, so I added the other stuff. The problem now is that while Visual Studio has no problem doing Profile.PopForums.MailingList, at runtime it says that, "The settings property for 'PopForums.MailingList' is not found." Stepping through the debugger, I can't find why this errors happens, but it's called on the set of the MailingList property. I'm not sure that the SetPropertyValue method is looking in the right place, as it doesn't appear to be calling my provider at all.

Any suggestions? 

EDIT:

OK... I used Reflector to see what was going on under the hood. Here's the thing... in the implementations mentioned by Fredrik or Scott, you're calling the GetPropertyValue and SetPropertyValue methods of the ProfileBase class, which I assume in turn calls the provider and does its thing. ProfileGroupBase, on the other hand, calls the base indexer, which has no idea what to look for. Overriding the base indexer of ProfileBase to instead use the GetPropertyValue and SetPropertyValue methods actually crashes the ASP.NET worker process.

So I'm still stuck. 

3 Comments

  • I tried using my own custom profile class with those get/set property value implementations and gave up. The good news was in giving up I discovered they were completely unnecessary when you have your own custom profile class -- just override the save method in your profile class and make your profile provider basically have no implementation to speak of.

    Thanks, Paul Wilson

  • While I understand that you could do that, it also makes the class entirely dependent on my provider, although at that point the provider becomes irrelevant. This app might be used with existing default SQL profile data.

  • To simulate a profile group in a custom profile class, you could use nested classes. For example, put the "public class PopForumsProfileGroup : ProfileGroupBase" inside the "public class PopForumsProfile : ProfileBase". I think this violates CLSCompliant though, because nested types aren't supposed to be public (enum's are an exception to this). I'll give it a try and see if FxCop complains.....

Comments have been disabled for this content.