Skip to main content

Layered Repository Design

The platform exposes data from database tables in a layered repository design. The layers consist of the following:

All repository classes derive from the IRepository<> interface. It follows the standard Request/Response design paradigm so that requests through all layers are handled the same way. It contains the following methods:

    
    public partial interface IRepository 
        where TObject : class        
    {
        IResponseItem<TObject> Insert(IRequestItem<TObject> request);
        IResponseItem<TObject> Get(IRequestItem<TPkDataType> request);
        IResponseItem<TObject> Update(IRequestItem<TObject> request);
        IResponse Delete(IRequestItem<TPkDataType> request);
        IStorageQueryResponseItems<TObject> Query(IRequestItem<IStorageQuery> request);
        IResponseItem<double> QueryAggregate(IRequestItem<IStorageQuery> request);
    }

The following code demonstrates use of each of the Repository methods above on the SecurityUser class, which define users in the system.

    
var service = ObjectLocator.Current.GetInstance<ISecurityUserBusinessRepository>();
var newUser = new SecurityUser();
newUser.FirstName = “test”;
//INSERT
var respInsert = service.Insert(new RequestItem<SecurityUser>(){Item = newUser});
//UPDATE
var updateUser = respInsert.Item;
var respUpdate = service.Update(new RequestItem<SecurityUser>(){Item = updateUser});
//GET
var respGet = service.Get(new RequestItem<Guid>(){Item = updateUser.UserKey});
//QUERY
var builder = new StorageQueryBuilder();
builder.IsEqual(“UserKey”, updateUser.UserKey.ToString());
var respQuery = service.Query(new RequestItem<IStorageQuery>(){Item = builder.GetStorageQuery()});
//QUERY AGGREGATE
var respQA = service.Query(new RequestItem<IStorageQuery>(){Item = builder.GetStorageQuery()});
//DELETE
var respDelete = service.Delete(new RequestItem<Guid>(){Item = updateUser.UserKey});

The IMappingRepository<> interface defines an object that allows mapping an object to a designation object before calling a repository method. This allows creating repositories for classes that do not have an underlying data store, but that map to a base case that can. This provides a shorthand way to collapse several lines of code into just a couple. The following example shows an IdentityUser object (ASP.NET Identity User implementation) that maps to the internal SecurityUser class that is mapped to storage. The following shows the usage with a MappingRepository:

    
//Starting object
IdentityUser identityUser = new IdentityUser();
identityUser.FirstName = “test”;
//Update an IdentityUser method #1
var repoM1 = ObjectLocator.Current.GetInstance<ISecurityUserServiceRepository>();
var mapM1 = ObjectLocator.Current.GetInstance<IMappingService>();
SecurityUser securityUser = map.Map<IdentityUser, SecurityUser>(identityUser);
var respM1 = repoM1.update(new ResponseItem<SecurityUser>(){Item=securityUser}); //SAVE USER

//Update an IdentityUser method #2
var repoM2 = ObjectLocator.Current.GetInstance<IMappingRepository<IdentityUser, Guid, ServiceModel.SecurityUser, ISecurityUserServiceRepository>>();
var respM2 = repoM2.update(new ResponseItem<IdentityUser>(){Item=identityUser}); //SAVE USER


//Update an IdentityUser method #3 – this uses a pre-defined interface, same as #2
public interface IIdentityUserServiceRepository : IMappingRepository<IdentityUser, Guid, SecurityUser, ISecurityUserServiceRepository
{}
var repoM3 = ObjectLocator.Current.GetInstance<IIdentityUserServiceRepository >();
var respM3 = repoM3.update(new ResponseItem<IdentityUser>(){Item=identityUser}); //SAVE USER