3

我想使用存储库模式(我知道我可以使用工作单元模式,但我想在这里使用存储库模式)。因此,在每个存储库类中,我都有对特定表的查询,例如:

public class NotesRepository : INotesRepository, IDisposable
{
    private DatabaseContext context;

    public NotesRepository(DatabaseContext context)
    {
        this.context = context;
    }

    public IQueryable<Notes> GetAllNotes()
    {
        return (from x in context.Notes
                select x);
    }
}

还有另一个存储库示例:

public class CommentsRepository : ICommentsRepository, IDisposable
{
    private DatabaseContext context;

    public CommentsRepository(DatabaseContext context)
    {
        this.context = context;
    }

    public IQueryable<Comments> GetAllComments()
    {
        return (from x in context.Comments
                select x);
    }
}

现在我想在一个控制器中使用这两个存储库。因此,使用存储库模式,我在控制器构造函数中初始化数据库连接并将其传递给每个存储库,对吗?例如:

public class HomeController : Controller
{
    private INotesRepository NotesRepository;
    private ICommentsRepository CommentsRepository;

    public HomeController()
    {
        DatabaseContext db = new DatabaseContext();

        this.NotesRepository = new NotesRepository(db);
        this.CommentsRepository = new CommentsRepository(db);
    }

    // ........
}
4

3 回答 3

1

我认为您应该至少更进一步,让控制器将存储库作为构造函数中的依赖项。这意味着您将在自定义ControllerFactory(在 ASP.NET MVC 和 MVC2 中使用)或 DependencyResolver(在 ASP.NET MVC3 IIRC 中引入;如果迁移到 IoC 容器则非常适合)创建 Controller 和 Repository 实例。DatabaseContext 通常HttpRequest在 Web 应用程序中创建,以便在请求期间保持相同的 UnitOfWork。如果您使用的是 IoC 容器,这很容易完成,因为所有主要容器都内置了对此的支持。如果您手动执行此操作,则必须连接一些机制来创建和处理DatabaseContextinApplication.BeginRequestEndRequest事件。

为简单起见,我将给您一个 ASP.NET MVC 示例,不使用容器并且DatabaseContext每个Controller. 当 Controller 被“释放”时,DatabaseContext 也不会被释放。

public class HomeController : Controller
{
    private INotesRepository notesRepository;
    private ICommentsRepository commentsRepository;

    public HomeController(INotesRepository notesRepostory, 
        ICommentsRepository commentsRepository)
    {
        this.notesRepository = notesRepository;
        this.commentsRepository = commentsRepository;
    }
}

public class MyControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(
        RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
            return base.GetControllerInstance(requestContext, controllerType);

        DatabaseContext db = new DatabaseContext();
        //Here you should have logic to determine which 
        //type of controller to create
        return new HomeController(new NotesRepository(db), 
            new CommentsRepository(db));
    }
}

在 Global.asax Application.Start 事件处理程序中:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

如果你走容器路线的话,有很多使用 MVC 和你最喜欢的 Container 品牌的 IoC 的例子。

使用控制反转的优点是应用程序变得松散耦合并且更易于更改和维护。

于 2012-12-08T16:10:20.390 回答
0

当您使用存储库时,您希望将数据访问代码从业务逻辑中抽象出来,因此处理控制器内部的连接不是很干净。也许存储库不是您在这里需要的...

于 2012-12-08T14:08:10.673 回答
-1

这里的情况非常适合使用依赖注入(DatabaseContextNotesRepository和的依赖CommentsRepository)。

像Ninject这样的依赖注入容器很容易解决这个问题。

于 2012-12-08T14:04:37.127 回答