A Dependency Injection and Repository Pattern Example – II

(continuing)

Service Layer

Here is another layer that we put advanced CRUD operation in it. The service layer will make use of repository layer to operate on the database, as well as be used by the controller to process the data. Here also firstly we define the interface IRoleService:

namespace Cobra.App.Infrastructure.Contracts
{
    public interface IRoleService
    {      
        Role GetRoleById(int id);
        IEnumerable<Role> GetRoleList();        
        void UpdateRole(Role role);
        void DeleteRole(int id);
        bool CreateRole(Role role);

        IEnumerable<Permission> GetRolePermissions(int id);
        IEnumerable<Permission> GetPermissionList();

        RolePermission GetRolePermissionById(int id);
        IEnumerable<RolePermission> GetRolePermissionByRoleId(int Id);
        void UpdateRolePermission(RolePermission rolePermission);
        void DeleteRolePermission(int id);
        bool CreateRolePermission(RolePermission rolePermission);

        IEnumerable<Role> GetRoleByLoginId(int id);
        void CreateUserRole(Role role, int userId);
        void UpdateUserRole(Role role, int userId);
        void DeleteUserRole(Role role, int userId);
        void CreateOrUpdateUserRole(Role role, int userId);
    }
}

Then in RoleService.cs is the implementation of the interface:

namespace Cobra.App.Infrastructure.Services
{
    public class RoleService : IRoleService
    {
        //Nicky
        private IRepository<Login> _loginRepository;
        private IRepository<Role> _roleRepository;
        private IRepository<RolePermission> _rolePermissionRepository;
        private IRepository<Permission> _permissionRepository;

        public RoleService(IRepository<Login> loginRepository, IRepository<Role> roleRepository, IRepository<RolePermission> rolePermissionRepository, IRepository<Permission> permissionRepository)
        {
            _loginRepository = loginRepository;
            _roleRepository = roleRepository;
            _rolePermissionRepository = rolePermissionRepository;
            _permissionRepository = permissionRepository;
        }

        public Role GetRoleById(int id)
        {
            return _roleRepository.GetById(id);
        }

        public IEnumerable<Role> GetRoleList()
        {
            return _roleRepository.GetAll();
        }

        public IEnumerable<Permission> GetRolePermissions(int id)
        {
            return _roleRepository.GetById(id).RolePermissions.Select(x => x.Permission);
        }

        public IEnumerable<Permission> GetPermissionList()
        {
            return _permissionRepository.GetAll();
        }

        public void UpdateRole(Role role)
        {
            _roleRepository.Update(role, true);
        }

        public void DeleteRole(int id)
        {
            _roleRepository.Remove(GetRoleById(id), true);
        }

        public bool CreateRole(Role role)
        {
            return _roleRepository.Add(role, true) == null ? false : true;
        }

        public RolePermission GetRolePermissionById(int id)
        {
            return _rolePermissionRepository.GetById(id);
        }

        public IEnumerable<RolePermission> GetRolePermissionByRoleId(int Id)
        {
            return _rolePermissionRepository.GetAll().Where(x => x.RoleId == Id);
        }

        public void UpdateRolePermission(RolePermission rolePermission)
        {
            _rolePermissionRepository.Update(rolePermission, true);
        }

        public void DeleteRolePermission(int id)
        {
            _rolePermissionRepository.Remove(GetRolePermissionById(id),true);
        }

        public bool CreateRolePermission(RolePermission rolePermission)
        {
            return _rolePermissionRepository.Add(rolePermission, true) == null ? false : true;
        }

        public IEnumerable<Role> GetRoleByLoginId(int id)
        {
            return _loginRepository.GetById(id).Roles;
        }

        public void CreateUserRole(Role role, int userId)
        {
            _loginRepository.GetById(userId).Roles.Add(role);
            _loginRepository.Save();
        }

        public void UpdateUserRole(Role role, int userId)
        {
            _loginRepository.GetById(userId).Roles.Clear();
            _loginRepository.GetById(userId).Roles.Add(role);
            _loginRepository.Save();
        }

        public void DeleteUserRole(Role role, int userId)
        {
            _loginRepository.GetById(userId).Roles.Remove(role);
            _loginRepository.Save();
        }

        public void CreateOrUpdateUserRole(Role role, int userId)
        {
            var roleList = GetRoleByLoginId(userId);
            if (roleList.Count() == 0)
            {    
                CreateUserRole(role, userId);
            }
            else
            {              
                UpdateUserRole(role, userId);
            }
        }
    }
}

And we also use the Ninject for binding:

Bind<IRepository<Person>>().To<Repository<Person>>();
Bind<IRepository<Profile>>().To<Repository<Profile>>();
Bind<IRepository<Organisation>>().To<Repository<Organisation>>();
...

Here when we want to create the instance of the service, Ninject will create the instance of the Respository objects automatically for us to be passed into the constructor. Then we can use the basic CRUD operation in  Respository objects to make more complex data operations.

Controller Layer

The last layer is the controller layer, in which we use the services to do all the data operations:

namespace Cobra.Controllers
{
    //[RequireHttps]
    [Authorize]
    public class ManageController : Controller
    {
        private IAccountService _accountService; //Required to GetCurrentUser-Id & Email
        private IAttributeTypeService _attributeTypeService;
        private IAuthenticationService _authenticationService;
        private IRoleService _roleService;

        // Author: Aaron Bhardwaj
        public ManageController(IAuthenticationService authenticationService,
            IAccountService accountService, IAttributeTypeService attributeTypeService,
            IRoleService roleService)
        {
            _accountService = accountService;
            _attributeTypeService = attributeTypeService;
            _authenticationService = authenticationService;
            _roleService = roleService;
        }
        ....
        //role management
        //Author Nicky

        [AuthorizePermission(PermissionNames = "Role Management", ReturnType = ReturnType.Html)]
        public ActionResult RoleManagement()
        {
            return View();
        }

        [AuthorizePermission(PermissionNames = "User Management", ReturnType = ReturnType.Json)]
        public JsonResult GetRoleList()
        {
            var roleList = _roleService.GetRoleList().Select(x => new { Id = x.Id, Name = x.RoleName });
            return Json(roleList, JsonRequestBehavior.AllowGet);
        }

        [AuthorizePermission(PermissionNames = "Role Management", ReturnType = ReturnType.Json)]
        public JsonResult GetPermissionList()
        {
            var permissionList = _roleService.GetPermissionList().Select(x => new { Id = x.Id, Name = x.ModuleName }).ToList();
            return Json(permissionList, JsonRequestBehavior.AllowGet);
        }
        ....
    }
}

Similarly we use the Ninject for binding:

Bind<IAttributeTypeService>().To<AttributeTypeService>();
Bind<IUserManagementService>().To<UserManagementService>();
Bind<IPaymentService>().To<PaymentService>();
Bind<IRoleService>().To<RoleService>();
...

So when the controller is instantiated by system, the related services will be created to be passed into the constructor.

In conclusion, we can see the diagram:

It clearly show the relationships between the layers.

 

Leave a Reply

Your email address will not be published. Required fields are marked *