• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 762
  • Last Modified:

3-tiered application - layer dependency

this question is related to 3-ties application design only, not 4+ tiered.

DAL
BLL
UI

UI form - displays collection of clients info (this is the example I would like use as reference)

I am interested in how would what layer pass data up to the UI form? The important thing is that layer dependency cannot be mutual, meaning all three layers reside in a separate dll (exe). So no need for coding properties, methods, etc, I just need interfaces to see how would data flow.
0
Priest04
Asked:
Priest04
  • 6
  • 6
  • 2
  • +2
4 Solutions
 
sunnycoderCommented:
DAL would typically encapsulate data storage and retrieval from rest of the application. Apart from DAL, no other component would know where and how data is stored. DAL would pass the data on to BLL

BLL would use this data to generate information of interest and pass it to UI for display.

http://msdn.microsoft.com/en-us/library/aa581776.aspx
http://msdn.microsoft.com/en-us/library/ms998478.aspx
http://en.wikipedia.org/wiki/Multitier_architecture
0
 
Priest04Author Commented:
This is not the answer I was looking for. I know what is the purpose of those 3 layers, but when we talk about DAL and BLL, how would DAL pass data from database to DLL. An example of interface for a DAL class and for BLL class, regarding loading customers, would be great.

I am not interested in using DataSets, DataAdapters etc, I want to use custom entities.
0
 
DarrenDCommented:
Hi,

You can get the data from the database any way you want.

Some people use Stored procedures in order to retrieve data via a dataset and then populate their custom (customer) classes by iterating through each record and creating a List<Customer> of their custom object and returning that. Others use LINQ to SQL as a way to retrieve the data and then iterate through this collection and populate their custom objects. You could also use ADO.NET Entities which is another way to get data from the database and populate custom objects at the same time. Another option is to buy a ORM tool.

http://msdn.microsoft.com/en-us/library/aa697427(VS.80).aspx

http://msdn.microsoft.com/en-us/library/aa697428(VS.80).aspx

The business layer will then use your custom objects in order to fulfill some business logic (whatever that may be).

Darren


         public interface IDatabase
	{
 
		// Used when calling stored procedures and returning scalar values.
		DataSet ExecuteSPQuery(string sConnection, string sStoredProcName) ;
		int ExecuteSPNonQuery(string sConnection, string sStoredProcName) ;
		string ExecuteSPScalar(string sConnection, string sStoredProcName) ;
 
		// Parameter overloaded methods.
		void AddParameter(string sParamName, SqlDbType EnumDBType, int iStrLen, string sValue) ;
		void AddParameter(string sParamName, SqlDbType EnumDBType, int iStrLen, byte[] sValue) ;
		void AddParameter(string sParamName, SqlDbType EnumDBType, long lValue) ;
 
		// Methods to add return and output parameters and retrieve their values.
		void AddOutputParameter(string sParamName, SqlDbType EnumDBType, int iStrLen) ;
		object GetOutputParameter(string sParamName) ;
 
		int Timeout{ get; set; }
	}

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
Priest04Author Commented:
Ok, seems you do not undertstand me. I have DAL class ready for getting data FROM database. I will post bellow how it looks like. The question I am asking is related to passing data that is taken from database TO business layer - how? What is the best pattern to do this? And a basic example of interface related to customers would be great.

This is how methods for getting data from database look like

