I have a fairly simple system, and for the purposes of this question there are essentially three parts: models, repositories, application code.
It is based on models. Let me use a simple far-fetched example:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
The same project uses the common repository interface. In the simplest case:
public interface IRepository<T>
{
T Save(T model);
}
Implementations of this interface are in a separate project and are introduced using StructureMap. For simplicity:
public class PersonRepository : IRepository<Person>
{
public Person Save(Person model)
{
throw new NotImplementedException("I got to the save method!");
}
}
So, in the application code, if I wanted to save the model, I would do the following:
var rep = IoCFactory.Current.Container.GetInstance<IRepository<Person>>();
myPerson = rep.Save(myPerson);
, , . , Save() , . , :
myPerson.Save();
, . , , . ISaveableModel<T> , Save() :
public static void Save<T>(this ISaveableModel<T> model)
{
var rep = IoCFactory.Current.Container.GetInstance<IRepository<T>>();
model = rep.Save(model);
}
, rep.Save(model) . , , . BaseModel<T>, :
public class BaseModel<T>
{
public void Save()
{
this = IoCFactory.Current.Container.GetInstance<IRepository<T>>().Save(this);
}
}
. , ? , , - , .