Delete

Table Of Contents

Delete Method

To delete entities from the data store you will need to call the Delete method on the entity's repository.

The Sharp Factory App generates one Repository per entity.

You will need an instance of the RepositoryContainer class to access your repositories.

The naming convention is RepositoryContainer.{Model Name}.{Sql Schema Name}.{Entity Name}.Delete

The Delete method has three overloads:

  1. The first one takes an entity to be deleted.

    This overload returns a boolean. True if succeeded, false if the entity does not exist.

  2. The second overload takes a list of entities and returns void.

  3. The third overload takes an instance of the Query class.

    This overload returns a int that represents the number of deleted records.

    This is one of the most powerful features available.

Delete Single

The following sample of C# code shows how to delete a Customer entity from the database:

C#
// declare an instance of the Repository Container

// remember this can be a singleton if you

// declare it early on and reuse it for the

// lifetime of your application

var repo = new RepositoryContainer();

// declare an instance of the Customer entity

// and populate it with the values

// this is for demonstration purposes only

// in real life this entity would come from the database

// or perhaps your system had already loaded it beforehand

var customer = new Customer
{
    Id = 1,
    Name = "John",
    LastName = "Doe"
};

// call the delete method and pass the existing customer

var success = _repo.AppDb.Sales.Customer.Delete(customer);

if (success)
{
    // it was deleted now do something about it

}

Delete List

To delete a list you can just pass the List to the Delete method.

The following sample of C# code shows how to delete a list of customer in the database:

C#
// declare an instance of the Repository Container

// remember this can be a singleton if you

// declare it early on and reuse it for the

// lifetime of your application

var repo = new RepositoryContainer();

// declare several instances of the Customer entity

// and populate them with the values

// this is only for demonstration purposes

// in real life the list would come from a query

// or perhaps from a collection of entities that

// your system had already loaded beforehand

var customer1 = new Customer
{
    Id = 1,
    Name = "John",
    LastName = "Doe"
};

var customer2 = new Customer
{
    Id = 2,
    Name = "Jane",
    LastName = "Smith"
};

// declare a list and add the customers

var customers = new List<Customer>
{
    customer1,
    customer2
};

try
{
    // call the delete method and pass the list of customers

    _repo.AppDb.Sales.Customer.Delete(customers);
}
catch (Exception ex)
{
    // there was an error

}

Delete Where

This is perhaps one of the most powerful features of The Sharp Factory Framework.

It gives you the ability to delete all entities within a given query. This is a feature lacking in some of the most popular ORMs in the market.

Three steps are required:

  1. First you need an instance of the Query class.

  2. Second add your predicate and craft your query.

  3. Third call the Delete method in the correct repository and pass the query instance.

The following sample of C# code shows how to delete "all Customers where Balance > 0 or (CreatedDate = today and (status = "new" or status = "inactive"))":

Fluent Syntax

C#
// declare an instance of the Repository Container

// remember this can be a singleton if you

// declare it early on and reuse it for the

// lifetime of your application

var repo = new RepositoryContainer();

// declare an instance of Query and 

// specify the entity's PropertyEnum

// add the predicate to apply to the query

var query = new Query<CustomerProperty>()     
     .BeginPredicate()
        .Where(CustomerProperty.Balance).GreaterThan(0)
        .Or((exp) =>
        {
            exp
            .Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
            .Where((exp2) =>
            {
                exp2
                .Where(CustomerProperty.Status).Equals("new")
                .Or(CustomerProperty.Status).Equals("inactive");
            });
        })
     .EndPredicate();

// call the update method and pass the query instance

var numberOfDeletedRecords = _repo.AppDb.Sales.Customer.Delete(query);

Builder Syntax

C#
// declare an instance of the Repository Container

// remember this can be a singleton if you

// declare it early on and reuse it for the

// lifetime of your application

var repo = new RepositoryContainer();

// declare an instance of Query and 

// specify the entity's PropertyEnum

// add the predicate to apply to the query

var query = new Query<CustomerProperty>()
    .Builder((builder) =>
    {
        builder        
        .Predicate((builder) =>
        {
            builder
            .Where(CustomerProperty.Balance).GreaterThan(0)
            .Or((exp) =>
            {
                exp
                .Where(CustomerProperty.CreatedDate).EqualsDate(DateTime.Now)
                .Where((exp2) =>
                {
                    exp2
                    .Where(CustomerProperty.Status).Equals("new")
                    .Or(CustomerProperty.Status).Equals("inactive");
                });
            });
        });
    });

// call the update method and pass the query instance

var numberOfDeletedRecords = _repo.AppDb.Sales.Customer.Delete(query);