Read EntityFramework. DynamicFilters source code _ experience 02, entityframework source code

Source: Internet
Author: User

Read EntityFramework. DynamicFilters source code _ experience 02, entityframework source code

The last time EntityFramework. dynamicFilters has an understanding of the overall project structure. This time, we will build an experience project by reading the instructions, sample projects, and unit tests, through the use of dynamic filters, we can have a psychological understanding of the filtering function, and then further study the internal mechanism of the Code.

First, let's take a look at the project description documentation. The structure of the project documentation is described below

  

This is the basic content of an open-source project.

2. LICENSE, you need to carefully consider which License to use. If your project is on fire in the future, you won't be confused because the License was not selected at the beginning, but you can't say anything about it.

In particular, many new users (like me) may not have chosen a License for their own projects. If you do not select a License for a project, it means that no one else can distribute or modify your project. However, you may use your software in your personal name or for commercial purposes. In addition, if you transfer a project without a License to Github, you will accept the Github Terms of Service Agreement by default-other users can view or fork your project.

This site introduces the differences between various license: https://choosealicense.com/licenses/

3. Readme. md: The first time you open this document, it looks like the following:

I went to the Internet and found out what the file with the md suffix is. Here:

Http://www.kuqin.com/shuoit/20141125/343459.html

How can I open it? I installed a plug-in the vs2015 extension:

 

This is what it looks like when you open the document again:

 

You can edit it on the left, and the editing effect is displayed on the right. It is really good.

README should be an overview of code source. In fact, this static file is a convention.

1. Project Introduction

2. What functions does the code implement?

3. How to use it? (System environment parameters and deployment elements)

4. What is the code organizational structure?

5. Important Summary of version updates

 

If your README includes the above content, when the user obtains the code and opens README, they will basically know how to start it.

 

The following is a created experience project:

Step 1: Create a console application

Step 2: Use the nuget Package Manager to reference:

1. EntityFramework 6.1.2

2. EntityFramework. DynamicFilters.2.6.0

Step 3: Add dbContext and model

Add DemoContext:

 public class DemoContext: DbContext    {        public static Guid CurrentAccountID { get; set; }        public DbSet<Account> Accounts { get; set; }        public DbSet<BlogEntry> BlogEntries { get; set; }        public DemoContext()        {            Database.SetInitializer(new ContentInitializer<DemoContext>());            Database.Log = log => System.Diagnostics.Debug.WriteLine(log);            Database.Initialize(false);        }        protected override void OnModelCreating(DbModelBuilder modelBuilder)        {            base.OnModelCreating(modelBuilder);        }    }    public class ContentInitializer<T> : DropCreateDatabaseAlways<T>       where T : DemoContext    {        protected override void Seed(T context)        {            System.Diagnostics.Debug.Print("Seeding db");            //  Seeds 2 accounts with 9 blog entries, 4 of which are deleted            var homer = new Account            {                UserName = "homer",                BlogEntries = new List<BlogEntry>                {                    new BlogEntry { Body="Homer's first blog entry", IsDeleted=false, IsActive=true, StringValue="1"},                    new BlogEntry { Body="Homer's second blog entry", IsDeleted=false, IsActive=true, StringValue="2"},                    new BlogEntry { Body="Homer's third blog entry (deleted)", IsDeleted=true, IsActive=true, StringValue="3"},                    new BlogEntry { Body="Homer's fourth blog entry (deleted)", IsDeleted=true, IsActive=true, StringValue="4"},                    new BlogEntry { Body="Homer's 5th blog entry (inactive)", IsDeleted=false, IsActive=false, StringValue="5"},                    new BlogEntry { Body="Homer's 6th blog entry (deleted and inactive)", IsDeleted=true, IsActive=false, StringValue="6"},                }            };            context.Accounts.Add(homer);            var bart = new Account            {                UserName = "bart",                BlogEntries = new List<BlogEntry>                {                    new BlogEntry { Body="Bart's first blog entry", IsDeleted=false, IsActive=true, StringValue="7"},                    new BlogEntry { Body="Bart's second blog entry", IsDeleted=false, IsActive=true, StringValue="8"},                    new BlogEntry { Body="Bart's third blog entry", IsDeleted=false, IsActive=true, StringValue="9"},                    new BlogEntry { Body="Bart's fourth blog entry (deleted)", IsDeleted=true, IsActive=true, StringValue="10"},                    new BlogEntry { Body="Bart's fifth blog entry (deleted)", IsDeleted=true, IsActive=true, StringValue="11"},                    new BlogEntry { Body="Bart's 6th blog entry (inactive)", IsDeleted=false, IsActive=false, StringValue="12"},                    new BlogEntry { Body="Bart's 7th blog entry (deleted and inactive)", IsDeleted=true, IsActive=false, StringValue="13"},                }            };            context.Accounts.Add(bart);            context.SaveChanges();        }    }

 

Add two entities:

 public class BlogEntry : ISoftDelete    {        [Key]        [Required]        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]        public Guid ID { get; set; }        public Account Account { get; set; }        public Guid AccountID { get; set; }        public string Body { get; set; }        public bool IsDeleted { get; set; }        public int? IntValue { get; set; }        public string StringValue { get; set; }        public DateTime? DateValue { get; set; }        public bool IsActive { get; set; }    }    public interface ISoftDelete    {        bool IsDeleted { get; set; }    }    public class Account    {        [Key]        [Required]        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]        public Guid ID { get; set; }        public string UserName { get; set; }        public ICollection<BlogEntry> BlogEntries { get; set; }        /// <summary>        /// Column used to verify handling of Entity properties mapped to different conceptual property names.        /// </summary>        [Column("RemappedDBProp")]        public bool RemappedEntityProp { get; set; }    }

 

Step 4: override protected override void OnModelCreating (DbModelBuilder modelBuilder) in dbcontext to build a dynamic Transmitter

 

protected override void OnModelCreating(DbModelBuilder modelBuilder)        {            base.OnModelCreating(modelBuilder);            modelBuilder.Filter("BlogEntryFilter", (BlogEntry b, Guid accountID, bool isDeleted) => (b.AccountID == accountID) && (b.IsDeleted == isDeleted),                                                () => CurrentAccountID, () => false);        }

A filter is constructed to automatically add conditions. The query data is not deleted by the software and only belongs to the data of the currently logged on user.

Call

var demoContext = new demo.DemoContext();            var allBlogEntries = demoContext.BlogEntries.ToList();            foreach (BlogEntry blogEntry in allBlogEntries)            {                Console.WriteLine(blogEntry.Body);            }

 

 

By reading: reademe. md in an open-source project, this dynamic filter is simply used for automatic query of ef entities.

 

Rafy was used by the company. At that time, we only knew how to use rafy's filtering conditions. Now we think it should be similar to this method. Like rafy's ghost plug-in, rafy just wanted to follow the conditions, whether it is convenient to configure dynamic query conditions. Previously, we only added plug-ins separately, but we did not consider the dynamic issue. Now ef's dynamic filtering is indeed quite convenient from global control.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.