Lightado.net

Light ADO.net is clean, simple and one line of code Data Access Layer for SQL Server.

View project on Nuget

Quick start.

To Start with LightADO.net add the LightADO.net reference in you project by ruining Nuget Command:

PM> Install-Package LightAdo.net

Once the reference get installed, you can immediately get a DataTable from execute a query like this:

DataTable table = new Query().EexecuteToDataTable("select * from Applications", CommandType.Text);

The Query object will get the connection string by default from the connection string section in the configuration file by default it named DefaultConnection as you can see:


< connectionstrings>
     < add name="DefaultConnection" connectionstring="Data Source=.\SQLEXPRESS;Initial Catalog=YourDataBase;Integrated Security=True" />
< / connectionstrings>
                    

Configuration

This section will explains the configuration needed to setup lightado.net

Connection String

Light ADO.net will need a connection string to connect to a database this connection can be set within the constructor of Query or NonQuery object or set it in the configuration file with the name " DefaultConnection "

< connectionstrings>
     < add name="DefaultConnection" connectionstring="Data Source=.\SQLEXPRESS;Initial Catalog=YourDataBase;Integrated Security=True" />
< /connectionstrings>
                    
You can change the name of the connection by pass it within the constructor of Query or NonQuery object and set the loadFromConfiguration to true:
Query newQuery = new Query("NameOfTheConnectionInTheConfigrationFile", True);
If you want to send the connection within the code itself create a new Query or NonQuery object in the constructor pass the connection string and set the loadFromConfiguration parameter to false:
NonQuery newNonQuery = new NonQuery(@"Data Source=.\SQLEXPRESS;Initial Catalog=YourDataBase;Integrated Security=True", False);

Encryption Key

if you want to enable encryption you will need to set the encryption key app setting section

  < appSettings>
    < add key="EncryptKey" value="745jhduerhsm"/>
  < /appSettings>

Introduction

In general Most method will take 3 parameter

  • Command Name or Command Text: the command can be text or the stored procedures name
  • CommandType can be:
    • Stored procedures
    • Text
    • Table
  • Params of parameters: parameter object

Example:

DataTable table = new Query().EexecuteToDataTable("select * from Applications", System.Data.CommandType.Text);
Console.ReadLine();

Execute Command Text with parameter

DataTable table = new Query().EexecuteToDataTable("select * from Applications where ID = @ID", System.Data.CommandType.Text, new Parameter("ID", 324));
Console.ReadLine();

Execute with stored procedure

if you want to use stored procedure just type the name if the stored procedure and set comm type to stored procedure
DataTable table = new Query().EexecuteToDataTable("Application_Get", System.Data.CommandType.StoredProcedure);
Console.ReadLine();

Execute Query

Query Class will handle will of sqlCOmmand.ExecuteQuery method, but you can do more the Query class, as you can get one of those type:

  • DataTable
  • DataSet
  • Object
  • List of Object
  • dynamic Object
  • List of dynamic Object

Execute To Data Table

DataTable table = new Query().EexecuteToDataTable("Application_Get", System.Data.CommandType.StoredProcedure);
Console.ReadLine();

Execute To Data Set

DataSet table = new Query().EexecuteToDataSet("Application_Get", System.Data.CommandType.StoredProcedure);
Console.ReadLine();

Execute To Object

Application application = new Query().EexecuteToObject< Application>("Application_Get_ByID", System.Data.CommandType.StoredProcedure, new Parameter("ID", 324));
Console.ReadLine();

Execute To List Of Object

List< Application> application = new Query().EexecuteToListOfObject< Application>("select * from Applications", System.Data.CommandType.StoredProcedure);
Console.ReadLine();

Execute To Dynamic object

dynamic application = new Query().EexecuteToDynamicObject("select * from Applications where ID = @ID", System.Data.CommandType.Text, new Parameter("ID", 324));
Console.WriteLine(application.Name);
Console.ReadLine();

Execute To List Of Dynamic Object

List< dynamic> applications = new Query().EexecuteToListOfDynamicObject("select * from Applications", System.Data.CommandType.Text);
Console.ReadLine();

Execute NunQuery

This object can execute NonQuery with auto mapped object or with manual mapping

Execute NonQuery with manual mapping

bool result = new NonQuery().Execute("Insert into Applications (Name, Password) values (@Name, @Password)",
                            CommandType.Text,
                            new Parameter("Name", "Test"),
                            new Parameter("Password", "123456"));

Execute NonQuery with auto mapping

To execute NonQuery with auto mapping the command need to be a stored procedure only,
bool result = new NonQuery().Execute< Application>("Application_Create",
                            CommandType.StoredProcedure,
                            new Parameter("Name", "Test"),
                            new Parameter("Password", "123456"));

Execute NonQuery with output values

if the output value is one of the object property that property will set automatically, if the output parameter was not on of the he object property you can just send it as parameter and set the direction of if as output

Output parameter is in the property of object

if (new NonQuery().Execute< Application>("Application_Create", application) == true)
{
	Console.WriteLine(application.ID);
}
Notes that LightADO.net will understand that ID is output parameter as you can see the stored procedure, and it will map it automatically:
Create PROCEDURE [dbo].[Application_Create]
	 @ID int output,
     @Merchant int,
     @Name nvarchar(1500),
     @IsEnabled bit,
     @CreateDate datetime,
     @FailedURL ntext,
     @SuccessURL ntext,
     @Notes ntext, 
	 @AuthenticationKey ntext
