Monthly Archives: Mart 2016

Generate Custom SQL Operations On Dalyan Project

Introduction

Dalyan is a single page application project template using AngularJS, ASP.NET & Web API technologies. It is the base of a web application project. It has CRUD operation example with AngularJS. Additionally, WebAPI controllers and CRUD operations are created by T4 templates. You can download it from github source codes. This post describes step by step how to run this project and generate WebAPI CRUD Operations with T4 templates on Dalyan Project.

Generate Custom Database Operations

You can add CRUD operations with T4 template automaticly. Also you can add custom database operations to your project.

I will axplain the step by step how to add a new database operations.

The example is password change operation.

  1. Create ChangePassword Query and Query Handler
  2. Create ChangePassword Service
  3. Create ChangePassword WebAPI method

Create ChangePassword Query and Query Handler

Open Dalyan.Domain -> Query -> CustomQuery All db operations are in Dalyan.Domain project. Create a query class.

namespace Dalyan.Domain.Query
{
    using System;
    using Dalyan.Domain;
    using System.Linq;
    using System.Text;
    using Dalyan.Domain.Query;
    using System.Data;
    using System.Xml;
    using Dalyan.Db;
    using Entities.Models;
    using Dalyan.Entities.Enumerations;
    using AutoMapper;
    using System.Threading.Tasks;
    using System.Collections.Generic;

    public class ChangePasswordQuery : IQuery<bool>
    {
        public int Id { get; set; }
        public string Password { get; set; }
    }

    public class ChangePasswordQueryHandler : IQueryHandler<changepasswordquery, bool="">
    {
        private readonly DbEntities _db;
        public ChangePasswordQueryHandler()
        {
            _db = new DbEntities();
        }

        public bool Handler(ChangePasswordQuery query)
        {
            try
            {
                var obj = new Dalyan.Db.User();
                obj = _db.User.FirstOrDefault(x =&gt; x.Id == query.Id);
                obj.Password = query.Password;
                obj.UpdatedDate = DateTime.Now;
                _db.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                throw new ExceptionLog(LogType.DATABASE_UPDATE, LogLevel.ERROR, ex, &quot;ChangePasswordQuery&quot;);
            }
        }
    }

}

ChangePasswordQuery class have your parameters and used in handler method. The parameter values pass with query class to handler.

Create ChangePassword Service

WebAPI use service layer to access database so you have to add a service class to Dalyan.Service solution.

namespace Dalyan.Service.Services
{
    using System;
    using SimpleInjector;
    using Dalyan.Domain;
    using System.Linq;
    using System.Text;
    using Dalyan.Domain.Query;
    using System.Data;
    using System.Xml;
    using Dalyan.Entities.Models;
    using Dalyan.Entities.Enumerations;
    using System.Threading.Tasks;
    using System.Collections.Generic;
    using Entities.Interfaces;
    using Entities.Contracts;

    public class ChangePasswordService
    {
        private readonly Container _container;
        public ChangePasswordService(Container container)
        {
            _container = container;
        }

        public ServiceResult<string> ChangePassword(int Id, string Password)
        {
            try
            {
                IMediator service = _container.GetInstance<imediator>();
                var query = new ChangePasswordQuery { Id = Id, Password=Password };
                return new ServiceResult<string>(service.Proccess(query).ToString(), message: ClientErrorMessage.Success(), state: ServiceResultStates.SUCCESS);
            }
            catch (ExceptionLog ex)
            {
                LoggerService.Logger.Log(_container, ex);
                return new ServiceResult<string>(result: &quot;false&quot;, message: ClientErrorMessage.Error(), state: ServiceResultStates.ERROR);
            }
        }
    }
}

We use mediator pattern and our handlers execute automaticly with mediator class with IoC.

Create ChangePassword WebAPI method

The last step is create WebAPI method. SPA project uses WebAPI methods to access database. You can add the method any controller.

    [ClientAuthorize]
    [HttpPost]
    public ServiceResult<string> ChangePassword(string password)
    {
        ChangePasswordService service = new ChangePasswordService(_container);
        IUserContext currentUser = _container.GetInstance<iusercontext>();
        return service.ChangePassword(currentUser.CurrentUserIdentity.UserID, password);
    }

Points of Interest

You can develop your project on Dalyan Web Application Template.