Class RepositoryBase<TModel>
Base class that implements the interface with the methods responsible for data administration of redis.
Inheritance
Implements
Inherited Members
Namespace: StoneCo.Framework.Data.Redis.Repositories
Assembly: cs.temp.dll.dll
Syntax
public class RepositoryBase<TModel> : IRepository<TModel> where TModel : class, IModel
Type Parameters
Name | Description |
---|---|
TModel | The type of the response to be returned in the result. |
Constructors
RepositoryBase(IDbContext)
Default constructor.
Declaration
public RepositoryBase(IDbContext dbContext)
Parameters
Type | Name | Description |
---|---|---|
IDbContext | dbContext | Recieve by dependency injection an instance of IDbContext. |
Examples
note
This is an initial configuration that should be used as a basis for the following examples.
using StoneCo.Framework.Model;
public class Foo : IModel
{
public Foo(long id, string content, bool done)
{
Id = id;
Content = content;
Done = done;
}
public long Id { get; private set; }
public string Content { get; private set; }
public bool Done { get; private set; }
}
Loading information for dependency injection.
services.AddScoped<IRepository, RepositoryBase>();
Methods
Add(Int64, TModel)
Stores an object into Redis Database.
Declaration
public bool Add(long key, TModel model)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | key | Long type more than zero. |
TModel | model | Object that will be stored. |
Returns
Type | Description |
---|---|
System.Boolean | Boolean value that's represent success on operation. |
Examples
Adding an TModel:
public class TestService
{
private IRepository<IRepository<Foo>> Repository { get; }
public TestService(IRepository<IRepository<Foo>> repository) => Repository = repository;
public bool Bar()
{
var foo = new Foo(repository.GetIncrementedDefaultKey(), "Test " + DateTime.Now.ToString(), false);
return repository.Add(foo.Id, foo);
}
}
GetAndPopById(Int64)
Get an object defined 'TModel', passing a key and remove it of redis database.
Declaration
public TModel GetAndPopById(long key)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | key | Long type more than zero. |
Returns
Type | Description |
---|---|
TModel | A object 'TModel' or null if not found. |
Examples
Getting an TModel object by id:
public class TestService
{
private IRepository<IRepository<Foo>> Repository { get; }
public TestService(IRepository<IRepository<Foo>> repository) => Repository = repository;
public void Bar()
{
var foo = repository.GetAndPopById(12);
}
}
GetAndPopById(String)
Get an object defined 'TModel', passing a key and remove it of redis database.
Declaration
public TModel GetAndPopById(string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | String value that contains 12 characters. |
Returns
Type | Description |
---|---|
TModel | A object 'TModel' or null if not found. |
Examples
Getting an TModel object by id:
public class TestService
{
private IRepository<IRepository<Foo>> Repository { get; }
public TestService(IRepository<IRepository<Foo>> repository) => Repository = repository;
public void Bar()
{
var foo = repository.GetAndPopById("000000000012");
}
}
GetAndPopList(Int32)
Get a list of objects and remove their items from Redis Data.
Declaration
public List<TModel> GetAndPopList(int quantity = 1)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | quantity | Max Number of itens that will be returned. |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<TModel> | List with or without any itens. |
Examples
Adding an TModel:
public class TestService
{
private IRepository<IRepository<Foo>> Repository { get; }
public TestService(IRepository<IRepository<Foo>> repository) => Repository = repository;
public int Bar()
{
List<Foo> fooList = repository.GetAndPopList(100);
return fooList.Count;
}
}
GetById(Int64)
Get an object defined 'TModel', passing a key.
Declaration
public TModel GetById(long key)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | key | Long type more than zero. |
Returns
Type | Description |
---|---|
TModel | A object 'TModel' or null if not found. |
Examples
Getting an TModel object by id:
public class TestService
{
private IRepository<IRepository<Foo>> Repository { get; }
public TestService(IRepository<IRepository<Foo>> repository) => Repository = repository;
public void Bar()
{
var foo = repository.GetById(12);
}
}
GetById(String)
Get an object defined 'TModel', passing a stored key.
Declaration
public TModel GetById(string key)
Parameters
Type | Name | Description |
---|---|---|
System.String | key | String value that contains 12 characters. |
Returns
Type | Description |
---|---|
TModel | A object 'TModel' or null if not found. |
Examples
Getting an TModel object by id:
public class TestService
{
private IRepository<IRepository<Foo>> Repository { get; }
public TestService(IRepository<IRepository<Foo>> repository) => Repository = repository;
public void Bar()
{
var foo = repository.GetById("000000000012");
}
}
GetIncrementedDefaultKey()
Get next number of default sequence that represents a unique key.
Declaration
public long GetIncrementedDefaultKey()
Returns
Type | Description |
---|---|
System.Int64 | A long type more than zero. |
Examples
Getting an unique id:
var foo = new Foo(repository.GetIncrementedDefaultKey(), "Test " + DateTime.Now.ToString(), false);
GetList(Int32)
Get a list of objects from Redis Data.
Declaration
public List<TModel> GetList(int quantity = 1)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | quantity | Max Number of itens that will be returned. |
Returns
Type | Description |
---|---|
System.Collections.Generic.List<TModel> | List with or without any itens. |
Examples
Adding an TModel:
public class TestService
{
private IRepository<IRepository<Foo>> Repository { get; }
public TestService(IRepository<IRepository<Foo>> repository) => Repository = repository;
public int Bar()
{
List<Foo> fooList = repository.GetList(100);
return fooList.Count;
}
}