My Hive Posts

    Getting Started with AspnetBoilerPlate

    Repository

    • https://github.com/aspnetboilerplate/aspnetboilerplate

    What Will I Learn?

    • You will learn about AspnetBoilerPlate
    • You will learn about How to get started with AspnetBoilerPlate
    • You will learn the basic structure of the Code Base in AspnetBoilerPlate
    • You will learn about creating Tasks in AspnetBoilerPlate

    Requirements

    • Basic Understanding of C#
    • Visual Studio 2017

    Difficulty

    • Basic

    Tutorial Contents

    What is AspnetBoilerPlate

    Whenever you have to develop an application, you have to think about designing the architecture of the application, the security, the performance and how to minimize the development effort. What if I say that all the above things are already done for you, you just have to add the business logic to get up and running with the modern day application.

    This is where ASP.NET Boilerplate comes into the picture, which is an application framework which implements best practices thus helps the developers to focus only on the development of the application and do not think about the architecture.

    How to get started with AspnetBoilerPlate

    To get started with ASP.NET Boiler Plate just head over to the https://www.aspnetboilerplate.com/Templates site and download the source code

    Basic structure of the Code Base in AspnetBoilerPlate

    When you download the Source Code you will be seeing the below structure of the Code Base. Now all of them are the different layers in the NLayered Architecture.

    Infrastructure Layer is the base layer of the framework where our ORM, Data Migrations, and other DB related tasks are done. In the ASPNETBoilerPlate it is done on the EntityFramework Core application. This is called infrastructure because it provides data to all other layers to be worked on.

    Domain Layer is the layer which has domain logic is written like the Entities which will talk to the Infrastructure Layer, the objects which will hold the value after fetching it from DB. The Core solution.

    Application Layer is the layer which acts as a link to fetch the values from the Domain Layer and pass it on to the frontend. It also helps in maintaining the authorization, session, caching etc. The application solution.

    Service Layer is the layer which is used to pass values if called as a REST Service. They mainly do not contain any logic, but just pass the values to the callee application. The Web.host solution.

    Presentation Layer is the layer which is shown to the end user and the end user interacts with it. The Web.MVC solution.

    The above are the NLayered architecture which sums up the whole application.

    Creating Tasks in AspnetBoilerPlate

    Since we know about the architecture, let's say if you want to build something like Steemit where you will store the Content in a database. For that you will be creating tasks, I will show how to do that.

    At first, you need to create a task in the Domain Layer i.e. .Core project. In the below code we have a created a SteemitPost which mainly contains three things Title, Body, and Tags. It is the simplest things you need when you create a Post. You can also see I have inherited the class CreationAuditedEntity because that will add which user has created this post and when.

    using System.Collections.Generic;
    using Abp.Domain.Entities.Auditing;
    
    namespace MyApp
    {
        public class SteemitPost: CreationAuditedEntity<int, User>
        {
            public virtual string Title { get; set; }
            public virtual string Body { get; set; }
            public virtual string Tags { get; set; }
    
            public SteemitPost(string title, string text, string tags)
            {
                Title = title;
                Body= text;
                Tags = tags;
            }
        }
    }
    

    Next thing is that we have to add it to our infrastructure project i.e. EntityFramework. So inside DbContext.cs add the below line which basically tells the ORM that whenever you create the Database create SteemitPost entity too.

    public virtual IDbSet<SteemitPost> SteemitPost { get; set; }
    

    Then we have to run the Update-Database command from the Package Manager Console which creates the Table i.e. SteemitPost.

    Now we have a ready Database which has the required table SteemitPost and contains three Columns Title, Body and Tags. Next is how we get all the posts or specific posts. For that, we have to write a Service in an .Application Layer.

    At first, we will be creating a DTO i.e. or Data Transfer Object

    using Abp.Application.Services.Dto;
    using Abp.AutoMapper;
    
    namespace MyApp
    {
        [AutoMapFrom(typeof(SteemitPost))]
        public class SteemitPostDto : CreationAuditedEntityDto
        {
            public string Title { get; set; }
            public string Text { get; set; }
            public string Tags { get; set; }
        }
    }
    

    Next is to define an interface for my DTO

    public interface ISteemitPostAppService : IApplicationService
        {
            PagedResultDto<SteemitPostDto> GetSteemitPosts(GetSteemitPostInput input);
        }
    

    And then writing the actual logic to get all the posts

    [AbpAuthorize]
        public class SteemitPostAppService : ApplicationService, ISteemitPostAppService
        {
            private readonly IRepository<SteemitPost> _SteemitPostRepository;
            public SteemitPostAppService(IRepository<SteemitPost> steemitPostRepository)
            {
                _steemitPostRepository= steemitPostRepository;
            }
    
            public PagedResultDto<SteemitPostDto> GetSteemitPosts(GetSteemitPostInput input)
            {
                var posts=
                    _steemitPostRepository
                        .GetAll()
                        .ToList();
    
                return new PagedResultDto<SteemitPostDto>
                       {
                           Items = posts.MapTo<List<SteemitPostDto>>()
                       };
            }
    }
    

    That's it done, now we just have to call the Application Layer from the Frontend to get all the Steemit Posts. It is just for basic understanding is to how easy is the development when you have a framework like AspNetBoilerPlate.