In my C#/ASP.NET project I have an object which has certain categories of behavior. Each behavior category is physically dependent on a root object but to make the code read better i want to clearly differentiate the categories. I think it would be interesting to see how my implementation compares to what others might write to solve the same problem.
In the example below i have a class which generates URLs to different places on the website. The website has a storefront which has it's own set of links which should be accessed differently from the urls such as the homepage.
public class WebsiteURLs
{
// One category of urls on the website is the store.
public class StoreURLs
{
private WebsiteURLs _website;
public class StoreURLs(WebsiteURLs website)
{
_website = website;
}
public string BestSellers
{
get { return _website.ResolveURL("~/store/bestSellers.html"); }
}
}
private StoreURLs _store;
public StoreURLs Store // property for generating store urls
{
get
{
if (_store == null ) {
_store = new StoreURLs(this);
}
return _store;
}
}
public string Homepage
{
get { return ResolveURL("~/default.aspx"); }
}
// .. Other Categories Here
protected string ResolveURL(string url)
{
return HttpContext.Current.Response.ApplyAppPathModifier(url);
}
}
Using this code would look similar to the following
WebsiteURLs website;
Console.WriteLine(website.Store.BestSellers);
Robert:
The example itself is only one scenario where I have found myself trying to more explicitly organize the functionality. Have you ever found yourself using a prefix around related methods. String.TrimStart(), String.TrimEnd(), String.Trim() is an example that comes to mind from the C# framework.
My attempt to orgnaize the code above (from a readability standpoint) suffers from the inabilty of nested classes to access outer classes members. There is extra work involved in constructing the inner class because i must pass a reference of the WebsiteURLs instance to the constructor of the StoreURLs class, it is almost a violation of C# coding practices because this closure behavior is not the behavior of nested classes within the language. I'm curious what C# code others would use in situations where there exists lots of related functionality (think tens or hundreds of methods). (Note: The code above is much more fluid to write in Java where nested classes do have access to their outer classes members).