Developing business managers

Now we can start developing our business managers, which can be used by the service controllers in service layers. When designing enterprise application architecture, it's always a better choice to expose interfaces rather than classes. This is recommended to encapsulate the actual implementation and to ensure that unnecessary methods or properties are not known by the consumer object.

Add the Managers folder and keep the interface and its implementation in its domain-specific folder.

The following is the IServiceRequestManager that exposes one method to return the list of service requests lodged by tenants:

    public interface IServiceRequestManager : IActionManager 
{
IEnumerable<TenantServiceRequest>
GetAllTenantServiceRequests();
}

We have derived this interface from the IActionManager interface, so the implementer class can provide implementation for the CRUD methods as well.

Now we will add the ServiceRequestManager class that implements the IServiceRequestManager interface as follows:

    public class ServiceRequestManager : BusinessManager,
IServiceRequestManager
{
IRepository _repository;
ILogger<ServiceRequestManager> _logger;
IUnitOfWork _unitOfWork;

public IUnitOfWork UnitOfWork
{
get
{
return _unitOfWork;
}
}

public ServiceRequestManager(IRepository repository,
ILogger<ServiceRequestManager> logger,
IUnitOfWork unitOfWork) : base()
{
_repository = repository;
_logger = logger;
_unitOfWork = unitOfWork;
}

public void Create(BaseEntity entity)
{
ServiceRequest serviceRequest = (ServiceRequest)entity;
_logger.LogInformation("Creating record for {0}",
this.GetType());
_repository.Create<ServiceRequest>(serviceRequest);
_logger.LogInformation("Record saved for {0}",
this.GetType());
}

public void Delete(BaseEntity entity)
{
}

public IEnumerable<BaseEntity> GetAll()
{
throw new NotImplementedException();
}

public void Update(BaseEntity entity)
{
throw new NotImplementedException();
}

public IEnumerable<TenantServiceRequest>
GetAllTenantServiceRequests()
{

var query = from tenants in _repository.All<Tenant>()
join serviceReqs in _repository.All<ServiceRequest>()
on tenants.ID equals serviceReqs.TenantID
join status in _repository.All<Status>()
on serviceReqs.StatusID equals status.ID
select new TenantServiceRequest()
{
TenantID = tenants.ID,
Description = serviceReqs.Description,
Email = tenants.Email,
EmployeeComments = serviceReqs.EmployeeComments,
Phone = tenants.Phone,
Status = status.Description,
TenantName = tenants.Name
};
return query.ToList<TenantServiceRequest>();
}

public void SaveChanges()
{
_unitOfWork.SaveChanges();
}
}

If you have noticed, we have injected Repository, Logger, and UnitOfWork. Both Repository and UnitOfWork objects will be scoped per request, whereas the Logger object will be a singleton object. We will register them through the .NET Core built-in dependency injector in the service layer Startup class.

Similar to ServiceRequestManager, we will add another manager under Managers > TenantManagement to provide tenant management. Here is the code for the ITenantManager interface:

    public interface ITenantManager : IActionManager 
{
Tenant GetTenant(long tenantID);
}

The implementation for the TenantManager class is as follows:

    public class TenantManager : BusinessManager , ITenantManager 
{
IRepository _repository;
ILogger<TenantManager> _logger;
IUnitOfWork _unitOfWork;
IServiceRequestManager _serviceRequestManager;


public IUnitOfWork UnitOfWork
{
get
{
return _unitOfWork;
}
}

public TenantManager(IRepository repository,
ILogger<TenantManager> logger, IUnitOfWork unitOfWork,
IServiceRequestManager serviceRequestManager) : base()
{
_repository = repository;
_logger = logger;
_unitOfWork = unitOfWork;
_serviceRequestManager = serviceRequestManager;
}

public virtual Tenant GetTenant(long tenantID)
{
try
{
_logger.LogInformation(LoggingEvents.GET_ITEM,
"The tenant Id is " + tenantID);
return _repository.All<Tenant>().Where(i => i.ID ==
tenantID).FirstOrDefault();
}catch(Exception ex)
{
throw ex;
}

}

public void Create(BaseEntity entity)
{
Tenant tenant= (Tenant)entity;
_logger.LogInformation("Creating record for {0}",
this.GetType());
_repository.Create<Tenant>(tenant);
SaveChanges();
_logger.LogInformation("Record saved for {0}",
this.GetType());
}

public void Update(BaseEntity entity)
{
Tenant tenant = (Tenant)entity;
_logger.LogInformation("Updating record for {0}",
this.GetType());
_repository.Update<Tenant>(tenant);
SaveChanges();
_logger.LogInformation("Record saved for {0}",
this.GetType());
}

public void Delete(BaseEntity entity)
{
Tenant tenant = (Tenant)entity;
_logger.LogInformation("Updating record for {0}",
this.GetType());
_repository.Delete<Tenant>(tenant);
SaveChanges();
_logger.LogInformation("Record deleted for {0}",
this.GetType());
}

IEnumerable<BaseEntity> IActionManager.GetAll()
{
return _repository.All<Tenant>().ToList<Tenant>();
}

public void SaveChanges()
{
_unitOfWork.SaveChanges();
}

}

SaveChanges() actually uses the UnitOfWork instance and this will be called either by the service controller (part of our Web API project) or from the manager itself. It depends on the requirement and is open for the developer to use it as per need.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.188.238