n-Tier Architecture

  • Thread starter Thread starter dkode
  • Start date Start date
D

dkode

Guest
I am laying out a static class diagram and a use case diagram in Visio for a new application I am making

Now,
I am having a very difficult time getting UML and object oriented ideas through my head.

I began by making a DataAccess class with some basic methods like so:

GetDS(ByVal sql As String)
- this runs the specified sql string and returns the dataset
GetDS(ByVal sql As String, ByVal dataTable As String)
- Overloaded GetDS Method for specifying what dataTable to put the results into. I did this so i could get a dataset with a specific table and then merge the returned dataset with another ds i might have with other relevant information in it

SaveDS(ByVal ds as DataSet)
- this saves the passed dataset back to the database
SaveDs(ByVal ds as DataSet, ByVal dataTable As String)
- saves the passed dataset datatable back to the database

I also have a business object that my other business objects derive from:

BusinessObject
Methods:
- GetDS(sql)
- GetDS(sql, dataTable)
- SaveDS(sql)
- SaveDS(sql, dataTable)

Derived classes from BusinessObjects:

Project - Inherits BusinessObject
Methods:
- GetAllProjects() : Returns a dataset with all projects
- GetProjectByID() : Returns a dataset with the specified Project
- Save(ds) : Saves the specified dataset back to the database

Employee - Inherits BusinessObject
Methods
- GetAllEmployees() : Returns a dataset with all employees
- GetEmployeeByID() : Returns a dataset with specified employee



Now, I think I am almost there with understanding n-Tier architecture, i just a few simple questions about my structure above:

1. Am I on the right track here with creating the right objects to do the right things? I dont feel like I am?

2. I just started looking into typed datasets and would like to use them in this application but I have no idea how to work them into my static structure diagram or how to properly use them. Is there a good doc I can read about implementing typed datasets into my architecture

3. If i implement the structure above, I would need to place my sql statements into my Project and Employee objects which your not supposed to do. My understanding is all SQL queries are to be placed in the data layer?

4. If I am passing all of this information back and forth with datasets, how does vb.net know which tables to insert the data into when I pass the dataset back to the data layer? would i need to specify another string for the table to save to?

5. I also read something about Collections of objects, Is this the way I want to go? I couldnt find any information on making Collections of Business Objects?

I guess I just need some direction as to where to go from here. Thank you.
 
Youre oversimplifying the function of the data tier far too much. The data tier should be returning business tier objects that can then be used without any knowledge of the underlying database. For instance, you have the following two classes defined in your business tier:

Code:
Public Class Project
     ...
End Class Project

Public Class ProjectCollection
    Inherits ArrayList

    ...
End Class ProjectCollection

A method call to your data tiers GetProjects member returns a ProjectCollection object that is populated with one or more Project objects.

C#:
public ProjectCollection GetProjects() {
    SqlConnection connection = new SqlConnection();
    SqlCommand command = new SqlCommand();
    SqlDataReader reader;
    
    connection.Open();
    
    command.CommandText = "SELECT * FROM dbo.projects";
    command.Connection = connection;
    
    reader = command.ExecuteReader();
    
    ProjectCollection pc = new ProjectCollection();
    
    while (reader.Read()) {
        Project p = new Project();
        //...
        pc.Add(p)
    }
    
    reader.Close();
    connection.Close();
    
    return pc;
} // end method GetProjects

You could then go ahead and pass that ProjectCollection object to your presentation tier which would in turn display the current projects in a form or web form.
 
Ok,

I think im starting to get it a little better now

one thing, ive read that the data layer is not supposed to have any knowledge of the business layer? do i just pass the dataset back to the business layer that would then place the rows into a collection of project objects??


So basically, for each business object, I also want to have a collection to hold those business objects? i.e. :
Project, create a ProjectCollection
Employee, create a EmployeeCollection


Then once I pass the collection to the presentation tier I can easily bind the collection to comboboxes/listboxes/textboxes etc...?

That answers question 1,

Any comments on the other things that are confusing to me? I appreciate the help. Thank you very much

DKode
 
