Last post Mar 02, 2009 05:37 PM by avmood
Feb 27, 2009 03:57 PM|avmood|LINK
Hi, I just started learning the BLL and DAL and got a question about the BLL. In the Article class and Category class, I see the following
Article - public static List<Article> GetArticles(bool publishedOnly, int categoryID)
Category- public List<Article> AllArticles
Why do we need public List<Article>AllArticles in the Category class, can we just retrieve all the articles using the List<article>GetArticles in the article class?
Feb 28, 2009 12:55 AM|chandu2chandu1|LINK
It's very simple. This is nothing but the feature of the generics released in asp.net 2.0. However your GetArticles will return the List of the Article class. Instead you can say this as the you will return a strongly
typed Articles while you make a call to GetArticles method. If you are even much enthusiastic about this, just google the Generics in C#. Anyway, your subject "Question about Categories & Articles" will make your question less readable. Always have a better
subject line instead that will make the attentions of the people at a single shot.
Feb 28, 2009 01:00 AM|haitham.elrefaie|LINK
the main deference is the implementation inside each function, the first one returns the published articles inside that category, the second one returns all the articles published/unpublished inside the category, I know it's a little bit cofusing, but just
take the concept and deal with it as per your needs, i've used this kit and modified to much in it to fit my needs..
Mar 02, 2009 12:04 PM|avmood|LINK
Hi chandu2chandu1, I think you misundertand my question. I understand what generic types are for. My question is why we need the method GetAllArticle in the Categories class, since we there is already another method call GetArticle(int category) in the class
Mar 02, 2009 12:08 PM|avmood|LINK
Actually what I want to know is what is the benefit of implementing an GetArticle in the Article class and another one in the Category class, isn't it duplicate?
Mar 02, 2009 01:44 PM|Lee Dumond|LINK
Actually, the short answer is: simple flexibility and convenience.
The AllArticles property of Category is merely a read-only property which actually uses the Article.GetArticles method to populate its value. That way, if you need to get a list of all articles that belong to a certain category, you can do this:
List<Article> articlesInMyCategory = myCategory.AllArticles;
instead of this:
List<Article> articlesInMyCategory = Article.GetArticles(myCategory.ID, 0, BizObject.MAXROWS);
If you look closely, you will notice that the AllArticles property is never actually used throughout the entire application, but it's there anyway. Is this duplicative? Yes, it is. But, it also extends the functionality of the API by giving you two different
ways of obtaining the same result, which is a common programming practice.
You will note that the Author has this kind of "duplication" in several other places. For example, in the Articles class:
public bool Delete()
public static bool DeleteArticle(int id)
Why have both an instance method and a static method that do exactly the same thing? Again, it offers an option. That's why it is a common programming practice to provide instance methods that mirror the static methods in a class. You will notice
in every business object in this application, there are instance methods that reference corresponding static methods, even though most of the instance methods are never used.
Whether you feel it is useful to offer this kind of flexibility in an API is basically a programming design choice. Many do it, others don't.
Hope that makes sense.
Mar 02, 2009 05:37 PM|avmood|LINK
Thanks Lee, now I understand it completely.