I'm trying to implement the unit-of-work pattern for mutliple storage types like the windows registry, a database or the app.config but I cannot figure out how to do it. All examples I find use Entity Framework which already is a UoW so it doesn't help much. What I'd like to store is a simple Setting object.
From http://martinfowler.com/eaaCatalog/unitOfWork.html I understand that
A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.
So I probably should start with a class that has two settable properties and one that tells me whether it's dirty and needs an update.
class Setting
{
private string _value;
public Setting(string name, string value)
{
Name = name;
_value = value;
}
public string Name { get; set; }
public string Value
{
get { return _value; }
set
{
_value = value;
IsDirty = true;
}
}
public bool IsDirty { get; private set; }
}
then I guess I need a base repository:
abstract class SettingRepository
{
private readonly List<Setting> _settings = new List<Setting>();
public abstract IEnumerable<Setting> Get(Setting setting);
public void Add(IEnumerable<Setting> settings)
{
_settings.AddRange(settings);
}
public abstract int SaveChanges();
}
and a unit-of-work as SettingContext:
abstract class SettingContext
{
protected SettingContext(SettingRepository settings)
{
Settings = settings;
}
public SettingRepository Settings { get; }
}
Now I'm not sure where I should put all the Update/Insert/SaveChanges logic? A database would for example support transaction whereas the windows registry not.
Do I need different repositories for each storage type or do I need different unit-of-work aka contexts for each of them? I'm really confused.
I don't know what exactly I should do next. Maybe I think to much Entity Framework and try to implement it in a too similar way like this:
using(var context = new SettingContext(new RegistryRepository()))
{
context.Settings.Add(new Setting("Foo", "Bar")); // this should be created
context.Settings.Add(otherSetting); // this should be updated
context.SaveChanges();
}