Wednesday 2 July 2008

PlugInSettings - Use this class if you wish to store simple relational data ...

Recently I had to create plugin for editors (EPiServer 5) which would allow them to change some internal settings. It's of course possible to create a new page type for that but it's not always the coolest approach. If the requirement is to provide some custom user interface then you are forced to write your own plugin. It's fine because writing plugins in EPiServer is so straightforward. But in the end you have to store your settings somewhere. It can be a bit problematic unless ... you will use class called PlugInSettings. Here are some information which you can find in documentation:

Class for handling simple plugin settings as a DataSet

and

Use this class if you wish to store simple relational data and web.config or database is not an option.

You will also find nice, simple example how to use this class. It's very cool and everything works like a charm! But what I don't like about it is that you have to manually deal with DataSet. It's quite burdensome. Therefore, I decided to write something to make it more pleasant. Here is how it should work in my opinion:


using Cognifide.PlugInSettingsWrapper;

.
.
.

TestDTO data = (TestDTO) SettingsWrapper.Load(new TestDTO());

data.Value1 = "example string";
data.Value2 = "another one";
data.Value3 = 3;
data.Value4 = 4;

SettingsWrapper.Save(data);


One line to load data and one line to save the data ... coolish?

Check how it works...

First of all you need to have data transfer object, within this, you need to have all properties which you want to store, declared in a following way:

[Persistable]
public string Value1
{
get { return value1; }
set { value1 = value; }
}

As you can see, it's standard property with additional attribute Persistable. Thanks to this attribute you can specify which properties should be saved/loaded. SettingsWrapper will find all relevant properties, it will create DataSet for you and it will store all your data.

Two things before you go for it ...
  • I didn't check how fast it is, I presume that it's not fast, but if you take care of caching those objects then it should be fine,
  • I don't know what will happen if you decide to upgrade your EPiServer to newer version. Will EPiServer migrate all data? I hope so ...

If it still looks interesting and you want to give it a try then download the zip file containing source code and binary version and have fun!

1 comment:

Anonymous said...

Nice little wrapper... I use PlugInSettings with CloudCuckoo but it would be nice to retrofit this for cleaner code!