Introduction
EntityWorker
is a new ORM that I built for people who like the old way of programming as well as the new way (Entityframework
).
Background
I have worked with entityframework
, and loved the new way of organizing my code as well as using linq to communicate with the database.
Sadly, Entityframework
is to slow, the way its build the SQL makes it too slow as well as propertychanged
, memoryuseg
, etc.
Entityworker
addresses almost all issues the entityframework
has, and also includes almost all the future Entityframework
has.
Nuget
Future and Code Reviewing
Using the Code
Let's build three tables, User
, Roles
and Person
.
public abstract class Entity{
[PrimaryKey]
public long? Id { get; set }
}
[Table("Roles")]
public class Role: Entity{
public string Name { get; set; }
public int Level { get; set; }
}
public class UserRoles: Entity{
[ForeignKey(typeof(User))]
public long User_Id { get; set; }
[ForeignKey(typeof(Role))]
public long Role_Id { get; set; }
[IndependentData]
public Role Role { get; set; }
public User User { get; set; }
}
public class Users: Entity{
[DataEncode]
public string Password { get; set; }
public Person Person { get; set; }
public List<UserRoles> UserRoles { get; set; }
}
public class Person : Entity{
public string FullName { get; set; }
public string Email { get; set; }
[ForeignKey(typeof(User))]
public long User_Id { get; set; }
}
That is all for our table, now like Entityframework
, we have migrations
and migrationconfig
.
Let's build a migration StartUp
and add an Admin
Role.
public class StartUp : Migration
{
public override void ExecuteMigration(IRepository repository)
{
var adminUser = new User() {
pessword= "Admin",
UserRoles = new List<UserRoles> { Role= new Role(){ Name: "Admin" } }
Person = new Person() {
FullName="Alen Toma",
Email="xxx@gmail.com"
}
};
repository.Save(adminUser);
}
}
Now to determine which migration will be executed first, we have to create a migrationconfig
.
public class MigrationConfig : IMigrationConfig
{
public IList<Migration> GetMigrations(IRepository repository)
{
return new List<Migration>(){new StartUp()};
}
}
Now that we have created our modules and migration, we need to create the Repository
.
public class Repository : Transaction
{
public Repository(DataBaseTypes dbType = DataBaseTypes.Mssql) :
base(GetConnectionString(dbType), dbType)
{
}
protected override void OnModuleStart()
{
if (!base.DataBaseExist())
base.CreateDataBase();
var latestChanges = GetCodeLatestChanges();
if (latestChanges.Any())
latestChanges.Execute(true);
InitializeMigration();
}
protected override void OnModuleConfiguration(IModuleBuilder moduleBuilder)
{
}
public static string GetConnectionString(DataBaseTypes dbType)
{
if (dbType == DataBaseTypes.Mssql)
return @"Server=.\SQLEXPRESS; Database=CMS; User Id=root; Password=root;";
else if (dbType == DataBaseTypes.Sqlite)
return @"Data Source=D:\Projects\CMS\source\App_Data\CMS.db";
else return "Host=localhost;Username=postgres;Password=root;Database=cms";
}
}
Using Linq, you could communicate with our database now.
Simple:
using ( var rep = new Repository()){
var query= rep.Get<User>().Where(x=> x.UserRoles.Any(a=> a.Role.Name == "Admin")).
Loadchildren();
List<User> users= query.Execute();
string sql = query.ParsedSql;
}
Check out the Github for more documentation and please feel free to ask.
History
- 23rd December, 2019: Initial version