using System;
using System.Collections;
using System.Diagnostics;
using C1.Data;

namespace DataLibrary
{
    /// <summary>
    /// Remote service class: supports communication with the library
    /// </summary>
    public class RemoteService : C1.Data.RemoteDataService
    {
        public C1DataSet MyCustomFill (string dataSetDefName, FilterConditions filterConditions)
        {
            // Business method: called from the client as c1DataSet1.RemoteDataService.CustomFill() both in 2-tier and 3-tier configurations.
            // C1DataSet is serializable (has SerializableAttribute and implements ISerializable), so it can be passed
            // to the server running on another machine.
            // In this sample, the method just fills a data set and passes it back to the client.
            // In real-life applications, you can add whatever logic you need in business methods, and add as many
            // business methods as you need.
            C1DataSet dataSet = new C1DataSet();
            dataSet.DataLibrary = "DataLibrary";
            dataSet.SchemaClassName = "DataLibrary.DataClass";
            dataSet.DataSetDef = "DataSet";
            dataSet.Fill(filterConditions, false);
            return dataSet;
        }

        public C1DataSet MyCustomUpdate (C1DataSet dataSet)
        {
            // Business method: called from the client as c1DataSet1.RemoteDataService.CustomFill() both in 2-tier and 3-tier configurations.
            // C1DataSet is serializable (has SerializableAttribute and implements ISerializable), so it can be passed
            // to the server running on another machine.
            // In this sample, the method just calls Update.
            // In real-life applications, you can add whatever logic you need in business methods, and add as many
            // business methods as you need.
            dataSet.Update();
            return dataSet;
        }

        public override void Fill (C1DataSet dataSet, FilterConditions filterConditions,
            string[] diagEntityNames, C1.Data.SchemaObjects.ConnectionCollection dynamicConnections, bool beginTransaction)
        {
            // You can call business methods to fill and update, as above, or you can rely on C1DataObjects doing fill/update automatically.
            // If you rely on automatic Fill, this virtual method will be called each time you call C1DataSet.Fill
            // (or at startup when the data set is filled due to FillOnRequest=True).
            // You can override this virtual method to customize Fill behavior. For example,
            // you can set beginTransaction = true, as below, to make C1DataObjects perform database fetch in
            // a transaction (by default, transactions are not used in Fill, only in Update).
            // Or you can set Connection.ConnectionString in the schema, or set Connection.DbConnection/DbTransaction
            // directly to a pre-created connection/transaction object (see DataClass.cs, BeforeFetch event).
            // You can also customize the Fill process in various other ways, including filling the dataSet manually
            // instead of calling base.Fill().
            beginTransaction = true;
            base.Fill(dataSet, filterConditions, diagEntityNames, dynamicConnections, beginTransaction);
        }

        public override void Update (C1DataSet dataSet, C1.Data.SchemaObjects.ConnectionCollection dynamicConnections, bool beginTransaction)
        {
            // You can call business methods to fill and update, as above, or you can rely on C1DataObjects doing fill/update automatically.
            // If you rely on automatic Update, this virtual method will be called each time you call C1DataSet.Update
            // You can override this virtual method to customize Update behavior. For example,
            // you can set beginTransaction = false, as below, to prevent C1DataObjects from using ADO.NET
            // transactions in Update. By default, beginTransaction = true, which means C1DataObject uses
            // ADO.NET transactions. This can be undesirable when you use COM+ automatic transactions
            // (distributed transactions spanning multiple databases).
            beginTransaction = false;
            // To use distributed transactions, set beginTransaction = false here, and call a method in an object
            // with appropriate TransactionAttribute, perform the Update in that method (for example, calling back to this
            // object's base.Update() or calling a business method of this object).
            //
            // Overriding this method, you can also change Connection.ConnectionString in the schema,
            // or set Connection.DbConnection/DbTransaction directly to a pre-created connection/transaction object
            // (see DataClass.cs, BeforeUpdate event).
            // You can also customize the Update process in various other ways, including updating the database manually
            // instead of calling base.Update().
            base.Update(dataSet, dynamicConnections, beginTransaction);
        }
    }
    
}
