SubSonic 3 domanda semplice repository
-
13-10-2019 - |
Domanda
Sto usando modello semplice repository di Subsonic 3 per memorizzare e ottenere i valori dal database. Vorrei sapere se devo usare Singleton Patten per creare SimpleRepository
o dovrei creare uno ogni volta che è necessario. Come se ho classe Persona in questo modo:
public class Person
{
public void Save()
{
var repo=new SimpleRepository("constr"); //CREATE REPO HERE
repo.Add<Person>(this);
}
public void Load(int id)
{
var repo=new SimpleRepository("constr");//CREATE REPO HER
.....
}
}
o accedere pronti contro termine come questo
public class Person
{
public void Save()
{
var repo=RepoHelper.GetRepository();//GET FROM SINGLETON OBJECT
repo.Add<Person>(this);
}
public void Load(int id)
{
var repo=RepoHelper.GetRepository();
.....
}
}
Soluzione
I use a singleton class for it. It seems to be the right thing when you have a centralized data store. I allows you to manage the type of repository in one place. Is also has the advantage that it makes it easier to switch from reposition type.
public static class Repository
{
static SimpleRepository repo;
public static IRepository GetRepository()
{
if (repo == null)
{
lock (repo)
{
repo = new SimpleRepository("NamedConnectionString",
SimpleRepositoryOptions.RunMigrations);
}
}
return repo;
}
}
Ps. I also build a base record class to do a Save() and to manage foreign relations.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow