Introduction
Using EntityWorker.Core
to save and delete object depending on objectstatus
.
Background
How many web developers use JavaScript to modify items, then save it to the database?
Here, I'm going to show you a way to modify objects, then save it to the database with ease.
Using the Code
Modules
First let's start with creating our modules.
We will create a Page
class that contains many categories as an example.
public enum ObjectStatus = { Added, Removed }
public abstract class Entity{
[PrimaryKey]
public Guid? Id { get; set; }
public ObjectStatus Object_Status { get; set; }
}
public class Category: Entity {
public string Name { get; set; }
}
public class PageCategory: Entity {
[ForeignKey(typeof(Page)]
public Guid Page_Id { get; set;}
[ForeignKey(typeof(Category)]
public Guid Category_Id { get; set; }
[IndependedData]
public Category Category { get; set; }
}
public class Page: Entity {
[ToBase64String]
[AllowHtml]
public string Content { get; set; }
public List<PageCategory> Categories { get; set; }
}
Now that we have our Modules, we should prepare our dbContext
and override the save mechanism of entityworker.core
.
using EntityWorker.Core.Interface;
using EntityWorker.Core.Transaction;
using System;
using System.Collections;
using System.Linq;
public class DbContext : Transaction
{
public DbContext() : base(GetConnectionString(), EntityWorker.Core.Helper.DataBaseTypes.Mssql)
{
}
public override IRepository Save(object entity)
{
void Prepare(object data)
{
if (data == null)
return;
var props = EntityWorker.Core.FastDeepCloner.DeepCloner.GetFastDeepClonerProperties
(data.GetType()).Where(x => !x.IsInternalType && x.CanRead);
foreach (var prop in props)
{
if (prop.ContainAttribute<JsonDocument>() ||
prop.ContainAttribute<XmlDocument>() ||
prop.ContainAttribute<ExcludeFromAbstract>())
continue;
var value = prop.GetValue(data);
if (value == null)
continue;
if (value is IList)
{
IList newList = (IList)Activator.CreateInstance(value.GetType());
var ilist = value as IList;
var i = ilist.Count - 1;
while (i >= 0)
{
var e = ilist[i] as Entity;
i--;
if (e.Object_Status == EnumHelper.ObjectStatus.Removed)
{
Delete(e);
}
else newList.Add(e);
}
prop.SetValue(data, newList);
}
else
{
var e = value as Entity;
if (e.Object_Status == EnumHelper.ObjectStatus.Removed)
{
Delete(e);
prop.SetValue(data, null);
}
else Prepare(e);
}
}
}
if (entity as Entity != null)
{
if ((entity as Entity).Object_Status != EnumHelper.ObjectStatus.Removed)
Prepare(entity);
else
{
Delete(entity);
return this;
}
}
return base.Save(entity);
}
protected override void OnModuleStart()
{
if (!base.DataBaseExist())
base.CreateDataBase();
var latestChanges = GetCodeLatestChanges();
if (latestChanges.Any())
latestChanges.Execute(true);
InitializeMigration();
}
public static string GetConnectionString()
{
return @"Server=.\SQLEXPRESS; Database=SEO; User Id=root; Password=root;";
}
}
}
Now we prepare a json data that contains an object to be added/updated or removed.
var category = new dbContext().Get<Category>().Json();
var pagesJson = {
content: "test",
categories: [
{ id:"5ac53e44-ba94-4a76-b94f-032077c1ef10", category: category }
{ category: category }, { id:"5ac53e44-ba94-4a76-b94f-032077c1ef79", category: category,
object_status: "Removed" }
]
}
And now the only thing left is to save this json to the database:
using (var db = new DbContext()){
var page = db.FromJson<Page>(pagesJson);
db.Save(page);
db.SaveChanges();
}
Points of Interest
This is really helpful when you are developing web application and using JavaScript(Json) to manipulate the data.
I hope that you learn from this.
History
- 14th May, 2017: Initial version