简体   繁体   中英

How to instantiate a class and call a method on it?

How do I call on my methods from the repository? I am trying to save my returned item from the rssReader method with my method from the repository

public class Repository<T> : IRepository<T> where T : class, IEntity
{
    protected XmlHandler<T> YoloHandler = new XmlHandler<T>();

    public void Save(List<T> value, string path)
    {
        try
        {
            YoloHandler.SaveXml(value, path);
        }
        catch (Exception ex)
        {
            throw new NotImplementedException();
        }
    }
}

This method I need to call so I can use it with my method that returns the syndication feed:

public class RssReader : IReader
{
    public List<FeedItem> Read(string url)
    {
        List<FeedItem> rssFlow = new List<FeedItem>();


        XmlReader reader = XmlReader.Create(url);
        SyndicationFeed feed = SyndicationFeed.Load(reader);
        reader.Close();

        var feedItem = new FeedItem();
        foreach (SyndicationItem item in feed.Items)
        {                
            feedItem.Title = item.Title.Text;

            foreach (var link in feed.Links)
            {
                feedItem.Link = link.Uri.ToString();
            }

            try
            {
                rssFlow.Add(feedItem);
            }
            catch (Exception)
            {
            }

        }
        return rssFlow;
    }
}

How to use the save method from my repository and save the returned list with that method?

Well, it's quite simple:

   var repo = new Repository<FeedItem>();
    repo.Save(rssFlow, "path to save");

I think you're just looking for this code to read the feed:

var reader = new RssReader();
var feedItems = reader.Read("uri");

And then save the results like this:

var repository = new Repository<FeedItem>();
repository.Save(feedItems, "path");

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