int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
int ExecuteNonQuery(CommandType commandType, string commandText, params IDataParameter[] commandParameters);
int ExecuteNonQuery(IDbTransaction trans, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
 
IDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
IDataReader ExecuteReader(CommandType commandType, string commandText, params IDataParameter[] commandParameters);
 
object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
object ExecuteScalar(CommandType commandType, string commandText, params IDataParameter[] commandParameters);
object ExecuteScalar(IDbTransaction trans, CommandType commandType, string commandText, params IDataParameter[] commandParameters);
 
IDataParameter CreateParameter(string paramName, System.Data.DbType paramType, object paramValue);

Open in new window

0
 
VotechCommented:
Hi Priest04,

See http://www.codeplex.com/LayerSample for a great .NET project architecture which includes communicating to a business logic layer from the UI model.

It does however include other layers, but just ignore them.
0
 
ai_ja_naiCommented:
I really don't understand the problem: it's the DAL duty to retrieve database information and create an object that BLL can cruch.
Now, creating that object is a very technical specific matter. I know how to do it in Java because I personally studied JDBC type 2 connector. The DAL retrieves the aswer string to the query and parses the attributes to build a bean. In Java is something like what i attached
If you have this type of things you are done.
Did I answer?
String retrieve = "select * from pdoducts where id=?";
statement = connection.prepareStatement(retrieve);
statement.setInt(1, id);
ResultSet result = statement.executeQuery();
if (result.next()) {
prodotto = new Product();
prodotto.setCode(result.getString("code"));
prodotto.setName(result.getString("name"));
prodotto.setDescription(result.getString("description"));
prodotto.setAvailability(result.getInt("availability"));
prodotto.setPrice(result.getInt("price"));
prodotto.setId(id);
}

Open in new window

0
 
Priest04Author Commented:
Thanks for the response, guys.

@Votech

I will look at the code you have provided and will let you know if this is what I wanted.

@ai_ja_nai

The problem is I dont understand the approach with 3 layered model. Here in your example you use Product class. Where is the Product class defined? If in BLL, then DAL will need a reference to BLL in order to use Product class. If this is true, then BLL CANNOT reference DAL, because circular dependency is not allowed. And then how would BLL receive data from DAL?

0
 
ai_ja_naiCommented:
The 3 tier approach principle is really straightforward: separation of interests and competences. Everyone has is own duties and working at problems belonging to other layers is forbidden.

The Presentation layer, the top, simply catches and dispatches requests to the business logic tier. Also, it formats the answers in a comprehensible to the user way. But it never perform any business operation.

The Business logic executes all the operations of the domain.
If you are developing, for example, an inventory system for a warehouse, it would be the tier that lets you modify names, quantities and so on. It works on preinitialized software objects and never communicates directly with the underlying SQL db

The Data Access Object layer is the one who produces, from db informations, the sfotware objects which will be manipulated by the business layer.
It manages queries and returned sets and never lets any other layer to interfere or know how it works.

Now, the product class is a bean class defined somewhere in the business logic, like the snippet I attach here.
The GRASP Information Expert pattern says "Assign responsibilities to the one who owns the information". Well, since only the business is allowed to manipulate the beans (while only the DAL is allowed to read and write to/from persistent information) the product definition must stay in the business tier.
But a tier is not something that has to be phisucally separeted. Of course, everything will be in its own packages, but everybody can access to everything.

Now, a good idea is to create a common facade of callable methods to allow interoperativity.
When needed, the program will call for a method which returns an instatiated object, available to all the application environment (call to DAL) and throwable either to the presentation layer (call for UI) or to a "modify price" operation (call for BLL). it depends on who called the create method.
public class Product {
	private int productCode;
	private String name;
	private String description;
	private int price;
	private int availability;
 
...various setters and getters...}

Open in new window

0
 
ai_ja_naiCommented:
To further explain, the user press a button on the web page, asking to modify a price.
The ui dispatches a request to the business logic, which has a method to modify the 'price' attribute of a product. This method accept as input paramenter a product object and the new price.Since this object is not already there, it automatically triggers a request of creating the object. This dispatches a request to a method belonging to the DAL, which retrieves the informatios from the db and assembles a bean, returned by the just invoked method.
Then, this bean is processed by the modify_price method, which at the end calls for a store_modifications methods, which takes as input a product bean. This executes an update on db table and stores the new informations. The control return to BLL, that in turn passes it to the UI, which writes on the screen "operation done".
Now, the product bean is still there, until the garbage collection will decide it can be killed, waiting for new operations to be done on it.
0
 
DarrenDCommented:
Hi again,

Now correct me if I'm wrong but I don't think that I am. Firstly we should all be talking about the same thing.

A tier represents a physical seperation.  3- Tier.
Client PC - Business Server - Database Server

A layer represents a logical seperation. This may be considered 4 Layer but it still completeson a 3 - Tier system.
Presentation Layer (Client PC) - Business Layer (Business Server) - Data Layer (Business Server) - Data Access Layer (Business Server).

I think that what you are looking for is the Data Layer. Whereby the Data Layer calls the Data Access Layer that performs all of the database persistence methods ie: Execute AddParameter etc.. Populates the Data Layer which are then passed back to the Business Layer to perform som work.

Some people tend to put the Business Layer and Data Layer into a single overall named Business Layer. (Single or Multiple DLL's)

This is where your problem lies. On a project I have just finished working on we seperated the Business Layer and Data Layer into different DLL's etc but the Business Layer and Data Layer reside on the same Tier (Physical Machine) it is only a logical seperation.

So the Data Layer needs a reference to the Data Access Layer. The Business needs a reference to the Data Layer and the Presentation Layer needs to reference the Business Layer



Cheers,

Darren
0
 
Priest04Author Commented:
I do not understand this part:

>>But a tier is not something that has to be phisucally separeted. Of course, everything will be in its own packages, but everybody >>can access to everything.

This I cannot manage to make it work. When I keep all 3 layes in different projects, I cannot find way to reference them.

>>Now, a good idea is to create a common facade of callable methods to allow interoperativity.
>>When needed, the program will call for a method which returns an instatiated object, available to all the application environment >>(call to DAL) and throwable either to the presentation layer (call for UI) or to a "modify price" operation (call for BLL). it depends >>on who called the create method.

I also dont know how to implement that in practice. Here is a real example: I create one project called DAL, and in there, beside having a class that I mentioned before (that executes statements on database), which I will call SqlHelper.  I also have one class called ProductInfo, which, to my thinking should contain next methods

class ProductInfo
{
    public Product GetProduct(int productID)  {....}
    public IList<Product> GetProducts()  {....}
    public int SaveProduct(int productID)  {....}
    public int SaveProducts()  {....}
    public int DeleteProduct(int productID)  {....} // this one maybe doesnt need to exist, maybe it should be handled by SaveProducts

}

thic class uses SqlHelper class to do CRUD operations. I compile this dll into DAL.dll. As it can be seen, DAL uses Product class, which is defined in BLL.dll. So it needs reference to BLL.dll.

Next I have one project called BLL. It has Product class which you have defined in your last post. Can you post the interfaces that will be needed in BLL to get data from database through DAL.dll. I cannot reference DAL.dll, IDE doesnt allow me to - I get circular dependency is not allowed exception.
0
 
Priest04Author Commented:
Hello Darren,

to simlify thigs, we will talk here about logical separation only. When I speak of 3 laters, that would mean UI (app,exe), BLL (bll.dll) and DAL (dal.dll). So, if I understood you completely. you say that it is "not possible" to have 3 layers, but we need a 4th dll, so BLL and DAL should reference to this 4th dell. If this is correct, then this is how I do my applications now, but I thought of maybe I am doing something wroing, and maybe there isnt need for the 4th layer. If you look at this example, MS talks about 3 layered approach. When I said "not possible", I meant in a feasable  way, since you can always use arrays to pass thata from DAL to BLL, but that would again mean a 4th layer, since here we are using mscorlib.dll (array class).

http://msdn.microsoft.com/en-us/library/ms978689.aspx
0
 
ai_ja_naiCommented:
Ok, ProductInfo is your facade of DAL functions. Everytime you need something that has to do with persistence layer, you ask for this.

Now, the application BLL must be aware of  the ProductInfo class, to call its methods, which call at their own turn the CRUD methods. Or, at least, must import the DAL methods to contact the db in some way.

I see that your IDE doesn't allow you due to circular dependencies. I belive that there is a design mistake, because it's a vertical dependency, it cannot be circular. BLL asks operations to DAL, which return something useful to BLL. Stop. DAL of course must import the Product class to build a product object, but it never relies on BLL operations.
0
 
Priest04Author Commented:
So, what can I do about it?

>> BLL asks operations to DAL, which return something useful to BLL.

this means that BLL needs reference to dal.dll

>> DAL of course must import the Product class to build a product object, but it never relies on BLL operations.

this means that DAL needs reference to bll.dll. which is not possible to achieve.

That was my primary question. :)

If you believe its a design mistake, I would then need information how to redisgn it, but without importing 4th layer. Is this possible or not? According to headline from MS link I posted above, its possible.
0
 
ai_ja_naiCommented:
The mistake was to build definitions of domain objects, shared by everybody, inside a layer. I never said that being used by a layer they should be part of the latter. Of course, now there is a circular dependency.
You should put those definitions aside, in an own dll. Or, if you HAVE to put them into a layer, put them into DAL.dll, so DAL may use it because is built in and BLL inherits it on the import.
0
 
ai_ja_naiCommented:
By the way, a layer doesn't have to be a unique file. Most of times, it's a folder or an archive with many files inside. That folder/archive is called a package and is the best way to organize the components
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 6
  • 6
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now