How would I go about grouping multiple yield statements to a method ?
I have the following code :
public IEnumerable<IPublisher> RegisterEvents()
{
var url= Settings.wcfUrl;
var publisher = PublisherFactory.Create<IWcfPublisher> (url)
publisher.Register(new object(..));
publisher.Register(new object(..));
yield return publisher;
var url = Settings.webpublisherurl;
var publisher = PublisherFactory.Create<IWebPublisher> (url)
publisher.Register(new object(..));
publisher.Register(new object(..));
yield return publisher;
var url = Settings.webpublisherurl2;
var publisher = PublisherFactory.Create<IWebPublisher2> (url)
publisher.Register(new object(..));
publisher.Register(new object(..));
yield return publisher;
. . .
}
The code is getting very large and I wanted to refactor some of the publishers to a static class EventHelper
public IEnumerable<IPublisher> RegisterEvents()
{
var url= Settings.wcfUrl;
var publisher = PublisherFactory.Create<IWcfPublisher> (url)
publisher.Register(new object(..));
publisher.Register(new object(..));
yield return publisher;
return yield EventHelper.CreateWebApiPublisher() //<-- compiler error
. . .
}
CreateWebApiPublisher()
looks like
public static IEnumerable<IPublisher> CreateWebApiPublisher()
{
var url = Settings.webpublisherurl;
var publisher = PublisherFactory.Create<IWebPublisher>(url)
publisher.Register(new object(..));
publisher.Register(new object(..));
yield return publisher;
var url = Settings.webpublisherurl2;
var publisher = PublisherFactory.Create<IWebPublisher2>(url)
publisher.Register(new object(..));
publisher.Register(new object(..));
yield return publisher;
}
I am confused how to use EventHelper.CreateWebApiPublisher()
in RegisterEvents()
I keep getting compiler error :
Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<IPublisher>' to 'IPublisher'...
yield return
can only yield a single value at a time. At the line where you're getting the compiler error, you need to iterate through the results of the method instead, and yield return
them sequentially.
foreach (var p in EventHelper.CreateWebApiPublisher())
yield return p;