This project is read-only.

Best Practices - Custom Data

Topics: Customizing Orchard
Nov 30, 2012 at 5:40 PM

I have an object that needs to be persisted in the repository.  I have no problem putting the data in and getting it out via injection of an IRepository<>. 

Here is a sample of the class:

public class ImageSettings {
	public virtual int Id { get; set; }
	public virtual string Caption { get; set; }
	public virtual string FileName { get; set; }
	public virtual int Height { get; set; }
	public virtual long Size { get; set; }
	public virtual string Title { get; set; }
	public virtual int Width { get; set; }

I need to add some helper properties that are computed and some that are readonly.  Obviously, these items do not need to be stored.  Here are a few examples:

public string Dimensions { get { return string.Format("{0}px X {1}px", Width.ToString(), Height.ToString()); } }
public string FriendlySize { get { return Size.ToFriendlySizeString(); } }

I know that I could make ImageSettings inherit from ContentPartRecord and then create another object that is derived from ContentPart<ImageSettings> but, I don't really want to make the images full fledged content parts.  I also know I could create a class that inherits ImageSettings and in the constructor pass in an ImageSettings object to set base properties.  To me, that approach is kind of a hack that is an administrative night mare.

I was wondering what the Orchard Team felt was the best way to approach a situation like this?


Nov 30, 2012 at 5:54 PM

You could create a helper class that adds those as extension methods, or you could have a service class that adds services around that data model. Or you could have something like what a ContentPart<T> does, which is a form of composition: it has a ImageSettings and can act on it.

Nov 30, 2012 at 7:32 PM

I was thinking in that direction.  I wanted to get an idea from you guys so I could make sure I was staying true to proper Orchard development.