Getting started with Business objects

Hi guys,

i m trying to get started with Business Objects, but i m a little bit lost here.. hope you guys can help me out.

So far all the tutorials i read on the internet just show you how to make an "employee" or a "product" class, coding that is very simple, and i don’t have any problems using them and even binding them to Windows Forms using BindingLins<T>, etc... i ve been testing it out and it works fine.

But right now i m trying to make something that have some relations, and i cant figure out how i am supposed to do it, what i m trying to do is a simple "Order" class that is related with a customer and some products... something like:

Order
Customer
Products

So, could you guys help me out with this, if you can post some refence, a sample code would be better but anything is fine.

The main problem is that i cant figure out how to work with DataRelation in this objects, how am i supposed to make a relation between products and order how can i add, update and delete produtos


Any help is welcome!

Thanx in advance,

-Fabio.


Answer this question

Getting started with Business objects

  • John Sudds - MSFT

    I'm still looking for a good sollution for deleting items :(

    I have tried using the state, but it aint working :(

    At this moment i remove the record directly from the database wich is not a good sollution.

    Lets say u edit an order, having like 3 products, u delete a product (directly deleted from the database) but u don't save the order ... u still lost the product record while my new or edited sub items are saved when the parent is saved.

    Its an issue i'm still working on :(


  • JIM.H.

    Thanx man, this is really helpfull!

    So you Store the products in the order as a list of "Product" objects, my main doubt was about how to store the itens on the order, now i think i can make this whole thing work somehow :)

    And also i found a great Article about validation and some other stuff on Business Objects, it worked like a charm here, if you guys are interested here it is:

    http://thecodeproject.com/csharp/DelegateBusinessObjects.asp


    thanx a lot!

    -Fabio.

  • rohit nagesh

    Nightmare_BE wrote:

    I'm still looking for a good sollution for deleting items :(

    I have tried using the state, but it aint working :(

    At this moment i remove the record directly from the database wich is not a good sollution.

    Lets say u edit an order, having like 3 products, u delete a product (directly deleted from the database) but u don't save the order ... u still lost the product record while my new or edited sub items are saved when the parent is saved.

    Its an issue i'm still working on :(



    I was thinking in this stuff, and the better idea i came was to make it like this:

    You load an order from de database, then you make a copy of the itens.. something like a snapshot, or just put all the original item ids in a List.

    When the user press "Save" you compare that list with your current itens, if you have something there that isnt in your current List, you delete.

    At least it was the easiest way i could think about.

  • Junmei

    That would work, but when u have lots of data and use a copy of the data, all data is in the memory 2x.

    Maybe i found a solution but din't test it.

    Instead of returning list<dataitem> create a list class for each data item. I think the datagrid and stuff is using the GetEnumerator() method for getting the data, so if u override that function and only return items that have the rowstate u want (so dont return deleted items) it might work

    U could also add a rejectchanges method where u reset the rowstate of each item to unmodified


  • satya999

    Oh, and another question...

    Lets imagine that you load an Order from the database, and this Order have 3 itens, then you put those itens on a List and Bind it to a DataGrid....

    What happens if i remove an item

    is it just goind to be removed from the list how am i supposed to remove it from my database

    I know that in DataTables for example you have the States, Added, Modified and Removed, Added and modified are easy to implement, but what about the Removed itens

  • etude

    I have been looking into it and found a solution for hiding deleted items using the GetEnumerator .... dint really test it with datagrids but it should work :)

     

    First of all u need an interface used by each datatype (called IRow)

    public interface IRow
    {
       void Delete();
       DataRowState RowState { get; }
    }

    Next u need a collection class using a custom enumerator

    public class RowCollection<T> : List<T>
    {
       new public RowEnumerator<T> GetEnumerator()
       {
          return new RowEnumerator<T>(base.GetEnumerator());
       }
    }

     

    public class RowEnumerator<T> : IEnumerator<T>
    {
       #region Class Members

       private
    IEnumerator<T> m_enumerator;

       public
    RowEnumerator(IEnumerator<T> enumerator)
       {
          m_enumerator = enumerator;
       }

       #endregion
       #region
    IEnumerator<IRow> Members

       public
    T Current
       {
          get { return m_enumerator.Current; }
       }

       #endregion
       #region
    IDisposable Members

       public
    void Dispose()
       {
          m_enumerator.Dispose();
       }

       #endregion
       #region
    IEnumerator Members

       object
    System.Collections.IEnumerator.Current
       {
          get { return m_enumerator.Current; }
       }

       public
    bool MoveNext()
       {
          if (m_enumerator.MoveNext())
          {
             if (((IRow)m_enumerator.Current).RowState == System.Data.DataRowState.Deleted)
                return MoveNext();
             return true;
          }
          return false;
       }

       public void Reset()
       {
          m_enumerator.Reset();
       }

       #endregion
    }

    Now i only hope the datagrid classes are using the enumerator for filling the grid.
    The enumerator only returns non deleted record. When using the row index u can access all items

    public class TestRow : IRow
    {
       private DataRowState m_rowState;
       private Guid m_id;
       private string m_name;

       public TestRow()
       {
          m_rowState =
    DataRowState.Added;
          m_id =
    Guid.NewGuid();
          m_name =
    "";
       }

       public TestRow(Guid id, string name)
       {
          m_rowState =
    DataRowState.Unchanged;
          m_id = id;
          m_name = name;
       }

       public void Delete()
       {
          m_rowState =
    DataRowState.Deleted;
       }

       public DataRowState RowState
       {
          get { return m_rowState; }
       }

       public Guid ID
       {
          get { return m_id; }
       }

       public string Name
       {
          get { return m_name; }
          set { m_name = value; }
       }
    }

    RowCollection<TestRow> rows = new RowCollection<TestRow>();
    rows.Add(
    new TestRow(Guid.NewGuid(), "Record 1"));
    rows.Add(
    new TestRow(Guid.NewGuid(), "Record 2"));
    rows.Add(
    new TestRow(Guid.NewGuid(), "Record 3"));
    rows.Add(
    new TestRow(Guid.NewGuid(), "Record 4"));
    rows.Add(
    new TestRow(Guid.NewGuid(), "Record 5"));
    rows.Add(
    new TestRow(Guid.NewGuid(), "Record 6"));

    rows[2].Delete();
    rows[3].Delete();
    rows[4].Delete();

    Console.WriteLine("===Active rows===");
    foreach(TestRow row in rows)
       Console.WriteLine(row.Name);

    Console
    .WriteLine("\r\n===All rows===");
    for (int index = 0; index < rows.Count; index++)
       Console.WriteLine(rows[index].Name);

    Console.ReadKey();


  • Bob Reinke

    Guess the dataaccess class depends on your database type.

    I allways use sql server, but my data classes using the interface IDataReader to be able to be used by different database types.

    This is how i read the data into a list

    public list<datatime> Load()
    {
    List<dataitem> data = new list<dataiutem>();
    sqlcommand command = null; 
    sqldatareader reader = null; <<< implements the IDataReader

    try
    {
    command = new sqlcommand();
    ... setup the command, query and parameters

    reader = command.executereader();

    while(reader.read())
       data.add(new dataitem(reader);
    }
    catch(Exception ex)
    {
    .... handle error
    }
    finally
    {
    ... cleanup reader and command
    }

    return data;
    }

     


  • Catalin Zima

    Yeah, many people do it that way cause they only use 1 type of database :(

    Sometimes they forget the power of interfaces in .net ... i guess the samples returning the reader are 3the party samples. As far as i know microsoft advices to use interfaces in these case.


  • NLCandyman

    This is how i do it (not sure its the correct way)

    When i have a class order i have 2 constructors

    Order()
    {
    ... init default data
    }

    Order(IDataReader reader)
    {
    ... get data from reader
    }

    When i have related data like products on the order i do like this

    private List<Order> m_order = new List<Order>();

    inside the order class.

    So when i'm showing the details of an order using 1 order class out the orders collection, i bind the Order.Products

    So the list of products is INSIDE my order class.

    Like i said, not sure its the right way, but it works :)


  • vdv_phuong

    Nice, thanx!

    It should be better to rewrite that than rewrite the classes to change the database!

    I saw some tutorials and stuff that used some metods like this:

    public SqlDataReader GetProdutos() {
    SqlConnection....
    ....
    }

    I didnt like it cause i imagined if i had to use another provider like Firebird (wich i m currently using) i would have to rewrite all my Classes!

    Again,

    Thanx a lot!

    -Fabio.

  • OmegaMan

    Anyway, if i m not asking too much, how do you make your DataAcess classes.. i.e. how do you get and pass the DataReader to read the products

    I m asking this because one of my goals about bussiness objects is make it easy to use with more than one DataBase so am reading a lot about it to know what are the best practices before writing my code.


    Thanx in advance,

    -Fabio.

  • chazparks

    I made a simple sample for u :)
    Its not been tested ... but this is the concept of how i handle these things ... hope u get what i mean :)

    I made 3 classes, Order, Customer and Product

    Each order has a customer presentet by the customer class. An order also has multiple products
    U will notice in the code my database fields allways start with the table name, like id of products = ProductID in table Products
    This is mostly for making the IDataReader get the right fields when using nested classes.

    So when i load an order from the database i create the order class using the IDataReader, that IDataReader is passed to the customer class to get the all info about the customer. (u need to make joined query for getting all data ... select Order.*, Customer.* from orders inner join customer ...)

    For the products i use an other way of loading cause there are multiple records ...
    Lets say u first get a list of your orders ... to get the datalist call OrderData.GetOrders() return List<Order>
    The customer data will allready be included in the order

    When u get to the details form of the order, load the products used by the order, so on the details_load() event call OrderData.GetProducts(Order order) that will fill the Order.Products list.

    So in OrderData.GetOrders() u create a new list of orders and return it to the application
    In OrderData.GetProducts u pass the order and fill the pre created Products list in the order class.

    //=============================
    // Order.cs
    //=============================
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;

    namespace
    OrderSample.DataItems
    {
    public class Order
    {
    private List<Product> m_products = new List<Product>();
    private Guid m_id;
    private Customer m_customer;
    private DateTime m_dateCreated;

    public
    Order()
    {
    m_id =
    Guid.NewGuid(); // New record, so generate an id
    m_customer = null;
    m_dateCreated = DateTime.Now;
    }

    public Order(IDataReader reader)
    {
    // Get the data from the datareader
    m_id = new Guid((string)reader["OrderID"]);
    m_dateCreated = (
    DateTime)reader["OrderDateCreated"];
    m_customer =
    new Customer(reader); // Create the subclass using the reader
    }

    public List<Product> Products
    {
    get { return m_products; }
    }

    public Guid ID
    {
    get { return m_id; }
    }

    public
    Customer Customer
    {
    get { return m_customer; }
    set { m_customer = value; }
    }

    public DateTime DateCreated
    {
    get { return m_dateCreated; }
    }
    }
    }

    //=============================
    // Customer.cs
    //=============================
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;

    namespace OrderSample.DataItems
    {
    public class Customer
    {
    private Guid m_id;
    private string m_name;

    public Customer()
    {
    m_id =
    Guid.NewGuid(); // New record, so generate an id
    m_name = "";
    }

    public Customer(IDataReader reader)
    {
    // Get the data from the datareader
    m_id = new Guid((string)reader["CustomerID"]);
    m_name = (
    string)reader["CustomerName"];
    }

    public Guid ID
    {
    get { return m_id; }
    }

    public string Name
    {
    get { return m_name; }
    set { m_name = value; }
    }
    }
    }

    //=============================
    // Product.cs
    //=============================
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;

    namespace OrderSample.DataItems
    {
    public class Product
    {
    private Guid m_id;
    private string m_name;
    private float m_price;

    public Product()
    {
    m_id =
    Guid.NewGuid(); // New record, so generate an id
    }

    public Product(IDataReader reader)
    {
    // Get the data from the datareader
    m_id = new Guid((string)reader["ProductID"]);
    m_name = (
    string)reader["ProductName"];
    m_price = (
    float)reader["ProductPrice"];
    }

    public Guid ID
    {
    get { return m_id; }
    }

    public string Name
    {
    get { return m_name; }
    set { m_name = value; }
    }

    public float Price
    {
    get { return m_price; }
    set { m_price = value; }
    }
    }
    }


  • Dvlnblk

    Here's a sample method i use in my current project

     

    public static List<Contact> GetKlanten()
    {
       List<Contact> data = new List<Contact>();
       SqlDataReader reader = null;
       SqlCommand command = null;

       try
       {
          // Create the sql command
          command = new SqlCommand();
          command.Connection =
    Sessie.Connection;
          command.CommandType =
    CommandType.Text;
          command.CommandText =
    "SELECT * FROM [Contacten]";

          // Execute the sql command
          reader = command.ExecuteReader();

          // Process the reader
          while (reader.Read())
             data.Add(
    new Contact(reader));
       }
       catch (Exception ex)
       {
          Sessie.HandleException(ex);
       }
       finally
       {
          // Cleanup the sql data reader
          if (reader != null)
          {
             reader.Close();
             reader.Dispose();
          }
          reader =
    null;

          // Cleanup the sql command
          if (command != null)
             command.Dispose();
             command =
    null;
      

       // Return the data
       return data;
    }

     


  • cplusplus1

    Anyone

  • Getting started with Business objects