Ehsan Ghanbari

Experience, DotNet, Solutions

Asp.net core razor pages with a simple command, query handler

I'm already working on a project and a challenging decision has been made to use asp.net core razor pages. Based on Microsoft definition:" ASP.NET Core Razor Pages is a page-focused framework for building dynamic, data-driven web sites with clean separation of concerns." I've found razor pages so cool so far as it's totally compatible with the architecture of the current project of mine. I'm using a simple command query (it's not CQRS) in this project. Once I shared another architectural pattern about Command and query over here. Take a deep look at the following simple base classes and interfaces:

 

 public interface ICommand<out TResult> { }

   public interface ICommandHandler<in TCommand, out TResult> where TCommand : 
   ICommand<TResult>
    {
        TResult Execute();
    }

    public class CommandResult
    {
        public bool Faild { get; set; }

        public string Message { get; set; }
     }

    public interface IQuery<out TResponse> { }

    public interface IQueryHandler<in TQuery, out TResponse> where TQuery :  
    Query<TResponse>
     {
        TResponse Get();
     }

 

I'm sharing these classes in one code snippet, but Commands and Queries will be lived in different assemblies although the above classes are bases and could be in an infrastructural assembly as well. Let's create a query sample. For example for querying a user detail, firstly I should create the related QueryView:

 


    public class UserDetailQueryView 
    {
        public int Id { get; set; }
        public string Username { get; set; }
    }

 

And then it's Query class turn. Note that the query class is just like Request in Request/Response pattern.

 

    public class UserDetailQuery : IQuery<UserDetailQueryView>
    {
        public long UserId { get; private set; }

        public UserDetailQuery(long userId)
        {
            UserId = userId;
        }
     }

 

And then create the handler of the above Query:

 

  
   internal class UserDetailQueryHandler : IQueryHandler<UserDetailQuery, 
   UserDetailQueryView>
    {
        private readonly UserDetailQuery _userDetailQuery;
     
        public UserDetailQueryHandler(UserDetailQuery userDetailQuery)
        {
            _userDetailQuery = userDetailQuery;
        }

        public UserDetailQueryView Get()
        {
            var user = MyDdContext.Users.GetById(_userDetailQuery.UserId);
            return new UserDetailQueryView { Username = user.Username, Id = user.Id };       
        }
    }

 

As you know, it's not a good idea to directly call EF DbContext to fetch data, it's just a sample code I'm sharing over here. And finally let's aggregate the queries in one Factory class  like this:

 

    public static class UserQueryFactory
    {
        public static IQueryHandler<UserDetailQuery, UserDetailQueryView> 
        Build(UserDetailQuery query)
        {
            return new UserDetailQueryHandler(query);
        }
     }

 

Now add a Razor page just like it's depicted below:

 

 

Now you can call the UserQueryFactory class in the razor page that we have created:

 


    public class DetailModel : PageModel
    {
        public int UserId { get; set; }

        public UserDetailQueryView UserDetailQueryView { get; set; }

        public IActionResult OnGet()
        {
            var query = new UserDetailQuery(UserId);
            var queryHandler = UserQueryFactory.Build(query);
            UserDetailQueryView = queryHandler.Get();

            if (UserDetailQueryView == null)
            {
                return NotFound();
            }

            return Page();
        }
     }

 

Now if you are interested to know how I'm using Commands, stay with me to create another command sample. A command is something the changes the state of an object. Such an example, ChangePassword is a command. Let's create it.

 


    public class ChangePasswordCommand : ICommand<CommandResult>
    {
        public string Username { get; set; }

        public string OldPassword { get; set; }

        public string NewPassword { get; set; }

        public string ConfirmPassword { get; set; }
     }

 

And again we should create a handler for the above command:

 

   internal class UserCommandHandler : ICommandHandler<ChangePasswordCommand, 
   CommandResult>
    {
        public CommandResult Execute()
        {
            var commandResult = new CommandResult();

            try
            {
                //fetch data from database and change the password
                
            }
            catch (Exception exception)
            {
                commandResult.Faild = true;
            }

            return commandResult;
        }
     }

 

And finally, just like query, let's create a factory for having all of the handlers in one place:

 

    public static class UserCommandFactory
    {
        public static ICommandHandler<ChangePasswordCommand, CommandResult> 
        Build(ChangePasswordCommand command)
        {
            return new UserCommandHandler();
        }
     }

 

Create another razor page for ChangePassword and in the class create with the same name, add the following command for changing the password:

 

    public class ChangePasswordModel : PageModel
    {
        [BindProperty]
        public string Username { get; set; }

        [BindProperty]
        public string OldPassword { get; set; }

        [BindProperty]
        public string NewPassword { get; set; }

        [BindProperty]
        public string ConfirmPassword { get; set; }

        public IActionResult OnPost()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            var changePasswordCommand = new ChangePasswordCommand
            {
                ConfirmPassword = ConfirmPassword,
                NewPassword = NewPassword,
                OldPassword = OldPassword,
                Username = Username
            };

            var commandHandler = 
            UserCommandFactory.Build(changePasswordCommand);
            var commandResult = commandHandler.Execute();

            if(commandResult.Faild)
            {
                //redirect to error page or return the message
            }

            return RedirectToPage("./Index");
        }
     }

 

Voila!



Register EF Core DBContext in data layer assembly

In Asp.net core samples, you always see the AddDbContext method In services to register Entity framework DbContext. I mean something like below:

 

  public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddApiVersioning(o => o.ApiVersionReader = new HeaderApiVersionReader("api-version"));
            services.AddDbContext<BaseDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyConnection")));        
        }
}

 

Suppose that we have an architecture that forces us to have loosely coupled services. For example, our EF DbContext lives in the Data Access layer and  the presentation layer should not know anything about that as it is talking to a service layer:

  1. Presentation
  2. Service
  3. Data Access

In order to register each of the above configurations in its own layer, we should create an extension method over IServiceCollection :

 

public static class DataRegistery
    {
        public static IServiceCollection RegisterData(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddScoped(typeof(IRepository<>), typeof(Repository<>));

            services.AddEntityFrameworkSqlServer().AddDbContext<DataContext>(options =>
            {
                options.UseSqlServer(configuration.GetConnectionString("MyConnection"));
            });

            return services;
        }
    }

 

And we can register this in Service layer configuration :

 

 public static class ServiceRegistery
    {
        public static IServiceCollection RegisterService(this IServiceCollection services, IConfiguration configuration)
        {
            services.RegisterData(configuration);

            return services;
        }
    }

 

And finally in asp.net core Startup class:

 

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
            services.RegisterService(Configuration);
        }
}

 

Cheers!



About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
Peridic pattern
How to use PagedList In asp.net MVC
How to query over Icollection<> of a type with linq
Domain driven design VS model driven architecture
What's the DDD-lite?
Redis as a cache server