Update

Table Of Contents

Update Method

To update entities in the data store you will need to call the Update 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}.Update

The Update method has four overloads:

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

    This overload returns a boolean. True if succeeded, false if no changes were found before the update.

    Throws and exception if the entity passed in is null.

    Throws and exception if the entity is not found in the database.

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

    Throws an exception if the list passed in is null.

    Throws an exception if any entity in the list is null.

    Throws an exception if any entity in the list is not found in the database.

  3. The third overload takes two entities. Compares the two for changes. Only updates if changes are found.

    • The first parameter is the original(unchanged) entity.

    • The second parameter is the changed entity.

    This overload returns a boolean. True if succeeded, false if no changes were found.

    The entities will be compared and the database will be called only if the entities are different.

    Once the update is finished the entities will be merged so that they are the same.

    Throws an exception if any of the two entities passed in is null.

  4. The fourth overload takes an instance of the Query class.

    This overload returns a List of entities that corresponds to the query passed in.

    This is one of the most powerful features available.

Any property that is generated at the database like Identity will be populated after calling Update.

Update Single

The following sample of C# code shows how to update a Customer entity 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 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 update method and pass the existing customer

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

if (success)
{
    //get the value of auto generated columns

    var identity = customer.Id;
    var updatedDate = customer.UpdatedDate;
}

Update List

To update a list you can just pass the List to the Update method.

The following sample of C# code shows how to update 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 update method and pass the list of customers

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

}

Update Change Tracking

If you are tracking the changes to an entity you can pass the original(unchanged) entity and the changed entity when calling the Update method.

The following sample of C# code shows how to update a Customer in the database using this feature:

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();

// this represents the unchanged entity

// it would most likely come from

// your tracking logic

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

// this represents the changed entity

// for example bound to the UI

var changed = new Customer
{
    Id = 1,
    Name = "John",
    LastName = "Smith" //changed the last name
};

// call the update method and pass the original and the changed

var success = _repo.AppDb.Sales.Customer.Update(original, changed);

if (success)
{
    // both entities are now the same    

}

Update Where

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

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

Five steps are required:

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

  2. Second call .UpdateValues() on the Query instance to make the .Set pipeline available.

  3. Third call .Set() for each property you want to update.

    The Set method takes two parameters

    • Property Enum for example Customer.Status.

    • Value the value to set to the property.

  4. Fourth add your predicate and craft the rest of your query.

  5. Fifth call the Update method on the correct repository and pass the query instance.

The following sample of C# code shows how to "set status equals "active" and LastModified equals "now" to 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

// call UpdateValues() to initialize the Set values pipeline

// call Set() on all the properties that need to be updated

// add the predicate to apply to the query

var query = new Query<CustomerProperty>()
     .BeginUpdateValues()
        .Set(CustomerProperty.Status, "Active")
        .Set(CustomerProperty.LastModified, DateTime.Now)
     .EndUpdateValues()
     .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 result = _repo.AppDb.Sales.Customer.Update(query);

foreach (var customer in result)
{
    // do something with each entity updated

}

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

// call UpdateValues() to initialize the Set values pipeline

// call Set() on all the properties that need to be updated

// add the predicate to apply to the query

var query = new Query<CustomerProperty>()
    .Builder((builder) =>
    {
        builder
        .UpdateValues((builder) =>
        {
            builder
            .Set(CustomerProperty.Status, "Active")
            .Set(CustomerProperty.LastModified, DateTime.Now);
        })
        .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 result = _repo.AppDb.Sales.Customer.Update(query);

foreach (var customer in result)
{
    // do something with each entity updated

}