ive read that the data layer is not supposed to have any knowledge of the business layer?
Thats simply incorrect.

Heres an exert from the Data Access Layer of Microsofts own PetShop implementation to validify my statements:

C#:
/// <summary>
/// Read an order from the database
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
public OrderInfo GetOrder(int orderId) {
            
    //Create a parameter
    SqlParameter parm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
    parm.Value = orderId;

    //Execute a query to read the order
    using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_DTC_ORDERS, CommandType.Text, SQL_SELECT_ORDER, parm)) {
    
        if (rdr.Read()) {

            //Generate an order header from the first row
            CreditCardInfo creditCard = new CreditCardInfo(rdr.GetString(2), rdr.GetString(3), rdr.GetString(4));
            AddressInfo billingAddress = new AddressInfo(rdr.GetString(5), rdr.GetString(6), rdr.GetString(7), rdr.GetString(8), rdr.GetString(9), rdr.GetString(10), rdr.GetString(11), rdr.GetString(12), null);
            AddressInfo shippingAddress = new AddressInfo(rdr.GetString(13), rdr.GetString(14), rdr.GetString(15), rdr.GetString(16), rdr.GetString(17), rdr.GetString(18), rdr.GetString(19), rdr.GetString(20), null);

            OrderInfo order = new OrderInfo(orderId, rdr.GetDateTime(0), rdr.GetString(1), creditCard, billingAddress, shippingAddress, rdr.GetDecimal(21));                                
    
            ArrayList lineItems = new ArrayList();
            LineItemInfo item = null; 
                
            //Create the lineitems from the first row and subsequent rows
            do{                                                
                item = new LineItemInfo(rdr.GetString(22), string.Empty, rdr.GetInt32(23), rdr.GetInt32(24), rdr.GetDecimal(25));
                lineItems.Add(item);
            }while(rdr.Read());

            order.LineItems = (LineItemInfo[])lineItems.ToArray(typeof(LineItemInfo));

            return order;
        
        }

    }

    return null;
}
Take note of OrderInfo, which is defined as the functions return type. This type is defined in PetShops business tier (BLL).

As for your other questions regarding DataSets I really see no reason for their use at all. A faster and more appropriate solution would be to retrieve the applications data using DataReaders which would in turn place their contents into new instances of your BLL objects, as is displayed above in both my example and Microsofts PetShop exert.
 
Ok,

I did the following to implement some collection functionality

I created a CollectionsBase class that inherits from System.Collections.CollectionBase

within CollectionBase I created 3 methods:
Add()
Remove()
Item()

Following a tutorial that showed me how to put these in there to have collection functionality

then i created 3 collection classes, one for each of my business entities, ProjectCollection, EmployeeCollection, RevenueGoalCollection

those 3 inherit from my BaseCollection class so they all have the 3 methods mentioned above.

Am i headed in the right direction with creating these collection classes?

p.s. i also find that i am asking myself if a method should be shared/not shared? How do i determine when to/when not to make a method shared

Thank you.
 
Am i headed in the right direction with creating these collection classes?
If you need to return more than one object to your business tier, than yes, you are headed in the right direction.

How do i determine when to/when not to make a method shared[?]
A method shouldnt be shared if it relies on a particular instance of its class. Take the following two examples for instance:

Code:
Public Class Math
    Public Shared Function Add(iFirst As Integer, iSecond As Integer) As Integer
        Return iFirst + iSecond Yes, I realize there could be an overflow exception
    End Function Add
End Class Math

The above method should be shared. It gets all its parameters from its argument, and none from instance members of the class.

Now take this code snippet for example:

Code:
Public Class Math
    Private iAddend As Integer = 100

    Public Property Addend() As Integer
        Get
            Return iAddend
        End Get
        Set(ByVal value As Integer)
            iAddend = value
        End Set
    End Property Addend

    Public Function Add(iNumber As Integer, iSecond As Integer) As Integer
        Return iNumber + iAddend Yes, I realize there could be an overflow exception
    End Function Add
End Class Math

This Add method relies on a property that was set prior to its invoking. Therefore, it cant be shared.
 
Back
Top