ABP Starter Series (3)--Domain layer creation entity

Source: Internet
Author: User
In this section we deal primarily with the domain level. First, we will make one by one correspondence to the ABP architecture and the solution created from the template. There is a code generator on the Internet to simplify this step of the task, but not recommended for beginners to use.





First, take a look at ABP architecture






The domain layer is the business layer, the core of a project, and all business rules should be implemented at the domain level.
Entity: The entity represents the data and operations of the business domain, in practice, by mapping it to a database table.
Warehousing (Repository): warehousing is used to manipulate databases for data access. Warehousing interfaces are defined in the domain layer, and the implementation class for warehousing should be written on the infrastructure layer.
Domain Service: When a business rule is processed that spans two (and more) entities, it should be written in the domain service method.
Domain event: Domain events can be triggered when certain situations occur at the domain level, and they are captured and processed in the appropriate place.
Unit of work: a work cell is a design pattern that maintains a list of business objects that have been modified (such as additions, deletions, and updates). It is responsible for coordinating the persistence of these business objects and concurrency issues.



Second, to see the solution






Once we have determined the level of each project under the solution, we begin to create a task entity.



Iii. Creating a task entity



1. Create the Tasks folder at the domain level and create a task entity class;
All entity classes in 2.ABP inherit from entity, and entity implements the IEntity interface, whereas the IEntity interface is a generic interface that specifies the primary key ID type by generic type, and the default entity primary key type is int.
Creating a task is sure to save the creation time, which can be achieved by implementing the Ihascreationtime in the audit module. The code is as follows:


namespace LearningMpaAbp.Tasks{   

 public class Task : Entity, IHasCreationTime    

 {       

 public const int MaxTitleLength = 256;        

 public const int MaxDescriptionLength = 64 * 1024;//64kb                

 public long? AssignedPersonId { get; set; }        

 [ForeignKey("AssignedPersonId")]        

 public User AssignedPerson { get; set; }        

 [Required]        

 [MaxLength(MaxTitleLength)]        

 public string Title { get; set; }        

 [Required]        

 [MaxLength(MaxDescriptionLength)]        

 public string Description { get; set; }        

 public TaskState State { get; set; }        

 public DateTime CreationTime { get; set; }        

 public Task()        

 {            

 CreationTime = Clock.Now;            

 State = TaskState.Open; ;        

 }        

 public Task(string title, string description = null) : this()        

 {            

 Title = title;            

 Description = description;       

  }    

  }    

  public enum TaskState : byte    

  {        

  Open = 0,        Completed = 1    

  }}


Which defines the Taskstate state enumeration. The Assignedperson navigation property is added to save the assigned task to a user. where [Required], [MaxLength] attributes are used for input validation.



3. After defining the entity, we are going to define the Dbset for the entity in DbContext to apply Code First Data migration. Find our base service layer, which is the project ending in EntityFramework, locate the DbContext class and add the following code


Todo:define an idbset for your entities ... public idbset<task> Tasks {get; set;}


4. Perform Code First Data migration.



After you open the Package Manager console, the default project selects EntityFramework corresponding to the project. Perform add-migration add_task_entity to create the migration.



After a successful creation, a class file of time _add_task_entity format is created under the Migrations folder. If you observe, we will find a migrations folder under the Seeddata folder, as the name implies, this folder of the class is mainly used to pre-set the seed data. We can pre-provision two tasks with reference to the existing class notation. Create the Defaulttestdatafortask class with the following code:


namespace LearningMpaAbp.Migrations.SeedData{public class DefaultTestDataForTask{    

private readonly LearningMpaAbpDbContext _context;    

private static readonly List<Task> _tasks;    

public DefaultTestDataForTask(LearningMpaAbpDbContext context)    

{        

_context = context;    

}    

static DefaultTestDataForTask()    

{        

_tasks = new List<Task>()        

{            

new Task("Learning ABP deom", "Learning how to use abp framework to build a MPA application."),            

new Task("Make Lunch", "Cook 2 dishs")        

};    

}   

public void Create()    

{      

  foreach (var task in _tasks)       

   {          

    _context.Tasks.Add(task);         

       _context.SaveChanges();      

         }    

         }

         }

         }


Then, in the seed method in the configuration class, add the following code.


New Defaulttestdatafortask (context). Create ();


In the Package Manager console, enter Update-database, and return to perform the migration. After successful execution, view the database, the Tasks table was created successfully, and two test data already exists in the table.



At this point, the task entity class was successfully created.
Source has been uploaded to GITHUB-LEARNINGMPAABP, can be self-reference.
ABP Starter Series Catalogue-A practical walkthrough of learning ABP Framework



The above is the ABP Primer series (3)-the domain layer to create the content of the entity, more relevant content please follow topic.alibabacloud.com (www.php.cn)!


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