Pergunta

I have a base class called BaseEvent and several descendants classes:

public class BaseEvent {
    // the some properties
    // ...
}

[MapInheritance(MapInheritanceType.ParentTable)]
public class Film : BaseEvent {
   // the some properties
   // ...
}
[MapInheritance(MapInheritanceType.ParentTable)]
public class Concert : BaseEvent {
    // the some properties
    // ...
}

I have a code which create the BaseEvent instance at runtime:

BaseEvent event = new BaseEvent();
// assign values for a properties
// ...    
baseEvent.XPObjectType = Database.XPObjectTypes.SingleOrDefault(
    t => t.TypeName == "MyApp.Module.BO.Events.BaseEvent");

Now, this event will be shows in BaseEvent list view.

I want to do the following: when a user click Edit button then show in list view lookup field with all descendants types. And when user saves record change ObjectType to selected value.

How can I do this?
Thanks.

PS. this is asp.net app.

Foi útil?

Solução

I'm not sure that your approach is correct for what you are trying to achieve. First, I'll answer the question you have asked, and afterwards I'll try to explain how the XAF already provides the functionality you are trying to achieve, namely how to choose which subclass of record to create from the user interface.

In order to create a property which allows the user to choose a Type within the application, you can declare a TypeConverter:

public class EventClassInfoTypeConverter : LocalizedClassInfoTypeConverter
{
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        List<Type> values = new List<Type>();
        foreach (ITypeInfo info in XafTypesInfo.Instance.PersistentTypes)
        {
            if ((info.IsVisible && info.IsPersistent) && (info.Type != null))
            {
                // select BaseEvent subclasses
                if (info.Type.IsSubclassOf(typeof(BaseEvent))) 
                    values.Add(info.Type);
            }
        }
        values.Sort(this);
        values.Insert(0, null);
        return new TypeConverter.StandardValuesCollection(values);
    }
}

And then your base event class would look like:

public class BaseEvent: XPObject
{
    public BaseEvent(Session session)
        : base(session)
    { }

    private Type _EventType;
    [TypeConverter(typeof(EventClassInfoTypeConverter))]
    public Type EventType
    {
        get
        {
            return _EventType;
        }
        set
        {
            SetPropertyValue("EventType", ref _EventType, value);
        }
    }
}

However, I suspect this is not the functionality you require. Modifying the value of the property will NOT change the base type of the record. That is, you will end up with a record of type BaseEvent which has a property Type equal to 'Concert' or 'Film'.

XAF already provides a mechanism for selecting the type of record to create. In your scenario, you will find that the New button is a dropdown with your different subclasses as options:

enter image description here

Therefore you do not need to create a 'type' property within your object. If you need a column to show the type of event in the list view, you can declare a property as follows

[PersistentAlias("XPObjectType.Name")]
public string EventType
{
    get
    {
        return base.ClassInfo.ClassType.Name;
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top