Inject TableName as Parameter for Update & Insert on GenericEntity in ServiceStack Ormlite - ormlite-servicestack

I have 3 tables of same structure so i have created the following entity using ServiceStack
public class GenericEntity
{
[Alias("COL_A")]
public string ColumnA { get; set; }
}
For retriving the results I use the following line of code. In it I pass the table name like "TableA"/"TableB" so that i can pull the appropriate results
db.Select<GenericEntity>(w => w.Where(whereExperssion).OrderBy(o => o.ColumnA).From("TableA"));
For delete i use the following code
db.Delete<GenericEntity>(w => w.Where(q => q.ColumnA == "A").From("TableA"));
With From() I can pass table name for SELECT & DELETE operations. Is there a similar way for Inserting and updating? The below is the snippet code I am using for update and insert
Insert
db.Insert(new GenericEntity() {});
Update
db.Update<GenericEntity>(new GenericEntity { ColumnA = "ModifiedData"},p => p.ColumnA == "OriginalData");

As you're wanting to this for multiple API's I've added a test showing how to achieve the desired behavior by extending OrmLite's API's with your own custom extension methods that modifies OrmLite's table metadata at runtime to add new API's that allow specifying the table name at runtime, i.e:
var tableName = "TableA"'
db.DropAndCreateTable<GenericEntity>(tableName);
db.Insert(tableName, new GenericEntity { Id = 1, ColumnA = "A" });
var rows = db.Select<GenericEntity>(tableName, q =>
q.Where(x => x.ColumnA == "A"));
rows.PrintDump();
db.Update(tableName, new GenericEntity { ColumnA = "B" },
where: q => q.ColumnA == "A");
rows = db.Select<GenericEntity>(tableName, q =>
q.Where(x => x.ColumnA == "B"));
rows.PrintDump();
With these extension methods:
public static class GenericTableExtensions
{
static object ExecWithAlias<T>(string table, Func<object> fn)
{
var modelDef = typeof(T).GetModelMetadata();
lock (modelDef) {
var hold = modelDef.Alias;
try {
modelDef.Alias = table;
return fn();
}
finally {
modelDef.Alias = hold;
}
}
}
public static void DropAndCreateTable<T>(this IDbConnection db, string table) {
ExecWithAlias<T>(table, () => { db.DropAndCreateTable<T>(); return null; });
}
public static long Insert<T>(this IDbConnection db, string table, T obj, bool selectIdentity = false) {
return (long)ExecWithAlias<T>(table, () => db.Insert(obj, selectIdentity));
}
public static List<T> Select<T>(this IDbConnection db, string table, Func<SqlExpression<T>, SqlExpression<T>> expression) {
return (List<T>)ExecWithAlias<T>(table, () => db.Select(expression));
}
public static int Update<T>(this IDbConnection db, string table, T item, Expression<Func<T, bool>> where) {
return (int)ExecWithAlias<T>(table, () => db.Update(item, where));
}
}
Adding your own extension methods in this way allows you to extend OrmLite with your own idiomatic API's given that OrmLite is itself just a suite of extension methods over ADO.NET's IDbConnection.

Related

Ardalis.Specification and Azure Cosmos Database : What is the place where the query is applied? Cosmos Database or Application Memory?

I am trying to implement Ardalis.Specification in .net core Web API application that uses Azure Cosmos Database
Below is my Specification definition
public class ToDoItemSearchSpecification : Specification<ToDoItem>
{
public ToDoItemSearchSpecification(
string title = "",
int pageStart = 0,
int pageSize = 50,
string sortColumn = "title",
SortDirection sortDirection = SortDirection.Ascending,
bool exactSearch = false
)
{
if (!string.IsNullOrWhiteSpace(title))
{
if (exactSearch)
{
Query.Where(item => item.Title.ToLower() == title.ToLower());
}
else
{
Query.Where(item => item.Title.ToLower().Contains(title.ToLower()));
}
}
// Pagination
if (pageSize != -1) // Display all entries and disable pagination
{
Query.Skip(pageStart).Take(pageSize);
}
// Sort
switch (sortColumn.ToLower())
{
case ("title"):
{
if (sortDirection == SortDirection.Ascending)
{
Query.OrderBy(x => x.Title);
}
else
{
Query.OrderByDescending(x => x.Title);
}
}
break;
default:
break;
}
}
}
and Evaluator
public class CosmosDbSpecificationEvaluator<T>: SpecificationEvaluatorBase<T> where T: class {}
//Below is the Repository method
private IQueryable<T> ApplySpecification(ISpecification<T> specification)
{
CosmosDbSpecificationEvaluator<T> evaluator = new CosmosDbSpecificationEvaluator<T>();
IOrderedQueryable<T> cosmosDBItems = _container.GetItemLinqQueryable<T>();
return evaluator.GetQuery(cosmosDBItems, specification);
}
public async Task < IEnumerable<T>> GetItemsAsync(ISpecification<T> specification) {
IQueryable <T> queryable = ApplySpecification(specification);
FeedIterator<T> iterator = queryable.ToFeedIterator();
List<T> results = new List<T>();
while (iterator.HasMoreResults) {
FeedResponse<T> response = await iterator.ReadNextAsync();
results.AddRange(response.ToList());
}
return results;
}
Below is the Controller implementation
[HttpGet("/search/title/{SearchString}", Name = "GetAllByTitle")]
[EnableQuery]
[ApiConventionMethod(typeof (DefaultApiConventions), nameof(DefaultApiConventions.Get))]
public async Task<IEnumerable<ToDoItem>> GetAllByTitle(string SearchString)
{
ToDoItemSearchSpecification specification = new ToDoItemSearchSpecification(SearchString);
IEnumerable<ToDoItem> result = await _repo.GetItemsAsync(specification);
return result;
}
And it is being called like mentioned below
https://localhost:7110/search/title/Get%200%20beers
I am trying to understand where the query is applied. Cosmos Database or Application Memory? and what does the below line do?
FeedIterator<T> iterator = queryable.ToFeedIterator();
The query is applied as an SQL query on the database side.
The Cosmos database will only return documents that satisfy the WHERE clause.

