MVC3 数据上下文最佳实践

发布于 2024-12-21 22:48:55 字数 769 浏览 0 评论 0原文

这是我在控制器中执行的操作。在此控制器内部创建和处置数据库连接(即 PhotoGalleryContext - 这是与 MySql 数据库的连接)而不是通过在模型的数据访问层中完成此操作来进行抽象,是否被认为是不好的做法。 cs课?

    // GET: /Admin/GetPhoto/id
    public ActionResult GetPhoto(int id)
    {
        PhotoGalleryContext db = new PhotoGalleryContext();

        Models.PhotoGallery.Photo photo = new Models.PhotoGallery.Photo();
        photo = db.Photos.Where(p => p.PhotoId == id).Single();

        string filePath = photo.FileLocation;

        db.Dispose();    

        byte[] byteArray;
        try
        {
            byteArray = System.IO.File.ReadAllBytes(filePath);
            return File(byteArray, "image/jpg");
        }
        catch (Exception)
        {
            //throw;
        }
        return null;
    }

This is an action I have in my controller. Is it considered bad practice to be creating and disposing a database connection (i.e. PhotoGalleryContext - which is a connection to a MySql database) inside of this controller as opposed to having an abstraction by having this action done in a data access layer in a model .cs class?

    // GET: /Admin/GetPhoto/id
    public ActionResult GetPhoto(int id)
    {
        PhotoGalleryContext db = new PhotoGalleryContext();

        Models.PhotoGallery.Photo photo = new Models.PhotoGallery.Photo();
        photo = db.Photos.Where(p => p.PhotoId == id).Single();

        string filePath = photo.FileLocation;

        db.Dispose();    

        byte[] byteArray;
        try
        {
            byteArray = System.IO.File.ReadAllBytes(filePath);
            return File(byteArray, "image/jpg");
        }
        catch (Exception)
        {
            //throw;
        }
        return null;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

硬不硬你别怂 2024-12-28 22:48:55

在做出决定时,您可能会考虑应用程序的大小。我想说分离模型是最佳实践,但对于较小的应用程序,也许您应该将 PhotoGalleryContext 包装在 using 块中:

using(PhotoGalleryContext db = new PhotoGalleryContext())
{

    Models.PhotoGallery.Photo photo = new Models.PhotoGallery.Photo();
    photo = db.Photos.Where(p => p.PhotoId == id).Single();

    string filePath = photo.FileLocation;
    byte[] byteArray;
    try
    {
        byteArray = System.IO.File.ReadAllBytes(filePath);
        return File(byteArray, "image/jpg");
    }
    catch (Exception)
    {
        //throw;
    }
    return null;
}

You would probably consider the size of your application when deciding this. I would say separating the model is a best practice, but for smaller apps, maybe you should just wrap the PhotoGalleryContext in a using block:

using(PhotoGalleryContext db = new PhotoGalleryContext())
{

    Models.PhotoGallery.Photo photo = new Models.PhotoGallery.Photo();
    photo = db.Photos.Where(p => p.PhotoId == id).Single();

    string filePath = photo.FileLocation;
    byte[] byteArray;
    try
    {
        byteArray = System.IO.File.ReadAllBytes(filePath);
        return File(byteArray, "image/jpg");
    }
    catch (Exception)
    {
        //throw;
    }
    return null;
}
錯遇了你 2024-12-28 22:48:55

同意大卫的观点,因为这取决于您的应用程序的大小。

但是,我建议使用依赖注入来为您管理连接。

使用诸如 StructureMap 之类的东西,您可能会得到如下所示的内容:

For<PhotoGalleryContext>()
   .HybridHttpOrThreadLocalScoped
   .Use<PhotoGalleryContext>();

翻译为:

当某些内容请求 PhotoGalleryContext 时,请为其提供一个 HTTP 范围的新 PhotoGalleryContext(在请求开始时创建,在结束)。

这样,StructureMap 将自动为您打开/关闭连接。

然后你的控制器看起来像这样:

private readonly PhotoGalleryContext _db;
public AdminController(PhotoGalleryContext db)
{
   _db = db;
}

在你的操作方法中,_db 将准备好为你服务。

只需几行代码即可在应用程序中节省大量重复的 using 语句。

Agree with David, in that it depends on the size of your application.

However, i would suggest to use dependency injection to manage the connection for you.

Using something like StructureMap, you could have something like this:

For<PhotoGalleryContext>()
   .HybridHttpOrThreadLocalScoped
   .Use<PhotoGalleryContext>();

Which translates to:

When something asks for a PhotoGalleryContext, give it a new PhotoGalleryContext that is HTTP-scoped (created at start of request, disposed at end).

That way, StructureMap will automatically open/close the connection for you.

Then your controller can look like this:

private readonly PhotoGalleryContext _db;
public AdminController(PhotoGalleryContext db)
{
   _db = db;
}

And in your action methods, _db will be ready to go for you.

Few lines of code to save a lot of repetitive using statements across your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文