Do not forget this ~ Abstract class

Source: Internet
Author: User

Returned directory

Preface

This abstract class has also been introduced in my previous articles. The abstract class in the "Don't forget" series will mainly introduce its concept and its differences with interfaces.

Concept:

Abstract classes have their own abstract identifiers. abstract methods can appear in abstract classes. They can only act as parent classes, in actual production, it is implemented by subclass, that is, the abstract class cannot be instantiated. The parent class mentioned above is often called its base class. For example, the controller of your WEB layer may need a base class to store common attributes and methods. At this time, abstract classes are the best choice. There are many such examples in frameworks, such as System. web. mvc. controller is an abstract Class, which consists of a group of methods and attribute components related to the Controller.

Abstract classes in the project:

In our implementation project, I advocate that each layer should have its own base class to store common things, such as BaseController in the controller and BaseBll In The BLL layer, the RepositoryBase in the data layer and so on. They are the parent classes of other function classes.

Differences from interfaces: in terms of concept:

An interface is used to constrain a group of behaviors. objects that implement an interface have no essential connection before. It is a behavior specification or an identifier. Through an interface, we can implement polymorphism!

Abstract class is an abstraction of some associated objects. It abstracts the public information of the associated objects and puts them in a class. This class is often called an abstract class and is marked with abstract.

From the code perspective:

An interface is a set of behavior specifications. Look at a simple warehousing interface.

                     IRepository<TEntity>            TEntity :                                                                                                             IQueryable<TEntity>                                    TEntity Find( 

It calls each specific warehousing interface to implement it. For example, IUserRepository is the user's persistent interface, and it may also be implemented by the persistence base class, such as DbContextRepository, it is a base class that uses ef for persistence. Of course, you can use MemoryContextRepository to implement the IRepository interface. Of course, its function is to use memory tables for persistence.

Shows the code of the abstract class:

Worker <TEntity> TEntity: IExtensionRepository <TEntity> member Insert (IEnumerable <TEntity> Update (IEnumerable <TEntity> Delete (IEnumerable <TEntity> Update <T> (System. linq. expressions. expression <Action <T> entity) T: IQueryable <TEntity> GetModel (EntityFrameworks. entity. core. specification. ISpecification <TEntity> IQueryable <TEntity> GetModel (System. linq. expressions. expression <Func <TEntity,> TEntity Find (System. linq. expressions. expression <Func <TEntity,> TEntity Find (EntityFrameworks. entity. core. specification. ISpecification <TEntity> BulkInsert (IEnumerable <TEntity> item, BulkInsert (IEnumerable <TEntity> BulkUpdate (optional <TEntity> item, BulkDelete (IEnumerable <TEntity> Action <entiframeworks. entity. core. savedEventArgs> Action <EntityFrameworks. entity. core. savedEventArgs> IOrderableRepository member IQueryable <TEntity> GetModel (Action <EntityFrameworks. entity. core. orderable <TEntity> IQueryable <TEntity> GetModel (Action <EntityFrameworks. entity. core. orderable <TEntity> orderBy, EntityFrameworks. entity. core. specification. ISpecification <TEntity> IQueryable <TEntity> GetModel (Action <EntityFrameworks. entity. core. orderable <TEntity> orderBy, System. linq. expressions. expression <Func <TEntity,> IRepository <TEntity> member IQueryable <TEntity> TEntity Find (

The abstract class above is just a DEMO, so the logic for implementing persistence is not implemented. Of course, this is not the focus. The point is that if your specific warehouse inherits it, objects will be persisted in DemoContextRepository mode.

Returned 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.