Skip to content

Repository

Ritesh Sutaria edited this page Sep 28, 2019 · 9 revisions

Core class performs all operations. Vega uses extensive caching to get Fastest Performance. Here is the guidelines to use Repository.

Create Repository object where T is class derived from EntityBase, pass the connection and perform operations.

Add

To add record use Add method of repository.

City city = new City()
{
    Name = "Ahmedabad",
    State = "GU",
    CountryId = 1,
    Longitude = 100.23m,
    Latitude = 123.23m,
};

using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     city.Id = (long)cityRepo.Add(city);
}

Note: to add record with specific column use Add(T entity, string columns) method.

Update

To update record use Update method of repository.

City city = new City()
{
    Id = 1,
    Name = "Ahmedabad",
    State = "GU",
    CountryId = 1,
    Longitude = 102.23m,
    Latitude = 124.23m,
};

using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     bool result = cityRepo.Update(city);
}

Note: to update record with specific column use Update(T entity, string columns) method.

Delete

To delete record use Delete method of repository. Soft Delete if Entity contains IsActive column.

//soft delete 
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     bool result = cityRepo.Delete(1); //Record Id & User Id
}

//hard delete 
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     bool result = cityRepo.HardDelete(1); //Record Id & User Id
}

Note: This method performs SoftDelete when IsActive property is defined. Use HardDelete method to perform Hard Delete

Read

Read One Record

Reads first record with specified criteria & converts it to object.

//read one with specified id
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     City city = cityRepo.ReadOne(1); //Record Id
}

//read one with specified dynamic criteria
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     City city = cityRepo.ReadOneWhere(new { Name = "Ahmedabad" }); //Dynamic criteria
}

Read List

Reads all records with specified criteria & converts it to enumerable object which can be converted to List.

//read all records
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList  = cityRepo.ReadAll().ToList();
}

//read all with specified dynamic criteria
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList = cityRepo.ReadAll("*", new { Name = "Ahmedabad" }).ToList(); //Dynamic criteria
}

Read Paged

Reads all records with server side paging, specified criteria & converts it to enumerable object which can be converted to List.

//read all records (name column) of page no 1 with page size 5
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList = cityRepo.ReadAllPaged("name", 1, 5).ToList();
}

//read all records (name column) of page 1 with page size 5 & specified dynamic criteria
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList = cityRepo.ReadAllPaged("name", 1, 5, null, new { State = "GU" }).ToList(); //Dynamic criteria
}

Read Paged (No Offset - Fastest of all)

Reads all records with server side paging without offset, specified criteria & converts it to enumerable object which can be converted to List.

//read all records (name column) of First Page with page size 5
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList = cityRepo.ReadAllPaged("name", 5, PageNavigationEnum.First).ToList();
}

//read all records (name column) of Next Page with page size 5. Provide sorted column value & id of last row.
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList = cityRepo.ReadAllPaged("name", 5, PageNavigationEnum.Next, null, new object[] { cityList.LastOrDefault().Name }, cityList.LastOrDefault().Id).ToList();
}

//read all (name column) of First Page with page size 5 & specified dynamic criteria 
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList = cityRepo.ReadAllPaged("name", 5, PageNavigationEnum.First, null, null, null, new { State = "GU" }).ToList(); //Dynamic criteria
}

//read all (name column) of Next Page with page size 5 & specified dynamic criteria 
using (SqlConnection con = new SqlConnection(connectionString))
{
     Repository<City> cityRepo = new Repository<City>(con);
     List<City> cityList = cityRepo.ReadAllPaged("name", 5, PageNavigationEnum.First, null, new object[] { cityList.LastOrDefault().Name }, cityList.LastOrDefault().Id, new { State = "GU" }).ToList(); //Dynamic criteria
}

Note: You can only move to Next, Previous, First and Last Page when NoOffset method is used.