Consume API in Repository Design Pattern

API Controller

The provided C# code is a controller method in an ASP.NET application. It receives a POST request, decrypts a string in the request, deserializes it into a FilterPropertyModel, and retrieves a property using a repository pattern. If an exception occurs, it logs the error and returns null. Here's a breakdown and some suggestions for improvement.

[HttpPost]
public CommonModel Getdemo(GetModel objs)
{
    response _response = new response();
    CommonModel _CommonModel = new CommonModel();
    try
    {
        objs.Getrequestresponse = mobjCryptography.StringDecrypt(objs.Getrequestresponse);
        FilModel obj = JsonConvert.DeserializeObject<FilModel>(objs.Getrequestresponse);
        _CommonModel = _ProRespository.Getdemo(obj);
        return _CommonModel;
    }
    catch (Exception ex)
    {
       
        return null;
    }
}

Classbusiness

method GetProperty retrieves data from a database, processes it, and then returns a CommonModel object containing the processed data. It does so by utilizing a DataSet to hold the results from a database query and then mapping those results to various model objects. Here's a breakdown and potential improvements for better readability, error handling, and efficiency.

public CommonModel Getdemo(string obj)
{
    DataSet dsDload = new DataSet();
    FilModel _response = new FilModel();
    CommonModel _CommonModel = new CommonModel();
    
    try
    {
        dsDload = ClsDal.Qry_WithDataSet(obj);
        
        if (dsDload != null)
        {
            if (dsDload.Tables.Count > 0)
            {
                if (dsDload.Tables[0].Rows.Count > 0)
                {
                    _response._FilList = dsDload.Tables[0].AsEnumerable().Select(DataRow => new FilterPropertyModel()
                    {
                        Id = Convert.ToString(DataRow.Field<int>("Id")),
                        Name = DataRow.Field<string>("Name"),
                        tionName = DataRow.Field<string>("tionName"),
                        stomerGroup = DataRow.Field<string>("stomerGroup"),
                        Amount = DataRow.Field<decimal?>("Amount") != null ? DataRow.Field<decimal>("Amount") : 0,
                        Image = DataRow.Field<string>("Image"),
                        ges = DataRow.Field<string>("ges"),
                        Occup = DataRow.Field<string>("Occup "),
                    }).ToList();
                }
                else
                {
                    _response._FilList = new List<FilModel>();
                }
                
                if (dsDload.Tables[1].Rows.Count > 0)
                {
                    _response._FilList = dsDload.Tables[1].AsEnumerable().Select(DataRow => new FilterLocationModel()
                    {
                        Id = DataRow.Field<int>("Id"),
                        Name = DataRow.Field<string>("Name"),
                        Status = DataRow.Field<string>("Status"),
                    }).ToList();
                }
                else
                {
                    _response._FilList = new List<FilModel>();
                }
                
                if (dsDload.Tables[2].Rows.Count > 0)
                {
                    _response._GetType = dsDload.Tables[2].AsEnumerable().Select(DataRow => new TypeModel()
                    {
                        Id = DataRow.Field<int>("Id"),
                        TypeName = DataRow.Field<string>("TypeName"),
                    }).ToList();
                }
                else
                {
                    _response._GetType = new List<TypeModel>();
                }
                
                var Reqjson = new JavaScriptSerializer().Serialize(_response);              
                Reqjson = mobjCryptography.StringEncrypt(Reqjson);
                _CommonModel.Postresponse = Reqjson;
                return _CommonModel;
            }
            return null;
        }
        else
        {
            return null;
        }
    }
    catch (Exception ex)
    {
       
        return null;
    }
}

Repository

A method named GetProperty which takes a FilterPropertyModel object as input and returns a CommonModel object. The method sets default values for CityId and LocationId if they are null or empty, constructs a parameter string, logs an error message, calls a stored procedure with those parameters, and catches any exceptions that occur.

public CommonModel Getdemo(FilModel obj)
{
    CommonModel _CommonModel = new CommonModel();
    obj.Id = string.IsNullOrEmpty(obj.Id) ? "0" : obj.Id;
    obj.tionId = string.IsNullOrEmpty(obj.tionId) ? "0" : obj.tionId;

    string parameter = "'" + obj.Id + "','" + obj.tionId + "'";
    try
    {
       
        _CommonModel = _ClsBusiness.Getdemo("CALL sp name ('" + obj.Id + "','" + obj.tionId + "')");

        return _CommonModel;
    }
    catch (Exception ex)
    {
       
        return null;
    }
}

IRepository

The CommonModel GetProperty(FilterPropertyModel obj) method likely represents a function in a C# application that retrieves a CommonModel object based on criteria defined in a FilterPropertyModel object. Let's break down what each component might involve and how to implement this in a typical C# environment.

CommonModel Getdemo(FilModel obj);


Similar Articles