Creating filter using Automapping for Deleted flag

Original Post Tommy's Avatar

Tommy

26 Apr, 2010 01:19 PM via web

Hello,

All of my DAO classes inherit from a Base class that contains a boolean called Deleted. Automapping has created this in the Database as a bit but I want to tell Fluent to not load anything that has Deleted set to 1 (True). What is the best way of doing this currently? I've tried a few methods today but constantly run into issues!

Thanks,

Tommy

  1. 2 Posted by Tommy on 04 May, 2010 08:26 AM

    Tommy's Avatar

    Any ideas on this? Surely someone must be using a system for deleting records without actually deleting them within Fluent NHibernate?

  2. 3 Posted by Devon Lazarus on 04 May, 2010 02:52 PM

    Devon Lazarus's Avatar

    I may be misunderstanding your question so I apologize in advance if I am.

    FNH isn't really responsible for querying records, it handles the mapping of entities to tables (although it can as you will see below).

    In effect FNH is doing this job by mapping the isDeleted property to a bit in the database. It is up to your repository to filter on that bit returning entities that have not been deleted or all entities. (Are you sure you never want to see a deleted record? What if someone deletes it by accident?)

    I've seen this done in a couple of ways, the easiest being an optional property on a Repository[entity].Get() method that forms a dynamic query for isDeleted = false/true, or ignores that property all together (which would return all entities regardless of isDeleted flag).

    I think you could use FNH on a one-to-many relationship by implementing IOneToManyConvention and using instance.Where("isDeleted = 0'); but this would still load root entities regardless of isDeleted (which is why we usually implement this in the Repository layer).

    hth,

    -devon

  3. 4 Posted by Tommy on 05 May, 2010 09:34 AM

    Tommy's Avatar

    Thanks a lot for your help so far; what you said has got me most of the way!

    I've put instance.Where("Deleted = 0") on my IOneToManyCollectionInstance and IManyToManyCollectionInstance and I've also put root level protection in by making all of my Load methods, etc, run through LINQ to NHibernate and putting Where(t => t.Deleted == 0) on the getter for the LINQ property.

    There is only one last issue I am having... If say, a User has an IList of EmailAddress and you have one of these EmailAddresses and the User is deleted, you can still see the User as IManyToOneInstance still has default behaviour. What do I have to do to IManyToOneInstance to stop this happening? I can't use the Where that I used on IOneToManyCollectionInstance as it's not a Collection so what is the best approach to sorting this?

    Thanks again,

    Tommy

  4. 5 Posted by Devon Lazarus on 05 May, 2010 02:12 PM

    Devon Lazarus's Avatar

    Well I would make the case that EmailAddress is dependent upon the lifecycle of User so deleting a User should "cascade" to EmailAddress such that EmailAddress is "deleted," too. Then, your repository for EmailAddress will take advantage of the LINQ query you've added.

    Barring that, there isn't a way to put a where clause on a Many-to-One as you can see in the NH documentation here:

    http://nhforge.org/doc/nh/en/index.html#mapping-declaration-manytoone

    That being the case, FNH probably won't be able to help you here. That doesn't preclude you from coming up with a solution, though. I think, if you cascade the delete from User to EmailAddress, your LINQ solution should work for you.

    hth,

    -devon

  5. James Gregory resolved this discussion on 16 May, 2010 05:25 PM.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

05 Jul, 2010 10:29 PM
05 Jul, 2010 12:45 PM
05 Jul, 2010 12:42 PM
05 Jul, 2010 12:17 PM
05 Jul, 2010 12:12 PM

 

03 Jul, 2010 12:26 AM
02 Jul, 2010 02:17 PM
02 Jul, 2010 08:18 AM
02 Jul, 2010 12:20 AM
01 Jul, 2010 10:14 PM