Skip to content
Sukanta Choudhury edited this page Apr 17, 2023 · 6 revisions

Introduction

Smart API is a Development automation framework which can reduce development effort up to 40%. With the use of Smart API, developer do not need to write code for Business & Data access layer. The framework contain both the layer in built. The Business layer of the SmartAPI have in-built implementation of AF.DataValidator component which can handle all static data validation.

The framework can be used in any Dot net projects.

Limitation

The in-built Data access layer of SmartAPI use AF.DataAccessor to connect database. The AF.DataAccessor only support Sql server. Hence, SmartAPI is compatible with sql server only.

How to get ?

Install nuget package AF.SmartAPI.

Step by Step implementation of AF.SmartAPI

First, Open up your visual studio to create a Console Application. I have used Visual studio 2022, you can use other version of visual studio as well.

Now, Install nuget package AF.SmartAPI from nuget Gallery.

install-package AF.SmartAPI

The SmartAPI has dependency on other two nuget package, AF.DataAccessor and AF.DataValidator. If you install SmartAPI from nuget, then it will install dependent component as well.

Once the package got installed, it will add below JSON files in the project

  • AFAuditTrail.json
  • AFSmartAPISetting.json

Right click on these two files and set below property:

  • Build Action: Content
  • Copy to Output Directory : Copy always

This package uses AF.DataAccessor nuget package to connect to sql database.Hence configure AFDataAccessor.json file with sql connection string. For more information on AF.DataAccessor & it's configuration check here

Open the file AFSmartAPISetting.json and set section AFConnectionStringName.The AFConnectionStringName is the connection string key defined in file AFDataAccessor.json.

Now, Add nuget package System.Data.SqlClient 4.8.5 to the project.

With SmartAPI, we do not need to write code for Business & Data access layer.

So how it validate data or map store procedure with DTO object ?

The Business layer of SmartAPI internally call AF.DataValidator which handle all static data validation. For this, the DTO object needs to be decorated with validation rules. If you do not decorate DTO with validation rule, then SmartAPI do not carried out static data validation for CRUD operations. For more information & Step by Step implementation of AF.DataValidator, Check out AF.DataValidator.

The Data access layer of SmartAPI map DTO with Store procedure for all CRUD operations. So we need to decorate DTO class.

Steps for Insert/Update/Delete operations

Add DTO class name SmartAPIEntity and add required properties. In this example, i have created employee module.

Now implement interface IAFWriteOperations in DTO class. The interface IAFWriteOperations is required for Insert/Update/Delete operations. If you want to use static data validation feature, then implement IAFValidation interface in DTO class.

The interface IAFWriteOperations will add property Operation in DTO class. This property needs to set before passing the DTO object to SmartAPI. Through this property, the framework identify nature of the operations. i.e Insert/Update/delete.

Why SmartAPI needs to know Operation type ?

Because different operation needs different parameters. In case of Insert, we need to pass all Parameters, but in case of Update, we may not need to pass all parameters. In case of Delete, we only need to pass Primary key.

How SmartAPI map Properties of DTO object with Store procedure ?

For this mapping, you need to decorate each property with below attribute.

[AFWriteOperations("EmpID",new[] {APIOperations.Add,APIOperations.Update },ParameterType.Guid)] public Guid EmpID { get; set; }

The first parameter of the attribute is name of the parameter of Store procedure. In case property name and Store procedure parameter name is identical, then you can skip the first parameter.

The second parameter of the attribute is array of operations (Insert/Update/Delete). The property would be considered only for those operation which passed in the array.

The third parameter of the attribute is Data type.

Steps for Read operation

For Read operation, Decorate properties with below attribute.

[AFReadOperation("EmpID")] public Guid EmpID { get; set; }

The parameter of the Attribute is, name of the field return by Dataset or Data reader from database which is going to be mapped with the property. Through this mapping, the framework fill the DTO object from Dataset or Datareader.

That's it. Decoration of DTO object is done. Now let's see how to call SmartAPI.

If you have 3 layer architecture without service (WCF/WebAPI), then you may call the SmartAPI from presentation layer. If you are using Service layer (WCF/WebAPI) then you need to call SmartAPI from Service layer.

Let's see how to perform Insert operation.

First, fill data into DTO object and set operation=APIOperations.Add;

Then, pass the DTO object to the SmartAPI along with Store procedure name and Schema name, default 'dbo'. See code block below.

var employeeEntity = new SmartAPIEntity();
employeeEntity.EmpID = Guid.NewGuid();
employeeEntity.Name = 'Employee Name';
employeeEntity.EmployeeAddress = 'Employee Address';
employeeEntity.Phone = 'Employee Phone';
employeeEntity.EMail = 'Employee Email';

employeeEntity.Operation = APIOperations.Add;

//Create and call Smart API var smartAPI = AFSmartAPI.CreateSmartAPI();
var result = smartAPI.InsertUpdateDelete(employeeEntity, "InsertEmployee");

BindEmployeeGrid();

if (result.IsValid)
{
Console.WriteLine("Record added successfuly");
}

If you have any comments or suggestions, Please write to : skc.chy@gmail.com with subject line : AF.SmartAPI