Logical Delete

4 posts, 0 answers
  1. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 12 Dec 2012 Link to this post

    My all table have one column named "IS_DELETED" to indicate whether the record is deleted. My query is whether there is a easy way to filter those deleted row without writing code to handle it for every table?
  2. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 17 Dec 2012 Link to this post

    Hi John,

    In order to filter the deleted records you could add a Where clause before getting them from the database. The GetAll<T> method of the OpenAccessContext is returning IQueryable<T> which allows you easily to filter the requested data. 

    Here is a sample code snipped demonstrating that approach:
    using (EntitiesModel db = new EntitiesModel())
    {
        // takes only the entities that are not marked as deleted
        IQueryable<SomeEntity> carsWithoutABS =
                                 db.GetAll<SomeEntity>().Where(e => !e.IsDeleted);
        // where IsDeleted is mapped to your IS_DELETED column
    }

    Also you could take a look at our ADO API with which you could directly execute sql statement and materialize the query result to a given type.

    I hope this is applicable for you.

    Kind regards,
    Dimitar Tachev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
  4. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 17 Dec 2012 Link to this post

    Hi,

    Yes, your solution can meet my requirement but it's not very convenient if all methods all need to write code to filter IS_DELETED column. Whether there a base method which is called for all search method in that case I only write the filter code in base class?
  5. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 20 Dec 2012 Link to this post

    Hi John,

    In order to write the filter only one time I suggest you to add custom EndPoints for the entities that have IS_DELETED column in the database by extending your Domain Model with a partial class.

    For your convenience I prepared the following two-step tutorial demonstrating that approach.

    1. Create a partial class.



    2. Implement new EndPoints.
    using System.Linq;
        
    namespace YoudDomainModelNamespace // e.g. OpenAccessModel
    {
        public partial class YourDomainModelName // e.g. EntitiesModel
        {
            // e.g. endpoint for Employee with name NotDeletedEmployees
            public IQueryable<EntityName> NotDeletedEntityName     
            {
                get
                {
                    return this.GetAll<EntityName>().Where(e => !e.IsDeleted);
                }
            }
            ... // the same endpoints for all of the entities you need
        }
    }
    Please bear in mind that the partial class should be in the same assembly and in the same namespace as your Domain Model.

    After that you will be able to choose whether to get all of the records from the database or only the filtered one without manually writing the filter each time – look at the example below:
    using (EntitiesModel context = new EntitiesModel())
    {
        // get all employees from the database
        IEnumerable<Employee> allEmployees = context.Employees;
     
        // get only the employees that are not marked as deleted
        IEnumerable<Employee> filteredEmployees = context.FilteredEmployees;
    }

    I hope this is applicable for you.  

    All the best,
    Dimitar Tachev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Back to Top