views:

71

answers:

2

Hi,

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?

A: 

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

Luis
+5  A: 

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;

          <#}#>
    }
}
Krunal
@cmroanirgo - Checkout my updated code and answer.
Krunal
That's exactly the answer I was looking for. I didn't even begin to think SubSonic might actually have this already!
cmroanirgo