Ticket T666245
Visible to All Users

How to create custom/dynamic properties for a non-persistent POCO marked with DomainComponentAttribute

created 7 years ago

Hello,
I try to follow Customize Business Object's Metadata to create an object dynamically. It is my understanding that XAF currently does not support dynamically created objects, so I try to work around and create custom fields in domain component object. The article says "To add a custom member to a Domain Component, handle the XafApplication.SettingUp event instead of overriding the CustomizeTypesInfo method." I found a lot of info how to add dynamic fields to persistent properties using XafTypesInfo but nothing for dynamic one.
Do you have any examples how to handle it?

Thank you.

Answers approved by DevExpress Support

created 7 years ago (modified 7 years ago)

Hello,

If you are referring to the Domain Components (DC) technology, you can find examples in the How to customize a domain component (DC) type info at runtime, e.g. define a custom member article. I found it using this search query: https://search.devexpress.com/?q=settingup%20domain%20component&f=&m=SupportCenter. We recommend that you search not only our online documentation, but also Code Examples, KB Articles and regular tickets in the Support Center for complete results.

If you are referring to Non-Persistent Objects, then this functionality is unsupported for POCO marked with the DomainComponentAttribute. If you attempt to call the ITypeInfo.CreateMember method for such a POCO, you will receive a meaningful exception.

There are still solutions, though:
1. use methods provided by the Reflection.Emit namespace (for instance, see Dynamic Type Using Reflection.Emit).
2. derive your POCO from an XPO class, though this is against XAF best practices. Example:

C#
[DomainComponent] public class MyNonPersistentObject: BaseObject { public MyNonPersistentObject(Session session) : base(session) { } public string Name { get; set; } public string Description { get; set; } }

I want to emphasize that these two solutions are edge and have no official support in our tests, demos, documentation and examples. Use them at your own risk.
3. It is possible that creating a custom form or user control will be easier to implement and maintain:
    Task-Based Help > How to: Show a Custom Windows Form
    How to: Show a Custom Data-Bound Control in an XAF View (WinForms)
More at Concepts > UI Construction > Using a Custom Control that is not Integrated by Default.

See also
How to define a business class at runtime or allow end-users to configure its members via the application UI?

    Comments (2)

      Hello Dennis,
      Thank you for the explanation. I found a solution How to add custom properties to a DetailView dynamically at runtime that very close to what I need if it can be applied to non persistent objects .

      Thank you.

      Dennis Garavsky (DevExpress) 7 years ago

        You're welcome, Tim.
        Feel free to use this example if you need to cover only DetailView functionality without full CRUD capabilities, security, validation, reporting, data analysis, etc.

        Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

        Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.