Recently, a brief look at the wojilu comment function implementation, the author introduces the article (http://www.wojilu.com/Common/Page/52)
FileComment: ObjectBase <FileComment>, IComment
FileComment implements the IComment interface, which defines the behavior of a comment object.
In addition, FileCommentController: CommentController <FileComment>.
Public class CommentController <T>: ControllerBase where T: ObjectBase <T>, IComment
{
Public ICommentService <T> commentService {get; set ;}
}
CommentController combines CommentService <T> objects to manage comments.
Read the Create method in CommentController and Create comments.
[HttpPost, DbTransaction]
Public virtual void Create (int postId ){
......
IComment comment = Validate (postId); // verify and get a comment object
......
Result result = commentService. Insert (comment, lnkTarget); // Save the comment object to the database.
......
}
Public IComment Validate (int postId ){
IComment comment = Entity. New (typeof (T). FullName) as IComment; // generate a comment object
......
Comment. RootId = postId;
Comment. ParentId = ctx. PostInt ("ParentId ");
Comment. AppId = ctx. app. Id;
Comment. Author = userName;
Comment. Content = content;
Comment. Ip = ctx. Ip;
Comment. Created = DateTime. Now;
If (ctx. viewer. IsLogin ){
Comment. Member = (User) ctx. viewer. obj;
}
Return comment;
}
I learned a lot from this code, deepened my understanding of interface usage, and understood the concept of generic constraints.
IComment comment = Entity. New (typeof (T). FullName) as IComment; // generate a comment object
Now, you have time to learn more.