简体   繁体   中英

How to use an interface here

Could someone please help me with any suggestions to improvise this kind of code structure. I have a class BlogArticle and a page where I need to show a list of all blog articles.

public class BlogArticle
{
  public string Title { get; set; }
  public string ImageURL { get; set; }
}

So, I wrote this one to get all the BlogArticles.

public class BlogArticlesSource
{
  public List<BlogArticle> GetBlogArticles() 
  {
     //code to get list of all blog articles
  }
}

It works fine, but there is a new requirement where there are no. of new article types that are not same but similar.

public class NewsArticle
{
  public string Description { get; set; }
  public string ImageURL { get; set; }
}

public class VlogArticle
{
  public string Title { get; set; }
  public string TargetURL { get; set; }
}

But for any article type, ultimately, I need to get the respective list and show it on page.

I can inherit from a base Article class, but is there something that can be done about the "ArticleSource" classes. I do not want to go on creating "VlogArticleSource", "NewsArticleSource" and so on.....

Can I have only one such "SourceClass" and use it in all logic.

If there is any other way to improve or simplify this structure, please advise.

Why not have a interface with those properties like

public interface IArticle
{
  public string Title { get; set; }
  public string URL { get; set; }
}

Have the concrete type implement the interface then

public class BlogArticle : IArticle
{ }

public class NewsArticle : IArticle
{ }

public class VlogArticle : IArticle
{ }

Have your list return a IArticle type rather like

public class BlogArticlesSource
{
  public List<IArticle> GetBlogArticles() 
  {
     //code to get list of all blog articles
  }
}

From the way I read that question, you need to derive from a base class. I think you're confusing the difference between a base class and interface.

public class ArticlesSource
{
    public List<NewsArticle> GetNewsArticles() { ... }
    public List<VlogArticle> GetVlogArticles() { ... }
    public List<BlogArticle> GetBlogArticles() { ... }
}

public class NewsArticle : ArticlesSource
{
  public string Description { get; set; }
  public string ImageURL { get; set; }
}

public class VlogArticle : ArticlesSource
{
  public string Title { get; set; }
  public string TargetURL { get; set; }
}

Read more about inheritance here in this article

Derive from this base class and you can access the methods. So, you could do...

var vlog = new VlogArticle();
var vlogArticles = vlog.GetVlogArticles();

var news = new NewsArticle();
var newsArticles = news.GetNewsArticles();

Alternatively, if you want to get fancy, you could just have one GetArticleSource<T> using generics .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM