Skip to content

Performing CRUD operations

Eros Stein edited this page Jan 12, 2017 · 9 revisions

Here we'll see how to perform CRUD operations using the library.

Create

We can add records to the database using a few different approaches:

public AccountViewModel Add(AccountViewModel data)
{
    using (var ctx = new AppContext(new AppConfiguration()))
    {
        var account = ctx.Add(new Account
        {
            CreationDate = DateTime.Now,
            Name = data.Name
        });

        ctx.Save(false);

        return account.ToViewModel();
    }
}

The above code can also be written this way:

public AccountViewModel Add(AccountViewModel data)
{
    using (var ctx = new AppContext(new AppConfiguration()))
    {
        var account = ctx.Add<Account>();

        account.CreationDate = DateTime.Now;
        account.Name = data.Name;

        ctx.Save(false);

        return account.ToViewModel();
    }
}

Update

To update a record:

public AccountViewModel Update(AccountViewModel data)
{
    using (var ctx = new AppContext(new AppConfiguration()))
    {
        var account = ctx.GetRepository<Account>().GetById(data.Id);

        account.LastAction = DateTime.Now; // should be done using `IAdditionalStep`. Here only for learning purposes
        account.Name = data.Name;
        account.IsActive = LoggedUser.IsSuperAdmin() || data.IsActive;

        ctx.Save(false);

        return account.ToViewModel();
    }
}

In the future, once the Update method is fixed, we'll also be able to update a record this way:

public AccountViewModel Update(AccountViewModel data)
{
    using (var ctx = new AppContext(new AppConfiguration()))
    {
        var account = ctx.Update(new Account
        {
            AccountId = data.Id,
            LastAction = DateTime.Now, // should be done using `IAdditionalStep`. Here only for learning purposes
            Name = data.Name,
            IsActive = LoggedUser.IsSuperAdmin() || data.IsActive
        }, data.ChangedProperties);

        ctx.Save(false);

        return account.ToViewModel();
    }
}

We inform the Id of the record being update, its new values and lastly the changed properties (data.ChangedProperties). The string[] changedProperties parameter is where we tell which properties we'd like the framework to update.

Get

To retrieve information we can use several approaches, by ID, get all records and filter the records:

By ID:

using (var ctx = new AppContext(new AppConfiguration()))
{
    return ctx.GetRepository<Account>().GetById(id);
}

All records:

using (var ctx = new AppContext(new AppConfiguration()))
{
    return ctx.GetRepository<Account>().GetAll();
}

Filtering records:

using (var ctx = new AppContext(new AppConfiguration()))
{
    return ctx.GetRepository<Account>().GetAll().Where(a => a.Name.Contains(query));
}

Another way of filtering data:

using (var ctx = new AppContext(new AppConfiguration()))
{
    var accounts = ctx.GetRepository<Account>().GetAll();
    
    if(!string.IsNullOrWhiteSpace(query))
        accounts = accounts.Where(a => a.Name.Contains(query));

    return accounts;
}

Delete

We can remove data using basically two methods:

Single

using (var ctx = new AppContext(new AppConfiguration()))
{
    var account = ctx.GetRepository<Account>().GetById(id);
    ctx.Delete(account);
    ctx.Save();
}

Multiple

using (var ctx = new AppContext(new AppConfiguration()))
{
    ctx.DeleteWhere<Account>(a => ids.Contains(a.AccountId));
    ctx.Save();
}

Or:

using (var ctx = new AppContext(new AppConfiguration()))
{
    ctx.DeleteWhere<Account>(a => a.CreationDate < DateTime.Now);
    ctx.Save();
}