亚音速数据库对象的重用

发布于 2024-08-22 22:18:27 字数 688 浏览 4 评论 0原文

另一个新手 SubSonic/ActiveRecord 问题。假设我想插入几条记录,目前我正在这样做:

using (var scope = new System.Transactions.TransactionScope())
{
    // Insert company
    company c = new company();
    c.name = "ACME";
    c.Save();

    // Insert some options
    company_option o = new company_option();
    o.name = "ColorScheme";
    o.value = "Red";
    o.company_id = c.company_id;
    o.Save();
    o = new company_option();
    o.name = "PreferredMode";
    o.value = "Fast";
    o.company_id = c.company_id;
    o.Save();

    scope.Complete();
}

但是,单步执行此代码,每个 company/company_option 构造函数都会创建一个新的 myappDB 对象,这似乎很浪费。

这是推荐的方法还是我应该尝试重用单个数据库对象 - 如果是这样,最简单的方法是什么?

Yet another newbie SubSonic/ActiveRecord question. Suppose I want to insert a couple of records, currently I'm doing this:

using (var scope = new System.Transactions.TransactionScope())
{
    // Insert company
    company c = new company();
    c.name = "ACME";
    c.Save();

    // Insert some options
    company_option o = new company_option();
    o.name = "ColorScheme";
    o.value = "Red";
    o.company_id = c.company_id;
    o.Save();
    o = new company_option();
    o.name = "PreferredMode";
    o.value = "Fast";
    o.company_id = c.company_id;
    o.Save();

    scope.Complete();
}

Stepping through this code however, each of the company/company_option constructors go off and create a new myappDB object which just seems wasteful.

Is this the recommended approach or should I be trying to re-use a single DB object - and if so, what's the easiest way to do this?

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

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

发布评论

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

评论(2

携君以终年 2024-08-29 22:18:27

我相信如果您愿意,您可以通过将其 IsNew 属性设置为 true 来使用同一对象,然后更改其数据属性,再次保存,重复。很容易。

不过,我不太确定你是否应该打扰。这取决于这些构造函数对你的伤害有多严重。

I believe you can use the same object if you want to by setting its IsNew property to true, then change its data properties, save it again, repeat. Easy enough.

I 'm not so sure that you should bother, though. It depends on how bad those constructors are hurting you.

衣神在巴黎 2024-08-29 22:18:27

在我看来,将多个对象分配给一个变量从来都不是一个好主意,但这是有争议的。我会这样做:

// Insert some options
company_option o1 = new company_option();
o1.name = "ColorScheme";
o1.value = "Red";
o1.company_id = c.company_id;
o1.Save();

company_option o2 = new company_option();
o2.name = "PreferredMode";
o2.value = "Fast";
o2.company_id = c.company_id;
o2.Save();

如果您担心性能,那不应该是问题,除非您想一次插入或更新许多对象。同样,在这种情况下,插入数据所花费的时间比创建对象所花费的时间要长。

如果您担心性能,可以使用插入查询完全跳过对象创建和保存部分:

http:// /www.subsonicproject.com/docs/Linq_Inserts

   db.Insert.Into<company_option>(
     x => x.name, 
     x => x.value,
     x => x.company_id)
    .Values(
        "ColorScheme",
        "Red",
        c.company_id
    ).Execute();

In my eyes assigning multiple objects to a single var is never a good idea but thats arguable. I would do this:

// Insert some options
company_option o1 = new company_option();
o1.name = "ColorScheme";
o1.value = "Red";
o1.company_id = c.company_id;
o1.Save();

company_option o2 = new company_option();
o2.name = "PreferredMode";
o2.value = "Fast";
o2.company_id = c.company_id;
o2.Save();

I you are worried about performance, that shouldn't be a issue unless you want to insert or update many objects at once. And again, in this case the time used for inserting the data takes longer than for the object creation.

If you are worried about performance you can skip the object creation and saving part completly by using a Insert query:

http://www.subsonicproject.com/docs/Linq_Inserts

   db.Insert.Into<company_option>(
     x => x.name, 
     x => x.value,
     x => x.company_id)
    .Values(
        "ColorScheme",
        "Red",
        c.company_id
    ).Execute();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文