Entity Framework, entityframework

Source: Internet
Author: User

Entity Framework, entityframework

Because EF is not applicable to zookeeper lookup, we should use the "lookup" and "command" scoring methods to handle the problem.

In the query examples section, the native SQL statement is used for optimization, while in the command section, the OO method is used together with EF FOR THE ORM framework.

In addition, the Repository mode is used for integration to decouple the user end.

For example, there are two requirements for a single operating system.

The operation part is to update a ticket

The check result section shows the number of orders and their quota.

 

Update ticket quota

Operate some programs

public void InserOrderItem(int orderId, int productId, double price, ...)        {            using (NorthwindContext context = new NorthwindContext())            {                var order = context.Orders.First(o => o.Id == orderId);                                Order.InserItem(productId, price, ...);                ....                context.SaveChanges();            }        }

The above code is an example of easy-to-use code. In this code, we can see that the Code is a string of queries and CRUD operations, generally, too many query methods are not allowed in commands. If so, you should check whether the design method is correct.

 

Check some program downloads

public IEnumerable<ResultModel> QueryOrders(...//where pararmters)        {            using (NorthwindContext context = new NorthwindContext())            {                string sql = @"SELECT Order.Id,Order.No,COUNT(*) AS ItemCountFROM OrderJOIN OrderItem ON OrderItem.OrderId=Order.IdWHERE ...GROUP BY Order.Id";                .....                 var context = ((IObjectContextAdapter)_dbContext).ObjectContext;                return context.ExecuteStoreQuery<ResultModel>(sql, ...));            }        }

QueryOrders is a query function. The query result field (Where). ResultModel is a fixed object.

In this example, we use EF's SQL Query machine to directly run the explain SQL statement method. The string SQL statement in the program is SQL command, in most data-driven SQL statements, this example shows better performance, in this case, Zookeeper should optimize the processing of queries through Native SQL, I should not solve this issue through LINQ in the checking cases, because it is very difficult (or should not be in the checking period) why are SQL statements produced by using LINQ.

 

In architecture, the Repository mode is usually used to integrate these data access operations.

For example, the above two requirements have redefined the following Repository type

Public class OrderRepository: RepositoryBase <Order> {public Order First (...) // returns the RepositoryBase public IEnumerable <ResultDto> QueryOrders (...){...}}

Generally, some general CRUD operations can be used to design a set of bottom-layer libraries in the generic mode, such as the above RepositoryBase <T>, I am currently using a case study https://repository.codeplex.com/

In this case, the user does not need to understand whether the back-end is implemented through EF or SQL, but the back-end accesses the envelope. It is also beneficial to UnitTest.

 

However, the above practice may have a problem, because QueryOrders queries allow users to design requirements, such as "orders tables ", in the preceding example table, multiple queries are usually performed, and the result of each query is a reverse design for the query. For example, the above ResultModel, so it will be a complicated and troublesome thing to define these products.

However, we can use a universal object to replace a DataTable, however, the missing Point of the DataTable is non-linear, and it is easy to generate a category that cannot be found during usage. in addition, the able does not combine in some architectures as an example object. For example, you need to configure a Service for other users (Java or other languages ).

Therefore, this solution provides a better method-Anonymous

public IEnumerable<T> QueryOrders<T>(T sample, ...//where pararmters)        {            using (NorthwindContext context = new NorthwindContext())            {                string sql = @"SELECT Order.Id,Order.No,COUNT(*) AS ItemCountFROM OrderJOIN OrderItem ON OrderItem.OrderId=Order.IdWHERE ...GROUP BY Order.Id";                .....                 var context = ((IObjectContextAdapter)_dbContext).ObjectContext;                return context.ExecuteStoreQuery<T>(sql, ...));            }        }

Let's take a look at the end-to-end method.

var repository = ... //get repository
var result = repository.QueryOrders(new {                Id = 0,                No = "",                ItemCount = 0            }, ....);            result.Id...            result.No...

In the QueryOrders batch, a variant generic declaration method is used. The returned results are defined by the end-to-end, but not anonymous. in this way, different query results are not required for each query.

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.