dimanche 28 mars 2021

How to implement multilayer in asp.net core web api when using repostiry pattern?

I am very new to programming actually I have different layer in project basically I want to to perform database related operation in separate data access layer not in repository concrete class, I have searched on google to solve my problem but could not find any help to solve my problem, sorry about my weak English.

webApi(Controller)

    [HttpPost]
    [Route("api/account/Login")]
    public int Login(LoginViewModel model)
    {          
            var result = _accountRepository.Login(model);
            return result;         
    }

Repository (in another library project)

 public class AccountRepository : IAccountRepository
        {
         public int Login(LoginViewModel model)
            {           

// **this below database related operation I want to move into another library(Data access layer)**

            var p = new DynamicParameters();
            p.Add("@pLogin", model.Email);
            p.Add("@pPassword", model.Password);
            var result = _dapper.Get<int>("[uspLoginUser]", p, commandType: CommandType.StoredProcedure);
            return result;
               
            }
       }

this is what I want to do in repository pattern to call another function which would be in another layer called data access.

public class AccountRepository : IAccountRepository
{
    public int Login(LoginViewModel model)
        {
           
                var result = _AccountAction.LoginUser(model);
                return result;
           
        }
  }

Data Access layer

public class AccountAction
    {
        private readonly IDBHelper _dapper;       
        public AccountAction(IDBHelper dapper)
        {
            _dapper = dapper;
        }
        
        public int LoginUser(LoginViewModel model)
        {
            var p = new DynamicParameters();
            p.Add("@pLogin", model.Email);
            p.Add("@pPassword", model.Password);
            var result = _dapper.Get<int>("[uspLoginUser]", p, commandType: CommandType.StoredProcedure);
            return result;

        }
    }

DBHelper

public interface IDBHelper : IDisposable  
    {
        DbConnection GetConnection();
        T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
        List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
        int Execute(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
        T Insert<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
        T Update<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure);
    }

Aucun commentaire:

Enregistrer un commentaire