Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

famoser/SqliteWrapper

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.

SqliteWrapper

a portable sqlite wrapper

What is this library

This library serves as Wrapper between your database entities and the models used in your view.

Preconditions

prepare sqlite

  • install "Famoser.FrameworkEssentials", "SQLite.Net.Async-PCL" and "SQLite.Net-PCL" from nuget in the Presentation project (the one which is deployed)
  • you may need addtionally an extension in your presentation project for sqlite to work, in UWP this extension can be installed within Visual Studio -> Tools -> Extensions -> install "Sqlite for Universal Windows Platform". Then do a right click in your Presentation project on References, Select "Add Reference", select "Universal Windows" -> Extensions -> mark Sqlite

prepare your project

  • have all your models implement the ISqliteModel interface with a parameterless constructor
  • have all your entities inherit from EntityBase with a parameterless constructor
  • put the EntityMapAttribute to all properties in the model you would like to be mapped, you may additionally need the EntityConversionAttribute if types of the property in Model & Entity do not match
  • implement the ISqliteServiceSettingsProvider in your presentation project
  • implement the ISqliteService, or use the one already provided by this library (called SqliteService)
  • implement the ISQLitePlatform, or use the one already provided inside SQLite.Net-PCL (inside Sqlite.Net.Platform.*)
  • create any additional converters you need with the EntityConversionAttribute by EntityConversionAttribute.RegisterConverter()

Use this library

  • construct the GenericRepository & use it!

Examples

  • There is an example project appended to this library, take a look at the ModelRepository inside the folder Repositores for a recommended way to use this library

Some notes

  • pass an implemententation of the IExceptionLogger to the GenericRepository so the GenericRepository may not throw exceptions but log them
  • there is already an implementation of an enum (model) to int (entity) converter, so no need to worry about that
  • there are some other commonly used converters already implemented in the namespace Converters.*, you still need to register them if you want to use them
  • the SqliteService creates tables automatically
  • usage of an IoC implementation is strongly encuraged (I can recommend the SimpleIoC implementation from the MvvmLight library, available on nuget)

Short example code

after everything is prepared correctly, you can use the generic repositories as this:

public class ModelRepository
{
    private readonly GenericRepository<MyModel, MyBaseEntity> _genericRepo;

    public ModelRepository()
    {
        //register additional converters
        EntityConversionAttribute.RegisterConverter(new ListStringToStringConverter(), typeof(string), typeof(List<string>));
        EntityConversionAttribute.RegisterConverter(new GuidStringConverter(), typeof(string), typeof(Guid));

        //construct the sqlite service, it is recommended to do this with an IoC approach
        var service = new SqliteService(new SQLitePlatformWinRT(), new SqliteServiceSettingsProvider());
        //construct generic repository
        _genericRepo = new GenericRepository<MyModel, MyBaseEntity>(service);
    }

    /// <summary>
    /// retrieves model this id
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<MyModel> GetByIdAsync(int id)
    {
        return await _genericRepo.GetByIdAsync(id);
    }

    /// <summary>
    /// Adds or Updates this model in the database
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> SaveAsync(MyModel model)
    {
        return await _genericRepo.SaveAsyc(model);
    }
}

About

opinionated sqlite abstraction simplifying the mapping of model classes to entity classes and back

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages