Data Contracts

Getting Started

The VersaTul Data Contracts project provides generic interfaces that are supported throughout the Data manipulating projects in the VersaTul ecosystem. These tend to be more database-oriented projects. Developers who may want to change the underline implementation of these contracts can create their own implementation of such contract and supply it to the VersaTul project in which they require to change the behavior.

Installation

To use VersaTul Data Contracts, first install it using nuget:

PM> NuGet\Install-Package VersaTul.Data.Contracts -Version latest

Main Components

  1. IDataConnection : Represent the Database Connection details needed to connect to a database.

  2. IRepository<TEntity, TKey> : Represents a collection of functionality that can be performed on a data store.

  3. IUnitOfWork : Represents the absolute unit of work to be sent to a database.

Functional Summary

  1. string IDataConnection.GetConnectionString() : Overloaded methods for getting the ConnectionString for the current application’s default configuration.

  2. string IDataConnection.GetProviderName() : Overloaded methods for getting the fully qualified provider name.

  3. TEntity IRepository<TEntity, TKey>.Add() : Overloaded methods for adding the given entity to the data store.

  4. void IRepository<TEntity, TKey>.AddRange() : Overloaded methods for adding the given entities to the data store.

  5. TEntity IRepository<TEntity, TKey>.Find() : Overloaded methods for finding an entity with the given primary key values.

  6. TEntity IRepository<TEntity, TKey>.Get() : Overloaded methods for getting entities in the data Store.

  7. TEntity IRepository<TEntity, TKey>.Update() : Overloaded methods for updating entities in the data store.

  8. TEntity IRepository<TEntity, TKey>.Remove() : Overloaded methods for deleting entities from the data store.

  9. TEntity IUnitOfWork.Commit() : Overloaded methods for saving the changes from the DbContext out the database..

  10. TEntity IUnitOfWork.Rollback() : Pull data from database and override changes in the Change Tracker. This method is not a true rollback in the database.

Code Examples

Example implementation as found in VersaTul.Data.EFCore.
// code shortened for breviate.
public abstract class BaseRepository<TEntity, TKey> : IRepository<TEntity, TKey> where TEntity : class, new()
{
    public TEntity Add(TEntity entity)
    {
        if (entity == null) { throw new ArgumentNullException(nameof(entity), "Argument cannot be null."); }

        return this.entity.Add(entity).Entity;
    }

    public void AddRange(IEnumerable<TEntity> entities) => entity.AddRange(entities);

    public TEntity Find(params object[] keyValues) => entity.Find(keyValues);
}

// code shortened for breviate.
public abstract class BaseUnitOfWork : IUnitOfWork
{
    public int Commit() => DataContext.SaveChanges();

    public void Rollback()
    {
        DataContext
            .ChangeTracker
            .Entries()
            .ToList()
            .ForEach(set => set.Reload());
    }
}

// code shortened for breviate.
public class ConnectionInfo : IConnectionInfo
{
    public ConnectionInfo(string connectionString, string providerName)
    {
        ConnectionString = connectionString;

        ProviderName = providerName;
    }

    public string ConnectionString { get; }

    public string ProviderName { get; }
}

Changelog

V1.0.8

  • Dependent package updates

  • Clean up interface

  • Minor fixes

V1.0.7

  • Code ported to dotnet core

  • Documentation completed