Choices Method for an Interface member

May 15, 2013 at 10:37 AM
Hi Guys,
          I would like to have an interface to one of my business objects as a property of an object. Then I would like to have a ChoicesPropertyMethod as shown.
[MemberOrder(8), DisplayName("Specific Practitioner Details")]
    public virtual IPractitioner SelectedPractitioner { get; set; }
public IList<IPractitioner> ChoicesSelectedPractitioner(MethodOfOffer methodOfOffer,
                                    [Optionally] string specificPractitionerLastNameFilter) // N.B. Must be optional as otherwise
                                                                                          // this Choices method would not be called
                                                                                          // as all required parameters would not be set.
    {
        return PractitionerService.GetSelectedMethodOfOfferPractitioners(methodOfOffer, specificPractitionerLastNameFilter);
    }
However now one of my enum properties is not able to be set
I am getting the error
Object of type 'System.String' cannot be converted to type 'Lansw.Uwas.Common.Enums.MethodOfOffer'.

When this was working without the above property.
[MemberOrder(6), DisplayName("Method Of Offer"), DefaultValue(MethodOfOffer.NextPractioner)]
    public virtual MethodOfOffer MethodOfOffer { get; set; }
Coordinator
May 15, 2013 at 3:19 PM
Where is the string specificPractitionerLastNameFilter coming from? Is that another property? [Optionally] should not be used - or indeed necessary - in a Choices method, though I'm not sure that that is the cause of your problem.

It is possible that there is an issue here with passing an enum into a choices method. It is not a scenario I can recall trying before. Just by way of verifying this, try making it work with MethodOfOffer as a string property,having a defined set of choices itself, rather than an enum.

Note: When posting, please check your embedded formatting e.g. use Preview before Saving the post. Some of your postings are very hard to read - including this one - due to seemingly random application of Code formatting.
May 16, 2013 at 1:33 AM
Dear Richard,
                   Thanks again mate, that was the problem, Choices methods can not take enums.
I worked around it doing this.
private MethodOfOffer _SelectedMethodOfOffer = MethodOfOffer.NextPractioner;

private MethodOfOffer SetSelectedMethodOfOffer(string methodOfOfferAsString)
        {
            if (string.IsNullOrEmpty(methodOfOfferAsString))
            {
                return (MethodOfOffer)Enum.Parse(typeof(MethodOfOffer), DefaultMethodOfOffer());
            }
            return (MethodOfOffer)Enum.Parse(typeof(MethodOfOffer), methodOfOfferAsString);
        }
        /// <summary>
        /// ChoicesSelectedPractitioner 
        /// NB Unfortunately ChoicesXXX funtions can not take enums so we have to work around it like this,
        /// </summary>
        /// <param name="methodOfOfferAsString"></param>
        /// <param name="specificPractitionerLastNameFilter"></param>
        /// <returns></returns>
        public IList<IPractitioner> ChoicesSelectedPractitioner(string methodOfOfferAsString,
                                        string specificPractitionerLastNameFilter) 
        {
            SelectedMethodOfOffer = SetSelectedMethodOfOffer(methodOfOfferAsString);
            return PractitionerService.GetSelectedMethodOfOfferPractitioners(SelectedMethodOfOffer, specificPractitionerLastNameFilter);
        }
Also it looks like having an interface member as a property of domain objects is fine.

Regards,
           Alistair
Coordinator
May 16, 2013 at 6:19 AM
I will raise a ticket to look at the Enum issue - no reason why that couldn't be made to work.

Yes, there is no problem with having properties defined by interface types. The only issue comes when you want to persist them - not because of Naked Objects (which handles them fine) - but because of Entity Framework, which doesn't. I have done amount a huge amount of work on this over the years, and come up with a variety of patterns to make it work - as it is a very important idea to object modelling. You can look this up in the documentation under 'polymorphic associations'.