AS
BEGIN
	insert into Applications(Merchant, Name, IsEnabled, CreateDate, FailedURL, SuccessURL, Notes, AuthenticationKey)
	values (@Merchant, @Name, @IsEnabled, @CreateDate, @FailedURL, @SuccessURL, @Notes, @AuthenticationKey)
	set @ID = SCOPE_IDENTITY();
	
END

output parameter is not one of the properties of the object:

in that case just send it as parameter and set the direction of if as output
Parameter parameter = new Parameter("ThisOutputParameter", null, ParameterDirection.Output);
if (new NonQuery().Execute("Application_Create", application, parameter) == true)
{
	Console.WriteLine(parameter.Value);
	Console.WriteLine(application.ID);
}

Object Relationship

This Section explain how to link to object together Suppose the that any application must have a merchant and you want to write something like this:
Console.WriteLine(Application.Merchant.Name)
In order to do that all you need to do is mark the merchant property in the application as Foreign key:
namespace ConsoleApplication1
{
    using LightADO;

    class Application
    {
        [PrimaryKey]
        public string ID { get; set; }

        public string Name { get; set; }

        [ForeignKey]
        public Merchant Merchant { get; set; }
    }
}
Now in the Merchant you will need to have a Contractor that take integer value as parameter this integer value will the value of the primary key, in that constructor you will need to Call the Execute Query to set the merchant object:
namespace ConsoleApplication1
{
    using LightADO;

    class Merchant
    {
        public Merchant()
        {
        }

        public Merchant(int id)
        {
            Merchant mecrhant = new Query().EexecuteToObject("select * from Merchants where ID = @ID", System.Data.CommandType.Text, new Parameter("ID", id);
            if(mecrhant != null)
            {
                this.ID = mecrhant.ID;
                this.Name = mecrhant.Name;
            }
        }

        [PrimaryKey]
        public int ID { get; set; }

        public int Name { get; set; }
    }
}

Once the LightADO.net find a property marked as foreign key it will call a constructor with integer value int

Encryption

You can use the Encrypted Attribute class to enable encryption for class or single property, to encrypt a every string property in class mark that as encrypted


    [Encrypted]
    public class Application
    {
        public int ID { get; set; }

        public int Merchant { get; set; }

        public string Name { get; set; }

        public bool IsEnabled { get; set; }

        public string FailedURL { get; set; }

        public string SuccessURL { get; set; }

        public string Notes { get; set; }

        public string AuthenticationKey { get; set; }

        public DateTime CreateDate { get; set; }
    }

If you want to encrypt single property mark that property as encrypted
    
    public class Application
    {
        public int ID { get; set; }

        public int Merchant { get; set; }

        public string Name { get; set; }

        public bool IsEnabled { get; set; }

        public string FailedURL { get; set; }

        public string SuccessURL { get; set; }

        public string Notes { get; set; }

        [Encrypted]
        public string AuthenticationKey { get; set; }

        public DateTime CreateDate { get; set; }
    }

Validate Object

Instead of writing if statement LightADo.net give you the option to Separate the validation part then the object by calling the AutoValidation calss

Validate property for null

To validate property for null mark that property for NullValidation and set the AllowNull = false , and the error message in case this property was set as null

[NullValidation(AllowNull = false, ErrorMessage = "Application Name can't be null")]
public string Name { get; set; }

Validate property for Regular Expression

To validate property for Regular Expression use RegularExpression Attribute.
[RegularExpressionsValidation(RegularExpression = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "Email not correct")]
public string Email { get; set; }

Create Custom Validation

To Create your own Validation you will need to inherit you validation class from AutoValidation class
namespace ConsoleApplication1
{
    using LightADO;
    using System;

    class MerchantIsExist : AutoValidation
    {
        public override bool Validate(object propertyValue)
        {
            throw new NotImplementedException();
        }
    }
}
Now you can use you class as attribute as you can see:
[MerchantIsEexist(ErrorMessage = "Merchant is not exist")]
[ForeignKey]
public Merchant Merchant { get; set; }

Exception

This section will explain the exception that throw by LightADO.net

LightADOException

This type of exception will throw when something wrong happened drawing exception query or NonQuery you will need to catch this type manually like this:
try 
{
	// your Code
}  
catch(LightADOException ex)
{
	Console.WriteLine(ex);
}

LightADOValidationException

This type of exception will throw when the property of in violation of AutoValidation class you will need to catch this exception manually like this:
try 
{
	// your Code
}  
catch(LightADOValidationException ex)
{
	Console.WriteLine(ex);
}

Error List

here's table the list all of the error you may face it:
GE01003 Error Message Reason
GE01000 Can't set null or empty as connection string Your Setting Null or empty as connection string to the connection string property
GE01002 No connection setting with name {0} was found in the connection Strings section If you set the connection string manually by passing the connection string name to Query or NonQuery object and set the loadFromConfiguration parameter to true, LightADO.net will search in the connectionstring section for that name, it did not found, it will throw this exception
GE01003 Stored Procedure expect paramete named: {0}, which was not supplied When using NonQuery with automapping by sending the object to it that object must have all of the store procedure parameter in it or in the parameter list
GE01004 primary key is Not defined in {0} When using automapping you will need to set the primary key and the foreign key for all of the object your project
E012903 Violation of {0}, set the error message as [{1}( ErrorMessage="This A demo ")]; to display it This error happened when object is in violation of AutoValidation class but you did not set the Error Message in the attribute of the property
GE01200 SQL Client Exception This Exception will be throw by the System.Data