This commit is contained in:
229
API/CSHelloAyaNova/Form1.cs
Normal file
229
API/CSHelloAyaNova/Form1.cs
Normal file
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* HelloAyaNova - Developers API sample application
|
||||
*
|
||||
* Illustrates the absolute basics of connecting to the AyaNova
|
||||
* business object framework, creating an saving an object and
|
||||
* retrieving it.
|
||||
*
|
||||
* Areas outlined with //******** blocks are AyaNova specific
|
||||
*
|
||||
* Note the 6 references added to this project. Those 6
|
||||
* references are required to work with the AyaNova API.
|
||||
*
|
||||
* The referenced .dll files are the same files found in the AyaNova
|
||||
* program folder when AyaNova is installed. If you did not install
|
||||
* your test copy of AyaNova to the default folder you may need to update those
|
||||
* references
|
||||
*
|
||||
* When a new version of AyaNova is released you simply need refresh your references and rebuild your
|
||||
* application to maintain compatibility
|
||||
*
|
||||
* */
|
||||
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Collections;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Forms;
|
||||
using System.Data;
|
||||
|
||||
//******* SAMPLE ********************
|
||||
//The AyaNova business object library:
|
||||
using GZTW.AyaNova.BLL;
|
||||
|
||||
//The security library required for
|
||||
//processing the login
|
||||
using CSLA.Security;
|
||||
|
||||
//Used for login confirmation
|
||||
using System.Threading;
|
||||
//*************************************
|
||||
|
||||
|
||||
namespace HelloAyaNova
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for Form1.
|
||||
/// </summary>
|
||||
public class Form1 : System.Windows.Forms.Form
|
||||
{
|
||||
private System.Windows.Forms.Button button1;
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.Container components = null;
|
||||
|
||||
public Form1()
|
||||
{
|
||||
//
|
||||
// Required for Windows Form Designer support
|
||||
//
|
||||
InitializeComponent();
|
||||
|
||||
//
|
||||
// TODO: Add any constructor code after InitializeComponent call
|
||||
//
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
protected override void Dispose( bool disposing )
|
||||
{
|
||||
if( disposing )
|
||||
{
|
||||
if (components != null)
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
}
|
||||
base.Dispose( disposing );
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// button1
|
||||
//
|
||||
this.button1.Location = new System.Drawing.Point(103, 102);
|
||||
this.button1.Name = "button1";
|
||||
this.button1.TabIndex = 0;
|
||||
this.button1.Text = "button1";
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
|
||||
this.ClientSize = new System.Drawing.Size(385, 253);
|
||||
this.Controls.Add(this.button1);
|
||||
this.Name = "Form1";
|
||||
this.Text = "Form1";
|
||||
this.ResumeLayout(false);
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.Run(new Form1());
|
||||
}
|
||||
|
||||
//**************** SAMPLE ************************
|
||||
private void button1_Click(object sender, System.EventArgs e)
|
||||
{
|
||||
//THIS IS AN EXAMPLE ONLY, YOU APPLICATION SHOULD IMPLEMENT
|
||||
//ERROR HANDLING AND RECOVERY AND CHECK FOR EXCEPTIONS
|
||||
|
||||
|
||||
|
||||
//The call to Initialize is always the first required in any
|
||||
//application working with AyaNova, it initializes the database
|
||||
//connection and business object framework and confirms operations can
|
||||
//take place
|
||||
|
||||
//IMPORTANT: If you receive an exception when calling the Initialize method it is most likely
|
||||
//a connection string problem in your config.txt file causing the
|
||||
//database provider to throw a not found exception. Or a problem with the configuration
|
||||
//file itself like it's missing or not configured properly.
|
||||
|
||||
//Remember that in the case of any exception using the AyaNova business object library
|
||||
//it is that exception's Inner exception which is most likely the actual error (if present)
|
||||
//because AyaNova objects are invoked through a factory method which wraps any actual exception
|
||||
//generated by the business object inside a TargeInvocationException
|
||||
|
||||
try
|
||||
{
|
||||
GZTW.AyaNova.BLL.AyaBizUtils.Initialize();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//"crack" the exception to get to the innermost exception
|
||||
while (ex.InnerException != null)
|
||||
ex = ex.InnerException;
|
||||
|
||||
MessageBox.Show(ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//Once your application gets to this point right after Initialize without error
|
||||
//you can be assured that you have a clean configuration file and a connection
|
||||
//to the database and are ready to log in
|
||||
|
||||
|
||||
//This is the default AyaNova trial login and password
|
||||
//and this is how you login to AyaNova
|
||||
AyaBizUtils.Login("manager","letmein");
|
||||
|
||||
//confirm the user is logged in:
|
||||
if(Thread.CurrentPrincipal.Identity.IsAuthenticated==false)
|
||||
{
|
||||
//Nope, they are not authenticated so
|
||||
//show an error and bail out
|
||||
MessageBox.Show("Login failed");
|
||||
return;
|
||||
}
|
||||
|
||||
//at this point the user is logged in and
|
||||
//you can now work with any AyaNova business objects that
|
||||
//the login account used above has rights to.
|
||||
|
||||
//Logging OUT: There is technically no need to logout
|
||||
//simply close the program
|
||||
|
||||
//Let's just make sure the database hasn't expired
|
||||
//an expired AyaNova database is read only so
|
||||
//the code below will not work
|
||||
//AyaBizUtils handles all manner of general operations
|
||||
//that aren't related to a particular object
|
||||
if(AyaBizUtils.Expired)
|
||||
{
|
||||
MessageBox.Show("Sorry this database has an expired license: " + AyaBizUtils.ExpiryDate.ToString());
|
||||
return;
|
||||
}
|
||||
|
||||
//lets add a Client
|
||||
Client c=Client.NewItem();
|
||||
//give the client a name and ensure it's unique for testing purposes
|
||||
//by putting the current date and time after the name
|
||||
c.Name="Test client "+System.DateTime.Now.ToString();
|
||||
c.Notes="Test client created from \"Hello AyaNova\" sample API program";
|
||||
c.Save();
|
||||
|
||||
//save the ID to retrieve it later on
|
||||
//AyaNova uses Guid's for every object
|
||||
Guid gNewClientID=c.ID;
|
||||
|
||||
//some feedback to show it worked
|
||||
//fetch the newly created client back from
|
||||
//the database
|
||||
Client b=Client.GetItem(gNewClientID);
|
||||
MessageBox.Show(b.Name);
|
||||
|
||||
|
||||
//That's all there is to it.
|
||||
//If you open the database from AyaNova you will see the new
|
||||
//client ready for use.
|
||||
|
||||
//of course there are many more properties of a client that can be set
|
||||
//and many more business objects that can be worked with in a similar manner
|
||||
//check out the other samples and the developers api reference at http://api.ayanova.com/ for more information
|
||||
|
||||
}
|
||||
|
||||
//********************** END OF SAMPLE ***************************************
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user