I have a class called ContentLoader. This class has one method LoadContent<T>(T model, int?loadLogicDecider1, int? loadLogicDecider2, etc). It runs through the input class looking for a properties decorated with a ContentItem Attribute. It then goes to the database and loads up the appropriate content. There is some logic on deciding which content to load and generally depends on what is passed in from the loadLogicDeciderX parameters. 1 or 2 of those loadLogicDecider parameters could be sent along with the model but generally all of those parameters are not the responsibility of the model being sent in. The class is about 200 lines long so I feel like splitting it out into it's own class was worthwhile but now it seems like I can do even more with this.

public class ContentLoader{
 LoadContent<T>(T model, int?loadLogicDecider1, int? loadLogicDecider2, etc);

An example of a client using the code

private contentLoader {get;set;}

public DisplayModel SubMethodOfLoadingPage(){
   DisplayModel model = new DisplayModel();
   return model;

I can't help but shake the feeling that I can do better with this class. Should it not even be a class? Is there a easy pattern here that I'm missing? Any advice here would be appreciated.

I have thought about having my classes inherit from an abstract class that contains this functionality but the dependency on the database and the external parameters make it a little difficult to encapsulate. Wiring up the method call with something like action filter would be difficult because sometimes the content can depend on other objects inside a class and their data needs to be loaded so you have a temporal coupling...something I would also like to eliminate. Below is an example that show the process of loading up data and formatting content with data that is inserted into a content item.

public class ExampleA{
   int commonDataItemToFormatWithOtherContent {get;set;} 
   ExampleB exampleB {get;set;}

public class ExampleB{
  string contentItemThatNeedsCommonDataPiece {get;set;}

public ExampleA SubMethodOfLoadingPage(){
   ExampleA model = new ExampleA();
   model.exampleB = new ExampleB();
   //Load model A's data
   //Load model B's data
   //Then load content
   return model;
  • 1
    This is rather open ended. The answers to your questions could all be "yes" or "no" depending on the patterns you are looking for. I think the biggest question for you to ask is whether you wish to use reflection (i.e. the ContentItem attribute) or not. If you are using reflection, it is not uncommon for all of the algorithm to be outside of the class, like you are doing with ContentLoader. If you wanted to approach this without reflection, some base classes and/or interfaces may be called for.
    – Cort Ammon
    Commented Aug 17, 2022 at 15:21
  • You may want to look at the Visitor Pattern. That's not to say you actually want to use the pattern, but I think it's a good example of prior art of how this sort of division between data structures and algorithms has been managed in situations where reflection cannot be used (or does not exist in the language). I think it's a good pattern to show you the costs of trying to provide this sort of flexibility, so that you can properly weigh the different solutions available to you.
    – Cort Ammon
    Commented Aug 17, 2022 at 15:23
  • Thank you even the insight that reflection is naturally driving this to an external class is helpful. The question is fairly open-ended and I think much of that derives from the fact that I can only "feel" something is wrong rather than being able to exactly put my finger on what's causing it. Commented Aug 17, 2022 at 15:59


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.