Base solution for your next web application
Open Closed

Help with Dependency Injection #1028


User avatar
0
moustafa created

Hello before i wrote this topic i read the documentation related to Dependency Injection [http://www.aspnetboilerplate.com/Pages/Documents/Dependency-Injection]) and other related documentation [http://www.aspnetboilerplate.com/Pages/Documents/Repositories])

but i'm still need help and advice in my case , maybe for some of you my questions are naive , for other are easy , on the other hand for someone like me its confused and need answers to put things together and make the big picture clear as possible as we can

so i have a class that have one of more properties used to retrieve static data (not coming from database) , i tried to use dependency injection structure descried in the documentation but still have doubts that what's i'm doing is not totally true Q1- can anyone correct me if i'm wrong with what i done here? Q2- can you provide me with different ways (practical examples) to accomplish my task based on my code below and please don't refer me to external links :) ? Q3- what is the write place to put these classes in my project ?

Classes & Interfaces

1- BlockedPermissions.cs

public class BlockedPermissions
    {
        public string Name { get; set; }
    }

2-IBlockedPermissionsAppService.cs (never used in my code)

public interface IBlockedPermissionsAppService : IApplicationService
    {
        List<BlockedPermissions> GetList();
    }

3- BlockedPermissionsAppService.cs

public class BlockedPermissionsAppService
    {
        private IBlockedPermissionsRepository _blockedPermissionsRepository;

        public BlockedPermissionsAppService(IBlockedPermissionsRepository blockedPermissionsRepository)
        {
            _blockedPermissionsRepository = blockedPermissionsRepository;
        }
        public List<BlockedPermissions> GetList()
        {
            var lst = _blockedPermissionsRepository.GetList();
            return lst;
        }
    }

4- IBlockedPermissionsRepository.cs

public interface IBlockedPermissionsRepository 
    {
        List<BlockedPermissions> GetList();
    }

5- BlockedPermissionsRepository.cs

public class BlockedPermissionsRepository : IBlockedPermissionsRepository
    {
        public List<BlockedPermissions> GetList()
        {
            List<BlockedPermissions> lst = new List<BlockedPermissions>();
            lst.Add(new BlockedPermissions() { Name = "Pages.Administration.Languages" });
            lst.Add(new BlockedPermissions() { Name = "Pages.Administration.Languages.Create" });
            lst.Add(new BlockedPermissions() { Name = "Pages.Administration.Languages.Edit" });
            lst.Add(new BlockedPermissions() { Name = "Pages.Administration.Languages.Delete" });
           return lst;
      }

Finally i use this code to retrieve the list

var blockedPermissionsAppService = new BlockedPermissionsAppService(new BlockedPermissionsRepository());
            var lst = blockedPermissionsAppService.GetList();

Sorry to prolong and thank you :)


6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    There are many ways of doing same thing. So, it's your programming skill. But I will provide some adivces.

    Short answer: Remove all your code and add just this:

    public static class BlockedPermission
    {
        public string Name { get; set; }
    
        public static List<BlockedPermission> GetAll()
        {
            List<BlockedPermission> lst = new List<BlockedPermission>();
           
            lst.Add(new BlockedPermission() { Name = "Pages.Administration.Languages" });
            lst.Add(new BlockedPermission() { Name = "Pages.Administration.Languages.Create" });
            lst.Add(new BlockedPermission() { Name = "Pages.Administration.Languages.Edit" });
            lst.Add(new BlockedPermission() { Name = "Pages.Administration.Languages.Delete" });
     
            return lst;
        }
    }
    

    And use BlockedPermission.GetAll() method wherever you need it. This is identical functionality with your code since you don't use app services, repositories and dependency injection in the purposes of their existence. And you don't need them all. Just use the code above.

    But if you want to learn more, other problems of your code:

    1. You make "new BlockedPermissionsAppService" which should not be done when you use DI.
    2. You did not inherit from IRepository for IBlockedPermissionsRepository.
    3. You are using GetList from your code, not from the presentation layer. So, you don't need an application service.
  • User Avatar
    0
    moustafa created

    thank you very much for your advice .. actually i admire you and impressed with amount of perfect work done in aspnetzero and really enjoy using it and looking for more from you

    back again to my question .. i need to learn more so consider that i need to use my code above to retrieve data from database , how can i accomplish that using dependency injection , can you correct my mistakes and write the write code to learn the proper way :)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Where do you call your final code lines ?

    var blockedPermissionsAppService = new BlockedPermissionsAppService(new BlockedPermissionsRepository());
    var lst = blockedPermissionsAppService.GetList();
    

    is it in an mvc controller ?

  • User Avatar
    0
    moustafa created

    no its inside RoleAppService.cs in GetRoleForEdit Method

    var blockedPermissionsAppService = new BlockedPermissionsAppService(new BlockedPermissionsRepository());
                var lst = blockedPermissionsAppService.GetList();
                var permissions = PermissionManager.GetAllPermissions().Where(w => lst.All(a => a.Name != w.Name));
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I suggest you to not call an app service from another app service first :), because it's a bad practice.

    I assume that, you have defined an entity with name "BlockedPermission", added it to your dbcontext, generate required migration and update the database.

    Then, you can directly use IRepository<BlockedPermission> in the roleAppService. Just inject this into roleAppService like this.

    private readonly RoleManager _roleManager;
    private readonly IRepository<BlockedPermission> _blockedPermissionsRepository;
    
    public RoleAppService(RoleManager roleManager, 
    	IRepository<BlockedPermission> blockedPermissionsRepository)
    {
    	_roleManager = roleManager;
    	_blockedPermissionsRepository = blockedPermissionsRepository;
    }
    

    Then in your app service, just get data using _blockedPermissionsRepository .

  • User Avatar
    0
    moustafa created

    thank you very much for your advice :D