is it a good idea to do transformation in a database model with ASP.Net

we are a small development team.
We develop in ASP.NET and we are starting to use generic controllers and services.
The goal is to have solid methods for things that are repetitive.
What we ask ourselves is if it is a good idea to do some transformation in the data models to allow us to reuse our functions that we know are working?
Exemple: we have a combobox and we want to manage the display and search. It's always the same and redundant.
This is my class
[Table("stage.Test")]
public partial class Test : IBaseEntity, ICombobox
{
public virtual Product Product { get; set; }
public string nom { get; set; }
public string prenom { get; set; }
public string title { get; set; }
[NotMapped]
public virtual string AffichageCombobox => nom + prenom;
[NotMapped]
public virtual string TexteRecherche => Product.Gabarit.Description;
}
as you can see i have two columns with the tag [NotMapped]. These are the columns in the interface ICombobox
public interface ICombobox
{
string AffichageCombobox { get;}
string TexteRecherche { get; }
}
this is the first service where I use one of my two columns which redirects to other columns. [We use the column "AffichageCombobox" from the model]
public List<ComboboxViewModel> GetComboboxViewModel(int? id, bool actifseulement, string text)
{
var query = _requestDatabaseService.GetComboboxQuery<T>(id, actifseulement, text);
var list = query.Select(table => new ComboboxViewModel
{
Id = table.Id,
AffichageCombobox = table.DateHFin == null ? table.AffichageCombobox : table.AffichageCombobox + " (inactif)"
}).ToList();
return list;
}
This is the RequestDatabaseService [We use the column "TexteRecherche" from the model]
public List<T> GetComboboxQuery<T>(int? id, bool actifseulement, string text) where T : class, IBaseEntity, ICombobox
{
text = text.ToLower();
var list = _dbContext.Set<T>()
.If(id.HasValue,
q => q.Where(x => x.Id == id))
.If(actifseulement,
q => q.Where(x => x.DateHFin == null))
.If(text != "",
q => q.Where(x => x.TexteRecherche.ToLower() == text))
.ToList();
return list;
}
As you can see, I am using an interface to add columns to redirect to the correct columns to my data model to avoid overriding my methods for two column.
Is it a good idea, a good practice ?
What do you think is the best practice if we want to do generic functions, but the columns are not called the same way?
Thank you!
Your solution has a lot of weaknesses
You have extended Model to handle specific UI cases. In my opinion it is bad practice.
Your virtual properties will not work in LINQ query. EF translates only Expression because it canot look into compiled property body.
What we can do here is simplifying of building such comboboxes. I have defind set fo extensions which can be reused for such scenarios. Sorry if there some mistakes, written from memory.
How it can be used:
Assuming that GetComboboxViewModel is not in generic class
public List<ComboboxViewModel> GetComboboxViewModel(int? id, bool actifseulement, string text)
{
// uncover DbContext. All that we need is IQueryable<Test>
var ctx = _requestDatabaseService.GetContext();
var query = ctx.Test.AsQueryable();
var comboItems = query
.FilterItems(id, actifseulement)
.GetComboboxQuery(text, e => e.Product.Gabarit.Description, e => e.nom + e.prenom)
.ToList();
return comboItems;
}
Think about this solution and yes, we can register somewhere pair of Lmbdas Dictionary<Type, (LambdaExpression: searchProp, LambdaExpression: displayProp)> and dynamically build call above.
Realisation:
public static class QueryableExtensions
{
// more simlified version for filtering
public static IQueryable<T> WhereIf(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
{
return condition ? query.Where(predicate) : query;
}
// handy function for filtering
public static IQueryable<T> FilterItems<T>(this IQueryable<T> query, int? id, bool onlyActive)
where T : IBaseEntity
{
query = query
.WhereIf(id.HasValue, x => x.Id == id)
.WhereIf(onlyActive, x => x.DateHFin == null)
return query;
}
// dynamic generation of filtering and projection
public static IQueryable<ComboboxViewModel> GetComboboxQuery<T>(this IQueryable<T> query, string text, Expression<Func<T, string>> searchProp, Expression<Func<T, string>> dsiplayProp)
where T : IBaseEntity
{
if (!string.IsNullOrEmpty(text))
{
text = text.ToLower();
// defining search pattern
// this also extension point, you may use here `Contains` or FullText search functions
Expression<Func<string, string, bool>> filterFunc = (s, t) => s.ToLower() == t;
// reusing parameter from searchProp lambda
var param = searchProp.Parameters[0];
// applying pattern to searchprop
var filterBody = ExpressionReplacer.GetBody(filterFunc, searchProp.Body, Expression.Constant(text));
// applying generated filter
var filterPredicate = Expression.Lambda<Func<T, bool>>(filterBody, param);
query = query.Where(filterPredicate);
}
// defining template for Select
Expression<Func<T, string, ComboboxViewModel>> createTemplate = (entity, dp) => new ComboboxViewModel
{
Id = entity.Id,
AffichageCombobox = entity.DateHFin == null ? dp : dp + " (inactif)"
};
// reusing parameter from dsiplayProp lambda
var entityParam = dsiplayProp.Parameters[0];
// injecting dsiplayProp into createTemplate
var selectBody = ExpressionReplacer.GetBody(createTemplate, entityParam, dsiplayProp.Body);
var selectLambda = Expression.Lambda<Func<T, ComboboxViewModel>>(selectBody, entityParam);
// applying projection
var comboQuery = query.Select(selectLambda);
return comboQuery;
}
// helper class for correcting expressions
class ExpressionReplacer : ExpressionVisitor
{
readonly IDictionary<Expression, Expression> _replaceMap;
public ExpressionReplacer(IDictionary<Expression, Expression> replaceMap)
{
_replaceMap = replaceMap ?? throw new ArgumentNullException(nameof(replaceMap));
}
public override Expression Visit(Expression exp)
{
if (exp != null && _replaceMap.TryGetValue(exp, out var replacement))
return replacement;
return base.Visit(exp);
}
public static Expression Replace(Expression expr, Expression toReplace, Expression toExpr)
{
return new ExpressionReplacer(new Dictionary<Expression, Expression> { { toReplace, toExpr } }).Visit(expr);
}
public static Expression Replace(Expression expr, IDictionary<Expression, Expression> replaceMap)
{
return new ExpressionReplacer(replaceMap).Visit(expr);
}
public static Expression GetBody(LambdaExpression lambda, params Expression[] toReplace)
{
if (lambda.Parameters.Count != toReplace.Length)
throw new InvalidOperationException();
return new ExpressionReplacer(Enumerable.Range(0, lambda.Parameters.Count)
.ToDictionary(i => (Expression) lambda.Parameters[i], i => toReplace[i])).Visit(lambda.Body);
}
}
}
Well, after writing this sample, I think, it can be cardinally simplified by using LINQKit. Will post another answer with LINQKit usage if you are interested,

Xamarin.Forms SQLite ,initialize data once on table creation

I am new in xamarin and I am trying to make xamarin.form app which contains sqlite database. So I know that the table is created once but also I have some records that I need to be in that table by default. I mean when table is created the data also must be initialized with it once. According to tutorial I have a class to handle the database called DataAccess.cs
class DataAccess : IDisposable
{
public void Dispose()
{
database.Dispose();
}
private SQLiteConnection database;
public ObservableCollection<DataModel> dataz { get; set; }
private static object collisionLock = new object();
public DataAccess()
{
database = DependencyService.Get<IConfig>().DbConnection();
database.CreateTable<DataModel>();
//database.Insert(new DataModel { Did = 1 , Data = "aaaa"});
//database.Insert(new DataModel { Did = 2, Data = "bbb" });
//database.Insert(new DataModel { Did = 3, Data = "ccc" });
this.dataz = new ObservableCollection<DataModel>(database.Table<DataModel>());
if (!database.Table<DataModel>().Any())
{
addNewData();
}
}
public void addNewData()
{
this.dataz.Add(new DataModel { Did = 1, Data = "aa" });
}
public void SaveData(DataModel record)
{
lock (collisionLock)
{
database.Insert(record);
}
}
public DataModel GetDataById(int id)
{
lock (collisionLock)
{
return database.Table<DataModel>().
FirstOrDefault(x => x.Did == id);
}
}
public List<DataModel> GeyAllData()
{
return database.Table<DataModel>().ToList();
}
}
Since the table is created in above class instructor. So I tried to initialized data there but data added to table on each run. So I confused how to initialize data on first run only.
You could go one of two ways.
1: Check if the table does not exist yet and if not, create it and add your data
var result = await conn.ExecuteScalarAsync<int>("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='DataModel';", new string[] { });
if (result == 0)
{
await conn.CreateTableAsync<DataModel>();
// Insert your initial data
}
2: Always check if the data exists and insert it if it does not
FYI: Assuming Did is your PrimaryKey of DataModel
var row = await conn.FindAsync<Record>(1);
if (row == null)
{
// Insert the "Did = 1" row
}

Caching Results in a Static Variable

I'm using a Linq query to retrieve entities from an SQL server using the Entity Framework. When I update an entitiy, the EF is caching the result. I suspect this is because the ObjectContext is in a static variable (below). The only way to refresh the data using my code below is to call a method and set _db to null when there might be stale data displayed (Eg: in a GridView). Is there a way to just prevent it from caching, or to add some sort of end request handler to call this method on my data layer instead of needing to detect when there may be stale data displayed?
private static ServiceEntities _db;
protected static ServiceEntitiesDb
{
get
{
if (_db == null)
{
_db = new ServiceEntities();
_db.Contacts.MergeOption = MergeOption.OverwriteChanges; // failed
}
return _db;
}
}
public static IEnumerable<Contact> GetContactsByName(string name) {
var items = Db.Contacts;
var filteredName = items.Where(i => (i.Name??string.Empty).IndexOf(name) >=0);
return filteredName;
}
The slightly verbose solution (which I wanted to avoid) is to wrap it in a using block. Ie:
public static IEnumerable<Contact> GetContactsByName(string name) {
var items = Db.Contacts;
var filteredName = items.Where(i => (i.Name??string.Empty).IndexOf(name) >=0);
return filteredName;
}
Becomes
public static IEnumerable<Contact> GetContactsByName(string name) {
using (var db = new SomeContext()) {
var items = db.Contacts;
var filteredName = items.Where(i => (i.Name??string.Empty).IndexOf(name) >=0);
return filteredName;
}
}

JSON returned data formatting

I'm using code below to get JSON data:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getBranchViaJson()
{
Json(getBranchList(AppSession.BranchID.Value));
}
private object getBranchList(int n)
{
var mybranchList = from p in getBranchs(n)
select new { p.Code, p.Name };
return mybranchList.ToArray();
}
Client side retain value :
[{"Code":000,"Name":"Milan"},
{"Code":001,"Name":"Istanbul"},
{"Code":002,"Name":"Baku"},]
But I want to get like this:
[{000:"Milan"},{001:"Istanbul"},{002:"Baku"}]
What is the best way to do this?
First things first:
[{000:"Milan"},{001:"Istanbul"},{002:"Baku"}]
is invalid JSON. Properties must be quoted like so:
[{"000":"Milan"},{"001":"Istanbul"},{"002":"Baku"}]
In order to achieve this output you could use a Dictionary<string, string> that the JavaScriptSerializer will serialize to the desired output. So simply call the ToDictionary extension method on your model in order to convert it to a dictionary:
Like that:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getBranchViaJson()
{
var branches =
from p in getBranchs(AppSession.BranchID.Value)
select new { p.Code, p.Name };
var model = branches.ToDictionary(x => x.Code, x => x.Name);
return Json(new[] { model }, JsonRequestBehavior.AllowGet);
}
or if you want to keep your private method which returns an object you could make it return a dictionary:
[AcceptVerbs(HttpVerbs.Get)]
public JsonResult getBranchViaJson()
{
return Json(getBranchList(AppSession.BranchID.Value), JsonRequestBehavior.AllowGet);
}
private object getBranchList(int n)
{
var mybranchList = from p in getBranchs(n)
select new { p.Code, p.Name };
return new[] { mybranchList.ToDictionary(x => x.Code, x => x.Name) };
}
Notice that I used new[] { model }. That's because otherwise the JavaScriptSerializer won't produce a javascript array as required but a simple javascript object.
Remark: notice that I have added JsonRequestBehavior.AllowGet so that this controller action can be consumed with a GET request which is disabled by default for actions returning JSON responses.

Resources