views:

90

answers:

3

Hi,

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  A: 

Another pattern to consider might be an interface or abstract class. Yes, you'd have 11 classes, but that's not necessarily a bad thing. It would keep your concerns cleanly separated while still offering a common framework.

If GetFileName works the same way in several cases, you could implement an abstract class with a virtual GetFileName method. This would limit the amount of new code you have to write, only overriding when necessary:

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

        public virtual string GetFileName()
        { 
            //Base GetFileName implementation
            return "filename";
        }
    }
Dave Swersky
+2  A: 

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
    }
}
Joel B
+1  A: 

For #4 you could use an IoC container like Unity or StructureBuilder. Then provide a class for the 2nd part:

public interface INewFilenameService {
  string FileName {get;set;}
}

public class ContentType {
    private INewFilenameService newFilenameService;

    public ContentType(INewFilenameService service) {
        this.newFilenameService = service;
    }

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

    public string GetNewFilename() {
      return service.Filename;
    }
}

then you could instantiate your content type list either in config or at runtime.

Bryce Fischer