❔ Is the functionality of my code in the right place
I created a valueobject for my sitemap. I'm wondering if it's in the right place or shouldn't it be in Application instead of Domain?
Use:
namespace Stand.Libraries.Website.Sitemaps.Domain.ValueObjects;
public class SitemapPage : IEquatable<SitemapPage>
{
public SitemapPage(IReadOnlyCollection<Page> pages)
{
Pages = pages?.ToList() ?? throw new ArgumentNullException(nameof(pages));
}
public List<Page> Pages { get; }
public bool Equals(SitemapPage other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Pages.SequenceEqual(other.Pages);
}
public override bool Equals(object obj)
{
return Equals(obj as SitemapPage);
}
public override int GetHashCode()
{
return Pages.GetHashCode();
}
public override string ToString()
{
XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xhtml = "http://www.w3.org/1999/xhtml";
var urlElements = Pages.Select(page =>
{
var pageElements = new List<XElement>
{
new XElement(xmlns + "loc", page.Url)
};
if (page.LastModified.HasValue)
pageElements.Add(new XElement(xmlns + "lastmod", page.LastModified.Value.ToString("yyyy-MM-ddTHH:mm:ss+00:00")));
if (page.ChangeFrequency.HasValue)
pageElements.Add(new XElement(xmlns + "changefreq", page.ChangeFrequency.Value.ToString().ToLowerInvariant()));
if (page.Priority.HasValue)
pageElements.Add(new XElement(xmlns + "priority", page.Priority.Value.ToString(CultureInfo.InvariantCulture)));
if (page.Cultures?.Any() == true)
{
foreach (var culture in page.Cultures)
{
pageElements.Add(new XElement(xhtml + "link",
new XAttribute(XNamespace.Xmlns + "xhtml", xhtml),
new XAttribute("rel", "alternate"),
new XAttribute("hreflang", culture.Name),
new XAttribute("href", culture.Url)));
}
}
return new XElement(xmlns + "url", pageElements);
});
return new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement(xmlns + "urlset", urlElements)
).ToString();
}
}
namespace Stand.Libraries.Website.Sitemaps.Domain.ValueObjects;
public class SitemapPage : IEquatable<SitemapPage>
{
public SitemapPage(IReadOnlyCollection<Page> pages)
{
Pages = pages?.ToList() ?? throw new ArgumentNullException(nameof(pages));
}
public List<Page> Pages { get; }
public bool Equals(SitemapPage other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Pages.SequenceEqual(other.Pages);
}
public override bool Equals(object obj)
{
return Equals(obj as SitemapPage);
}
public override int GetHashCode()
{
return Pages.GetHashCode();
}
public override string ToString()
{
XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xhtml = "http://www.w3.org/1999/xhtml";
var urlElements = Pages.Select(page =>
{
var pageElements = new List<XElement>
{
new XElement(xmlns + "loc", page.Url)
};
if (page.LastModified.HasValue)
pageElements.Add(new XElement(xmlns + "lastmod", page.LastModified.Value.ToString("yyyy-MM-ddTHH:mm:ss+00:00")));
if (page.ChangeFrequency.HasValue)
pageElements.Add(new XElement(xmlns + "changefreq", page.ChangeFrequency.Value.ToString().ToLowerInvariant()));
if (page.Priority.HasValue)
pageElements.Add(new XElement(xmlns + "priority", page.Priority.Value.ToString(CultureInfo.InvariantCulture)));
if (page.Cultures?.Any() == true)
{
foreach (var culture in page.Cultures)
{
pageElements.Add(new XElement(xhtml + "link",
new XAttribute(XNamespace.Xmlns + "xhtml", xhtml),
new XAttribute("rel", "alternate"),
new XAttribute("hreflang", culture.Name),
new XAttribute("href", culture.Url)));
}
}
return new XElement(xmlns + "url", pageElements);
});
return new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement(xmlns + "urlset", urlElements)
).ToString();
}
}
namespace Stand.Libraries.Website.Sitemaps.Application.Services;
public class MapService : IMapService
{
readonly IEnumerable<ISitemap> _sitemaps;
public MapService(IEnumerable<ISitemap> sitemaps)
=> _sitemaps = sitemaps;
public IReadOnlyCollection<Map> GetMaps()
=> _sitemaps
.Select(map => new Map(map.Name))
.ToList();
public SitemapIndex GetSitemapIndex()
{
var maps = _sitemaps
.Select(map => new Map(map.Name))
.ToList();
return new SitemapIndex(maps);
}
}
namespace Stand.Libraries.Website.Sitemaps.Application.Services;
public class MapService : IMapService
{
readonly IEnumerable<ISitemap> _sitemaps;
public MapService(IEnumerable<ISitemap> sitemaps)
=> _sitemaps = sitemaps;
public IReadOnlyCollection<Map> GetMaps()
=> _sitemaps
.Select(map => new Map(map.Name))
.ToList();
public SitemapIndex GetSitemapIndex()
{
var maps = _sitemaps
.Select(map => new Map(map.Name))
.ToList();
return new SitemapIndex(maps);
}
}
2 Replies
I'm wondering if this functionality violates the standards. I don't know if it would be better to create a new class, e.g. Services, which will handle this.
.
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.