SubSonic 3简单存储库问题

时间:2010-12-25 20:22:50

标签: c# asp.net orm subsonic3 subsonic-simplerepository

我使用Subsonic 3的简单存储库模式来存储和获取数据库中的值。我想知道是否应该使用Singleton模式创建SimpleRepository,或者应该在需要时创建一个模式。就像我有这样的Person类:

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
        .....
    }
}

或者像这样访问回购

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();
        .....
    }
}

1 个答案:

答案 0 :(得分:1)

我使用单例类。当您拥有集中式数据存储时,这似乎是正确的。我允许您在一个地方管理存储库的类型。还有一个优点是它可以更容易地从重新定位类型切换。

public static class Repository
{
    static SimpleRepository repo;

    public static IRepository GetRepository()
    {
        if (repo == null)
        {
            lock (repo)
            {
                repo = new SimpleRepository("NamedConnectionString",
                    SimpleRepositoryOptions.RunMigrations);
            }
        }

        return repo;
    }
}

聚苯乙烯。我还构建了一个基本记录类来执行Save()和管理外部关系。