views:

213

answers:

1

Hi


Assume we create 3-tier module, which enables us to display/create/edit articles. Articles are:

• organized into categories
• before article can be published, admin has to approve it by setting Approve field of Articles DB table to true
• by setting MembersOnly field ( Articles table ) admin can also specify whether particular article can be viewed by anybody or only by registered users
Articles table also has ExpiredDate field, which tells when will the article expire and thus no longer be published


a) At DAL layer we provide methods GetAllArticles, GetArticlesByCategory, GetPublishedArticles and GetPublishedArticlesByCategory for retrieving articles from the DB

At BLL layer we use GetArticles() overloads to call all of the above DAL methods.


  • What are the some of the benefits of using single overloaded method at BLL layer instead of BLL methods having one-to-one correspondence with DAL methods? Only advantage I can think of is that this way same ObjectDataSource control can call two or more of GetArticles() overloads, depending on the value of parameters, for example:

     public static List<Article> GetArticles(bool publishedOnly)
     {
        if (!publishedOnly)
             return GetArticles();
          ...
     }
    


  • If you don’t also design UI layer and thus can’t be sure which methods UI programmer will prefer the most, would it be best practice for DLL layer to provide four overloads of GetArticles + GetAllArticles, GetArticlesByCategory, GetPublishedArticles and GetPublishedArticlesByCategory?


2) When designing DAL methods to retrieve data from DB, how can you in advance know/predict ( without first designing the UI ), exactly which methods (for accessing DB) we should create at the DAL layer?

Namely, in the previous example I’ve had several methods for retrieving articles based on number of parameters ( based on category they belong to, whether we only want published articles etc). Assuming I’m selling this module to third party UI developers, then there is no way to know which data access methods they would prefer the most:


a) so should I create as many data access methods as I can think of ( one for getting all articles that are already expired, one for getting all articles that are already expired, but were never published, one for getting all articles that are not published, one for getting all articles that can be view by registered users only… ) ?


b) Even if all three layer are written by myself – should I still create as many data access methods as I can think of?


thanx


EDIT:

A common way to achieve this is to use interfaces to define the behavior of the API.

a) I’m not sure I understand this. Which class should implement this interface? Perhaps DLL class? In other words, if the name of my DLL class is Article, then third party would derive class named ChildArticle from Article, where ChildArticle would also implement this interface? Or did you mean something else?


b) Anyways, as far as I understand it, providing interface ( which declares defines additional DLL methods to retrieve articles from DB ) would also require DAL class to already have appropriate methods defined, which would be called by methods declared in the interface?


To your point, I believe it is a good idea to prefer fewer coarse-grained methods in the BLL to cover all the functionality required by an entire business operation

I’m not familiar with this term, but you’re prob suggesting that we should prefer overloaded GetArticles() over GetAllArticles, GetArticlesByCategory, GetPublishedArticles and GetPublishedArticlesByCategory?


A) The design of an API is strictly related to what it is meant to achieve and by whom it will be used. In practice, this means that you should know the target audience of your API, and give them only what they need to get the job done.

Unless I personally interview the people that would buy my product, I can generally guess which methods they would find useful, but within that space, there are still any number of possible methods I could define. Thus, how should I know whether they would also find use for, say, GetArticles() overload which retrieves articles that already expired?!


On the other side it is perfectly fine to have many smaller data-centric methods in the DAL to work with specific pieces of data.

If not DLL, should DAL have as many data access methods as I can come up with ( for particular target audience of course )?


SECOND EDIT:

A few extensibility points can be built into the API to obtain a certain degree of flexibility. A common way to achieve this is to use interfaces to define the behavior of the API. This will allow consumers to replace or extend the pieces of the built-in functionality by providing custom implementations.

Assume I create a BLL layer and then provide some additional interfaces, which consumers could implement to extend BLL’s build-in functionality. But for consumers to be able to implement these interfaces, they will need to have access to BLL’s source code, right? But what if I don’t want consumers to view BLL’s source code?


Interfaces should exist between layers. More specifically, classes should interact with classes from other layers exclusively through interfaces

a) So even DAL’s built-in functionality should be exposed through interfaces? But why? Namely, if we’d use abstract class instead of interfaces, then this class could already implement some utility functions common to all provider classes that inherit from this abstract class?! On the other hand, if DAL uses interfaces instead, then utility functions common to all providers will have to be implemented once for each provider, which could mean a lot of redundant coding?!


b) Anyways, I don’t quite see the benefits ( except when we provide interfaces with which consumers could extend the basic functionality ) in having classes from different layers interacting through interfaces?


For added clarity, instead of overloading methods to work with different parameters, I believe it is better to have one method that accepts a single parameter. This parameter would be an object containing all the data for the method to work with. Some of that data could be required, some could be optional and would influence the effect of the operation.

If I know UI will extensively make use Object Data Source controls, should I still prefer BLL to define a single method (this method having as parameter an object with all the data for the method to work with) instead of method overloads?

cheers mate

+1  A: 

I took the liberty to summarize your post in two main questions. I hope I managed to capture the essence of what you are asking.

Q) What is the relationship between the intefaces exposed by the DAL and the ones exposed by the BLL?

A) The BLL is an outward-facing API, and as such it should implement functionality that is useful to the external consumers of the application and expose it in a way that makes sense to them.
The DAL, on the contrary, is a inward-facing API that exposes functionality to retrieve and persist data in way that hides the details of the storage mechanism being used.

In short, the DAL focuses on how data is being represented and managed internally in the application, while the BLL focuses on exposing data in way that is meaningful to consumers.

Q) How many methods should a public API have, and which ones?

A) The design of an API is strictly related to what it is meant to achieve and by whom it will be used.
In practice, this means that you should know the target audience of your API, and give them only what they need to get the job done.
Since it is impossible to predict all the possible ways an API will be used, it is important to decide which main use cases to support, and work to make them really straightforward in the API. A good principle to keep in mind is what Alan Kay once said:

Simple things should be simple, complex things should be possible.

A few extensibility points can be built into the API to obtain a certain degree of flexibility. A common way to achieve this is to use interfaces to define the behavior of the API. This will allow consumers to replace or extend the pieces of the built-in functionality by providing custom implementations.

To your point, I believe it is a good idea to prefer fewer coarse-grained methods in the BLL to cover all the functionality required by an entire business operation.
On the other side it is perfectly fine to have many smaller data-centric methods in the DAL to work with specific pieces of data.

UPDATE:

About interfaces
Interfaces should exist between layers. More specifically, classes should interact with classes from other layers exclusively through interfaces.
For example, the DAL should expose interfaces for the classes used to access data, like IOrderHeaderTable or IOrderRepository depending on the design pattern being used.
The BLL should expose classes used to execute business operations, like IOrderManagementWorkflow, or ICustomerService.
Note: common functionality inside a layer can still be placed in base classes, since in modern Object-Oriented languages like C#, VB.NET and Java a class can both inherit from a base class and implement one or more interfaces.
Also, external parties who wish to customize the built-in functionality by implementing any of the provided public interfaces can do so without needing access to the source code. Interfaces should however be self-describing and well-documented, in order to make it easy for extenders to understand its semantics.

About the BLL
The BLL should be explicit about the business logic it supports. Therefore it is generally a good idea to have methods that are directly related to business operations.
For added clarity, instead of overloading methods to work with different parameters, I believe it is better to have one method that accepts a single parameter. This parameter would be an object containing all the data for the method to work with. Some of that data could be required, some could be optional and would influence the effect of the operation.
Implementation detail: this kind of BLL API is fully supported by ObjectDataSource control built into ASP.NET Web Forms.

About the API
An API should contain all methods the designer can come up with, within the scope defined by the use cases the API is intended to support.

Enrico Campidoglio
hi - In case you're willing to offer some more help ( which would be very appreciated :) ) - I've edited my post in response to your reply
SourceC
I updated my answer to clarify on the points you brought up. I hope this helps.
Enrico Campidoglio
Hi, I’ve edited my post…I know I’m testing your patience, therefor these are my last question. thanx mate
SourceC
No problem :-) I added a couple of notes in my answer to address your questions.
Enrico Campidoglio
1) “The BLL should be explicit about the business logic it supports. Therefore it is generally a good idea to have methods that are directly related to business operations.”I’m not sure what you’ve meant by “methods that are directly related to business operations”. What kind of methods could BLL also have instead?2) So to sum it up - if target audience is very narrow, then BLL should contain very specific methods.But if we have a wide target audience(and thus BLL needs to support a multitude of UIs), then BLL methods should be more generic?
SourceC
2) Yes, you can say that in general terms
Enrico Campidoglio
thank you for all your help
SourceC