SubSonic 生成代码并始终过滤记录

发布于 2024-08-30 12:41:32 字数 901 浏览 4 评论 0原文

我有一个名为“用户”的表,其中有一列名为“已删除”,这是一个布尔值,指示用户已从系统中“删除”(当然,没有实际删除它)。

我还有很多表对 Users.user_id 列具有 FK。 Subsonic 以类似的方式(非常好地)生成所有外键的代码:

    public IQueryable<person> user
    {
        get
        {
              var repo=user.GetRepo();
              return from items in repo.GetAll()
                   where items.user_id == _user_id
                   select items;
        }
    }

虽然这很好,但是有没有一种方法可以以这样的方式生成代码来始终过滤掉“已删除”用户?

在办公室里,我们能想到的唯一建议是使用部分类并扩展它。当有很多很多类使用 User 表时,这显然是一种痛苦,更不用说很容易无意中使用错误的属性(本例中是 User 与 ActiveUser):

    public IQueryable<User> ActiveUser
    {
        get
        {
              var repo=User.GetRepo();
              return from items in repo.GetAll()
                   where items.user_id == _user_id and items.deleted == 0
                   select items;
        }
    }

有什么想法吗?

I have a table called "Users" that has a column called "deleted", a boolean indicating that the user is "Deleted" from the system (without actually deleting it, of course).

I also have a lot of tables that have a FK to the Users.user_id column. Subsonic generates (very nicely) the code for all the foreign keys in a similar manner:

    public IQueryable<person> user
    {
        get
        {
              var repo=user.GetRepo();
              return from items in repo.GetAll()
                   where items.user_id == _user_id
                   select items;
        }
    }

Whilst this is good and all, is there a way to generate the code in such a way to always filter out the "Deleted" users too?

In the office here, the only suggestion we can think of is to use a partial class and extend it. This is obviously a pain when there are lots and lots of classes using the User table, not to mention the fact that it's easy to inadvertently use the wrong property (User vs ActiveUser in this example):

    public IQueryable<User> ActiveUser
    {
        get
        {
              var repo=User.GetRepo();
              return from items in repo.GetAll()
                   where items.user_id == _user_id and items.deleted == 0
                   select items;
        }
    }

Any ideas?

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

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

发布评论

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

评论(2

jJeQQOZ5 2024-09-06 12:41:32

您需要更改 ActiveRecord.tt 文件中的以下代码并重新生成代码:

以下代码位于:#region 'Foreign Keys'

更新: 我已更新您的评论代码,用于检查删除列是否可用,然后仅应用删除条件。

HasLogicalDelete() - 如果表有“deleted”或“isdeleted”列,此函数将返回 true,否则返回 false。

public IQueryable<<#=fk.OtherClass #>> <#=propName #>
{
    get
    {

          var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();

          <#if(tbl.HasLogicalDelete()){#>

          return from items in repo.GetAll()
               where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#> && items.deleted == 0
               select items;

          <#}else{#>

          return from items in repo.GetAll()
               where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
               select items;

          <#}#>
    }
}

You need to change following code in your ActiveRecord.tt file and regenerate your code:

Following is code is located under : #region ' Foreign Keys '

Update: I've updated code for your comment for checking if delete column is available then only apply delete condition.

HasLogicalDelete() - This function will return true if table has "deleted" or "isdeleted" column, false otherwise.

public IQueryable<<#=fk.OtherClass #>> <#=propName #>
{
    get
    {

          var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();

          <#if(tbl.HasLogicalDelete()){#>

          return from items in repo.GetAll()
               where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#> && items.deleted == 0
               select items;

          <#}else{#>

          return from items in repo.GetAll()
               where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
               select items;

          <#}#>
    }
}
一直在等你来 2024-09-06 12:41:32

你用的是Subsonic3吗?如果是这样,那么您实际上可以编辑模板来修改数据访问层类的生成方式。

Are you using Subsonic3? If so then you can actually edit the templates to modify the way the Data Access Layer classes are generated.

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