EF architecture ~ The path to EF asynchronous Transformation ~ Warehouse interface transformation, ef Architecture

Source: Internet
Author: User

EF architecture ~ The path to EF asynchronous Transformation ~ Warehouse interface transformation, ef Architecture

Back to directory

C #5.0 brings parallel programming

{C #1.0 hosting code → C #2.0 generic → C # 3.0LINQ → C #4.0 dynamic language → C #5.0 asynchronous programming}

With C #5.0 in. after net4.5 came out, their parallel technologies gradually became increasingly popular. This heat has been passed to me. As a warehousing uncle, I must also perform parallel transformation on my own warehousing, this is the trend of the times. the Core project is expanded, that is, some corresponding parallel interfaces are added, and then my parallel (asynchronous) warehousing is used to implement it. In fact ,. net ef, asynchronous (parallel) implementation is very easy, in C #5.0 due to the emergence of async/await keywords, this makes asynchronous implementation easier, huh, huh, or that sentence,. net programmers, I feel happy!

IRepository becomes IRepositoryAsync
/// <Summary> /// Asynchronous Operation // basic data operation specifications /// irrelevant to the ORM architecture /// </summary> /// <typeparam name = "TEntity"> </typeparam> public interface IRepositoryAsync <TEntity> where TEntity: class {// <summary> // sets the data context, it is usually injected by the architecture method /// </summary> /// <param name = "unitOfWork"> </param> void SetDbContext (IUnitOfWork unitOfWork ); /// <summary> /// add an object and submit it to the data server // </summary> /// <param name = "item"> Item to add to repository </param> Task Insert (TEntity item ); /// <summary> /// remove an object and submit it to the data server // If the table has constraints, delete the sub-table information /// </summary> /// <param name = "item"> Item to Delete </param> Task delete (TEntity item ); /// <summary> /// modify the object and submit it to the data server // </summary> /// <param name = "item"> </param> Task Update (TEntity item ); /// <summary> /// get the specified object set (delayed result set) /// Get all elements of type {T} in repository // </summary> // <returns> List of selected elements </returns> IQueryable <TEntity> GetModel (); /// <summary> /// obtain the object based on the primary key /// </summary> /// <param name = "id"> </param> /// <returns ></returns> TEntity Find (params object [] id );}
IExtensionRepository becomes IExtensionRepositoryAsync
/// <Summary> /// Asynchronous Operation // extended Repository operation specification /// </summary> public interface IExtensionRepositoryAsync <TEntity>: IRepositoryAsync <TEntity>, IOrderableRepository <TEntity> where TEntity: class {// <summary> // Add a set. [This method is used when the number of sets is small, use BulkInsert for a large collection] /// </summary> /// <param name = "item"> </param> Task Insert (IEnumerable <TEntity> item ); /// <summary> /// modify the set [this method is used when the number of sets is small, use BulkUpdate for a large collection] /// </summary> /// <param name = "item"> </param> Task Update (IEnumerable <TEntity> item ); /// <summary> /// delete a set [this method is used when the number of sets is small, delete large sets in batches] /// </summary> /// <param name = "item"> </param> Task Delete (IEnumerable <TEntity> item ); /// <summary> /// Based on the specified lambda expression, get the latency result set /// </summary> /// <param name = "predicate"> </param> /// <returns> </returns> IQueryable <TEntity> getModel (Expression <Func <TEntity, bool> predicate); // <summary> // Based on the specified lambda expression, obtain the first object /// </summary> /// <param name = "predicate"> </param> /// <returns> </returns> TEntity Find (Expression <Func <TEntity, bool> predicate); /// <summary> /// batch Add. You can remove the auto-increment Attribute before adding it, no removal by default /// </summary> /// <param name = "item"> </param> /// <param name = "isRemoveIdentity"> </param> task BulkInsert (IEnumerable <TEntity> item, bool isRemoveIdentity ); /// <summary> /// batch add /// </summary> /// <param name = "item"> </param> Task BulkInsert (IEnumerable <TEntity> item ); /// <summary> /// batch update /// </summary> /// <param name = "item"> </param> Task BulkUpdate (IEnumerable <TEntity> item, params string [] fieldParams ); /// <summary> /// batch Delete /// </summary> /// <param name = "item"> </param> Task BulkDelete (IEnumerable <TEntity> item ); /// <summary> /// save it /// </summary> event Action <SavedEventArgs> AfterSaved; /// <summary >/// before saving /// </summary> event Action <SavedEventArgs> BeforeSaved ;}
ISpecificationRepository is changed to ISpecificationRepositoryAsync
/// <Summary> /// EF underlying architecture, about the storage interface of the Statute function // </summary> /// <typeparam name = "TEntity"> </typeparam> public interface ISpecificationRepositoryAsync <TEntity>: IExtensionRepositoryAsync <TEntity> where TEntity: class {// <summary> // according to the specified protocol, get the delayed result set /// </summary> /// <param name = "specification"> </param> /// <returns> </returns> IQueryable <TEntity> getModel (ISpecification <TEntity> specification ); /// <summary> /// according to the specified protocol, obtain the first object /// </summary> /// <param name = "specification"> </param> /// <returns> </returns> TEntity Find (ISpecification <TEntity> specification ); /// <summary> /// if the sorting function is enabled, expected result set /// </summary> /// <param name = "orderBy"> </param> /// <param name = "specification"> </param> /// <returns> </returns> IQueryable <TEntity> GetModel (Action <IOrderable <TEntity> orderBy, ISpecification <TEntity> specification );}

As you can see, Uncle warehouse's Asynchronous Operation interfaces all end with Async, which also complies with Microsoft's specifications, such as synchronous ToList () after the method is changed to async, it becomes ToListAsync (). Instead of modifying the original interface, Zhan extends the asynchronous interface, and the method name is not changed.

It is more friendly and acceptable, that is, whether our CUD operation is (Insert, Update, Delete), just differentiate on the interface, you use the IRepository interface to declare the instance, A synchronization operation is generated, and an Instance declared by IRepositoryAsync is implemented asynchronously.

Back to directory

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.