Creating a new Class

This is a guide that shows you how to create classes to interact with any Preactor table defined in the TDF.

For this example we will use the "Customers" table:

1) First in your own Project create a ICustomer interface like this (Create a file called ICustomer.cs):

using Preactor.ObjectModel.Interfaces;

namespace YourProjectName
{
    public interface ICustomer : IPreactorRecord
    {
        string Name { get; set; }
    }
}

2) Create a Customer class to implement the interface (Customer.cs):

using Preactor;
using Preactor.ObjectModel;
using Preactor.ObjectModel.Attributes;
using Preactor.ObjectModel.Entities;

namespace YourProjectName
{
    public class Customer : PreactorRecord, ICustomer
    {
        public Customer(PrimaryKey id, PreactorObjectModel preactorObjectModel)
            : base(id, preactorObjectModel)
        {}

        [PreactorField("Name", FieldDeclarationType.FieldName)]
        public string Name { get { return (string)GetValue("Name"); } set { SetValue("Name", value); } }

        [PreactorField("Contact Tel. No.", FieldDeclarationType.FieldName)]
        public string PhoneNumber { get { return (string)GetValue("PhoneNumber"); } set { SetValue("PhoneNumber", value); } }
    }
}

3) You now have to register the ICustomer interface and the associated Customer class with POM in your custom action class. Note that the Register Method will take a string which will correspond to the name of the table used in the TDF file.

    [Guid( "8B2EAB92-0C36-4D03-8034-...." )]
    [ComVisible( true )]
    [ClassInterface( ClassInterfaceType.None )]
    public class CustomActions : ICustomActions
    {

        private static PreactorObjectModel Pom { get; set; }

        static CustomActions()
        {
            Pom = new PreactorObjectModel();
            Pom.Register<ICustomer, Customer>( "Customers" );
        }
        ....

4) You can now manipulate the customer data, create records, edit fields etc...

var customers = Pom.GetAllRecords<ICustomer>().ToList();

var customer = Pom.GetRecord<ICustomer>( 1 ) as Customer;
customer.PhoneNumber = "999-000";

Obviously if you are going to use this class a lot it makes sense to create an extension function for it in your project so that you can do:

Customer customer = pom.GetCustomer(3);
var allCustomers = pom.GetAllCustomers();

// etc...

NOTE: As you can see you can define any interface and implementation you like in your own project. In fact you can even overwrite the interfaces defined in POM with your own, HOWEVER the very purpose of having interfaces in common is to allow us to share code with one another and know that it will work. For this reason if you think we should add or remove some properties from the interfaces please let us know.

Last edited Sep 12, 2012 at 2:10 AM by bvsms, version 7

Comments

No comments yet.