Entity Framework Application - A Tool for LeetCode
2017-05-29 23:04
507 查看
Readers
If you are interested and ready to get started for Entity Framework, esp many-to-many relationship issue;If you are lost in management for Leetcode problems you have done;
If you want to study a new ORM Framework;
If you find a or many bugs;
If you want to give me a suggestion
BackGround
This article is a practical description of the problem, which uses the Entity Framework, based on WinForm data binding technology, and the SQL Server database to manage LeetCode problems that we have done. LeetCode manager will manage problems according to “LeetCode tags“, such as Array, HashTable, Stack, Tree, Graph etc… Under each category problem can be created, deleted, modified, and one problem may be associated with multiple tags, so this is a typical instance used to solve many-to-many relationship.Software Structure
The main GUI of software is as follows, its main structure includes:
Entity;
DAL;
Controller;
View;
Also include other folders:
DataMd is a template file that imports problems we have done;
Lib is the help object of software;
Migrations are done with the package management console after making changes to the entity’s attributes;
Assembly: third-party control: Infragistics UltraCombo control.
Details
Now, Let’s introduce the folders we have involved above in detail.Entity
Building entity is based on Code First of EF. Entity folder includes following files: Problem entity and Tag entity.Problem Entity:
public class Problem { public int ProblemId { get; set; } [MaxLength(50)] [Required(ErrorMessage = "Title is required.")] public string Number { get; set; } public string Title { get; set; } public string LtUrl { get; set; } public string CsdnAddress { get; set; } public string Content { get; set; } public string TagsCombine { get; set; }//it's separated by ';' // Tags is the navigation property public virtual IList<Tag> Tags { get; set; } //it's a navigation property }
Tag entity:
public class Tag { public int TagId { get; set; } [MaxLength(30)] public string Name { get; set; } //Problems is the navigation property public virtual IList<Problem> Problems { get; set; } }
DAL
DAL is the abbreviation of Data Access Layer, and we can simplify the code with EF. Now, we look at DAL objects.First, it is MyDb object, which is inherited from ContextDb object:
public class MyDb:DbContext { public DbSet<Tag> Tags { get; set; } public DbSet<Problem> Problems { get; set; } //many-to-many relationship building: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Tag>().HasMany(p => p.Problems).WithMany(r => r.Tags).Map( t => t.ToTable("TagProblem"). MapLeftKey("TagId"). MapRightKey("ProblemId")); } }
Next, let’s create an instance of MyDb: _db, remember it’s only one instance for operating Database.
public class BaseDAL { protected static MyDb _db; protected BaseDAL() { _db = _db ?? new MyDb(); } }
Next, we create an object named ProblemDAL, which inherited from BaseDAL and it mainly solves the issues about problem data access database. It’s structure is following:
public class ProblemDAL:BaseDAL { public IEnumerable<Tag> QueryAllTags() { DbSet<Tag> tags = _db.Tags; if (tags == null) return new List<Tag>(); return tags.AsEnumerable<Tag>(); } public IEnumerable<Problem> QueryAllProblems() { DbSet<Problem> problems = _db.Problems; if (problems == null) return new List<Problem>(); return problems.AsEnumerable<Problem>(); } public bool IsNew(Problem problem) { return problem.ProblemId == 0; } public Problem CreateAProblem(Problem problem) { if (!IsNew(problem)) return problem; _db.Problems.Add(problem); _db.SaveChanges(); var problems = _db.Problems.OrderByDescending(r => r.ProblemId).AsEnumerable<Problem>(); if (!SysHelper.CollectionNullOrEmpty<Problem>(problems)) return problems.First(); return new Problem(); } public void UpdateProblems() { _db.SaveChanges(); } public void DeleteAProblem(Problem problem) { _db.Problems.Remove(problem); _db.SaveChanges(); } public void DeleteProblems(IList<Problem> problems) { int cnt = problems.Count(); for(int i=0;i<cnt;i++) { if(_db.Problems.Contains(problems[i])) _db.Problems.Remove(problems[i]); } _db.SaveChanges(); } }
Similarly, we have the TagDAL object:
public class TagDAL:BaseDAL { public IEnumerable<Tag> QueryAllTags() { DbSet<Tag> tags = _db.Tags; if (tags == null) return new List<Tag>(); return tags.AsEnumerable<Tag>(); } public Tag QueryTagByName(string name) { DbSet<Tag> tags = _db.Tags; if (tags == null) return new Tag(); return tags.FirstOrDefault(r => r.Name == name); } public Tag QueryTagById(int id) { DbSet<Tag> tags = _db.Tags; if (tags == null) return new Tag(); return tags.FirstOrDefault(r => r.TagId == id); } public bool IsNew(Tag tag) { return tag.TagId == 0; } public Tag CreateATag(Tag tag) { if (!IsNew(tag)) return tag; _db.Tags.Add(tag); _db.SaveChanges(); var tags = _db.Tags.OrderByDescending(r => r.TagId).AsEnumerable<Tag>(); if (!SysHelper.CollectionNullOrEmpty<Tag>(tags)) return tags.First(); return new Tag(); } public void DeleteATag(Tag tag) { _db.Tags.Remove(tag); _db.SaveChanges(); } public void UpdateATagName(Tag tag) { Tag tagdb = _db.Tags.FirstOrDefault(r => r.TagId == tag.TagId); if (tag != null) tagdb.Name = tag.Name; _db.SaveChanges(); } }
Controller
Here, controllers are not same to the controllers of MVC, but it’s a bit of similar to that of MVC. It is the middle layer between DAL and View layer. I donot want to write it in detail here and you could look at it if you have some interest in it. Following is its structure:View
Views are mainly including Index form, Tag input form, Problem input form. Here, I don’t plan to write it in detail, because this is not the key point for this passage.Main Function
LeetcodeManager mainly manage to the Tag and Problems on the management of many-to-many relationship, including the Tag of the creation, deletion, modification; Problem of the creation, deletion, modification. In addition, problems sort in accordance with the number of problem in an ascending and descending order, and can be filtered by the number of problem. Also a click can clear all the data in database; we have quickly import problems in accordance with a fixed format on the problems.Next work
Asp.net version is implemented.Fork and Star
https://github.com/jackzhenguo/LeetCodeManager相关文章推荐
- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
- 什么是游戏开发的实体系统框架 What is an entity system framework for game development
- Entity Framework for Oracle 基本配置
- Entity Framework Code First for SQL Compact
- Application Blocks for the .NET Compact Framework
- Entity Framework samples For RTM
- Feature CTP Walkthrough: Code Only for the Entity Framework
- VS2010 使用 EntityFramework For SQL Server Compact 4.0
- Entity Framework Code First使用者的福音 --- EF Power Tool使用记之一
- error 0152: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.
- Solve: Your project references the latest version of Entity Framework (for MySQL) in Visual Studio 2013
- MySql For Entity Framework 6 Code First 例子
- EF Provider for Access/ODBC 以及ADO.Net Entity Framework 与Linq to SQL的比较和适用场景
- Entity Framework CodeFirst For Oracle[转]
- Application Blocks for the .NET Compact Framework
- Entity Framework Power Tool
- Entity Framework samples For RTM
- What is an entity system framework for game development?
- Oracle Entity Framework For Visual Studio 2010
- 翻译:ASP.NET MVC Example Application over Northwind with Entity Framework