Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228662
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:43:10+00:00 2026-05-17T16:43:10+00:00

I’m writing some code to do some stuff and I’m pretty sure its not

  • 0

I’m writing some code to do some stuff and I’m pretty sure its not well designed at the moment but I can’t think how I should be refactoring it to make it nicer…

The simple summary is that I have some code which goes through some files in a directory structure and different directories contain different content types. I have a limited number of these content types and currently I have a content type object that I am just creating a lot of to add to a list in a way such as follows:

        contentTypes.Add(new ContentType { ContentName = "2010 Call Report", FolderName = "2010 Reports", RenameFile = false });
        contentTypes.Add(new ContentType { ContentName = "2010 Email Report", FolderName = "2010 Reports", RenameFile = false });
        contentTypes.Add(new ContentType { ContentName = "Above Average Call Recording", FolderName = "Call Recordings", RenameFile = true, HasMultiple = true });
        contentTypes.Add(new ContentType { ContentName = "Below Average Call Recording", FolderName = "Call Recordings", RenameFile = true, HasMultiple = true });

This really doesn’t feel right (11 lines of virtually identical code in total) but I can’t think what else I should be doing.

The ContentType class contents a few properties that can be seen above and a single public method called GetNewFilename. Currently the GetNewFilename method is very simple and shared by all content types. However, I now want to have a few of the ContentType objects to have their own versions of this method…

Things I have considered are:

1) Subclass ContentType to create a class for each content type

This didn’t seem right to me because I’d have 11 classes, all of which never have their information altered and which there is never any point in having more than one of. This didn’t same right for a class (I know about singletons but have heard that if you are using them you may well be doign it wrong).

2) Func property on the ContentType

I figured that I could set a delegate on the ContentType to deal with the GetNewFilename function being different but it still then feels messy generating them in the way described above.

3) Factory Classes

I’ve never had to use Factory classes before (as far as I’m aware) but I know they are used for generating classes. My reading on them suggested that this pattern was used for generating different subtypes rather than just a set of instances of a class.

4) Config file

The data as I have it above could all be put in a config file or database or something and then loaded up and looped through to generate it more nicely (this only just occured to me) but it still wouldn’t solve the problem of the varying getNewFilename method. I am not sure I can put a delegate in a config file easily. 🙂

5) Having all the different getNewFileName methods on one class

I could just have the content class have all the different methods I could want and use some kind of select to then choose the right one. This just seems to be missing the point a bit too though.

So can anybody suggest a good way to do this?

Here is the current signature for my ContentType class (with logic cut away – ask if you think its relevant).

public class ContentType
{
    public string ContentName { get; set; }
    public string FolderName { get; set; }
    public bool RenameFile { get; set; }
    public bool HasMultiple { get; set; }

    public string GetNewFilename(string originalFilename, int fileIndex)
    {...} // This method needs to do diffent things for different contenttypes
}

If you want more details of how this class is used then ask and I can paste it in but I didn’t want to swamp the class in code that I didn’t think was that relevant.

This is only for a one use bit of code (to move files around to appropriate directories to put on a new website and ensure they are named correctly) so best possible code isn’t vital but its going to bug me if I don’t at least know what I should be doing. If the correct way looks like it will take too long (eg rewrite code from scratch) then I won’t bother but at least I’ll know for next time. 🙂

P.S. I realise now also that a constructor or two to set those initial values and making them readonly would be an appropriate change to make to neaten it up but still doesn’t solve all my problems.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:43:10+00:00Added an answer on May 17, 2026 at 4:43 pm

    Have your ContentType class be a base class and make the GetNewFilename method virtual. Derive from ContentType classes for each file-type that may need special handling in the GetNewFilename method and override the virtual implementation. Then just create instances of those inherited classes as needed when the file-types that require special handling are found in your directory search, otherwise just create an instance of ContentType class.

    public class ContentType
    {
        public virtual string GetNewFilename(string originalFilename, int fileIndex)
        {
            // get file name here
        }
    }
    
    public sealed class SpecialContentType : ContentType
    {
        // Inherrits all the properties of ContentType
    
        public override string GetNewFilename(string originalFilename, int fileIndex)
        {
            // get special file name here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.