Example E1198
Visible to All Users

How to generate persistent classes at runtime based on a dataset

Files to look at:

This example demonstrates how to generate persistent classes at runtime

See Also:
How to dynamically build a persistent class from a DataSet and then bind it to the GridControl

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WindowsApplication4/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.Xpo; using DevExpress.Xpo.DB; using DevExpress.Xpo.Metadata; namespace WindowsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private XPClassInfo CreateObject(IDataStore s, string name, Type baseType) { DBTable[] tables = ((IDataStoreSchemaExplorer)s).GetStorageTables(name); DBTable table = tables[0]; XPClassInfo info = XpoDefault.Dictionary.CreateClass(XpoDefault.Dictionary.QueryClassInfo(baseType), table.Name); string key = table.PrimaryKey.Columns[0]; foreach (DBColumn c in table.Columns) { XPMemberInfo mi = info.CreateMember(c.Name, DBColumn.GetType(c.ColumnType)); if (key == c.Name) mi.AddAttribute(new KeyAttribute(c.IsIdentity)); } return info; } string connection = SQLiteConnectionProvider.GetConnectionString(@"nwind.sqlite"); private void Form1_Load(object sender, EventArgs e) { IDataStore s = XpoDefault.GetConnectionProvider(connection, AutoCreateOption.None); XPDictionary d = new ReflectionDictionary(); XpoDefault.Dictionary = d; XpoDefault.DataLayer = new SimpleDataLayer(d, s); XPClassInfo order = CreateObject(s, "Order", typeof(DetailDataObject)); XPClassInfo customer = CreateObject(s, "Customer", typeof(ParentDataObject)); AssociationAttribute a = new AssociationAttribute("CustomerOrders"); //, typeof(DetailDataObject) a.ElementTypeName = "Order"; AssociationAttribute a1 = new AssociationAttribute("CustomerOrders"); //, typeof(ParentDataObject) order.CreateMember("CustomerId", customer, new Attribute[] { a1 }); customer.CreateMember("Orders", typeof(XPCollection), true, new Attribute[] { a }); gridControl1.DataSource = new XPCollection(Session.DefaultSession, customer); } } [NonPersistent, MemberDesignTimeVisibility(false)] public class DetailDataObject : XPLiteObject { public DetailDataObject(Session session, XPClassInfo classInfo) : base(session, classInfo) { } } [NonPersistent, MemberDesignTimeVisibility(false)] public class ParentDataObject : XPLiteObject { public ParentDataObject(Session session, XPClassInfo classInfo) : base(session, classInfo) { } } }
WindowsApplication4/Program.cs(vb)
C#
using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsApplication4 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

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.