none
Dependancy injection RRS feed

  • Question

  • Hi All, 

    Can you guys please help me in How to register the below injection in my API controller ?

         var enricher = DependencyResolver.Current.GetService<IEditModelEnricher<T>>();

    The above line in the code gives me NULL value as of now.

    public class EnrichedActionResult<T>
        {

            public string ActionName;
            public T Model;

            public EnrichedActionResult(string actionName, T actionData)
            {
                this.ActionName = actionName;
                this.Model = actionData;
            }

            public void ExecuteResult()
            {
                if (this.Model != null)
                {

                    var enricher = DependencyResolver.Current.GetService<IEditModelEnricher<T>>();
                    //enricher.Enrich

                    if (enricher != null)
                    {
                        enricher.Enrich((T)this.Model);
                    }
                }
            }
        }


    Selvakumar Rathinam

    • Moved by CoolDadTx Thursday, June 20, 2019 1:59 PM ASP.NET related
    Thursday, June 20, 2019 10:37 AM

All replies

  • Why wouldn't you use a simple IoC, like Simple Injector, Unity, Autofact etc. and ect.?

    What is all this complication you are doing in using an Action method in a controller and using a Model?

    Where did you get this code from?




    • Edited by DA924x Thursday, June 20, 2019 11:50 AM
    Thursday, June 20, 2019 11:48 AM
  • I am not clear if you understand the code. 

    The purpose for using this in the controller is to enrich by adding few more entities result in the existing instance.

    So that dynamically any instance I can enrich it by what ever its edit model implementation methods are..   


    Selvakumar Rathinam

    Thursday, June 20, 2019 12:46 PM
  • It depends on what actual DI container you're using. All of them support registering generic types though. For example if you wanted to register such a type in Autofac you'd do this.

    //EditModelEnricher is the concrete implementation of IEditModelEnricher<T>
    builder.RegisterGeneric(typeof(EditModelEnricher<T>)).As<typeof(IEditModelEnricher<>));

    Please post questions related to DependencyResolver itself in the ASP.NET forums.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, June 20, 2019 1:59 PM
  • I am not clear if you understand the code. 

    The purpose for using this in the controller is to enrich by adding few more entities result in the existing instance.

    So that dynamically any instance I can enrich it by what ever its edit model implementation methods are..   


    Selvakumar Rathinam

    I understand the code fully. But would I do what you're trying to do? The answer would be no. IMO, the WebAPI is just a conduit between the WebAPI client and the layer the WebAPI is calling upon on the behalf of the client, like the WebAPI would be calling a DAL and implementing SoC that is DI into the controller.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    I also suggest that you post to the ASP.NET forums.

    https://forums.asp.net/

    using DAL;
    using Entities;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;
    
    namespace ProgMgmntCore2Api.Controllers
    {
        [Produces("application/json")]
        [Route("api/[controller]")]
        [ApiController]
    
        public class TaskController : ControllerBase
        {
            private readonly IDaoTask _daoTask;
    
            public TaskController(IDaoTask daoTask)
            {
                _daoTask = daoTask;
            }
    
            [HttpGet]
            [Route("GetTaskById")]
            public DtoTask GetTaskById(int id)
            {
                return _daoTask.GetTaskById(id);
            }
    
            [HttpGet]
            [Route("GetTasksByProjId")]
            public List<DtoTask> GetTasksByProjectId(int id)
            {
                return _daoTask.GetTasksByProjectId(id);
            }
    
            [HttpPost]
            [Route("CreateTask")]
            public void CreateTask(DtoTask dto)
            {
                _daoTask.CreateTask(dto);
            }
    
            [HttpPost]
            [Route("UpdateTask")]
            public void UpdateTask(DtoTask dto)
            {
                _daoTask.UpdateTask(dto);
            }
    
            [HttpPost]
            [Route("DeleteTask")]
            public void DeleteTask(DtoId dto)
            {
                _daoTask.DeleteTask(dto.Id);
            }
        }
    }


    • Edited by DA924x Thursday, June 20, 2019 3:55 PM
    Thursday, June 20, 2019 3:39 PM
  • Hi, 
    It was implemented earlier using Structuremap and IOC now I want to use the same for asp.net core 

    Scan(
                    scan =>
                    {
                        scan.Assembly("---.---.---.Models");
                        scan.WithDefaultConventions();
                        scan.ConnectImplementationsToTypesClosing(typeof(IEditModelEnricher<>));
                    });

    can you please let me know  How do I achieve this same thing in asp.net core ?


    Selvakumar Rathinam


    Friday, June 21, 2019 8:10 AM
  • I am sorry. this is not what I am trying to do.. I am trying to write a generic controller which will take care of all my entity's (your code works only for Task entity) CRUD operation. 

    and while fetching the GetbyId I want to enrich the drop down control related collection to that Entity result.  

    Hope now its clear to you. 


    Selvakumar Rathinam

    Friday, June 21, 2019 8:27 AM
  • Since your question is related to MVC controllers and StructureMap please post in the appropriate forums. MVC questions can be posted in the ASP.NET forums. Questions related to StructureMap need to posted in their support area.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, June 21, 2019 1:53 PM
  • I am sorry. this is not what I am trying to do.. I am trying to write a generic controller which will take care of all my entity's (your code works only for Task entity) CRUD operation. 

    and while fetching the GetbyId I want to enrich the drop down control related collection to that Entity result.  

    Hope now its clear to you. 


    Selvakumar Rathinam

    https://forums.asp.net/1255.aspx/1?ASP+NET+Core

    You can post to the forum. However, I view this like I view the generic repository pattern, which is limited in scope.

    Friday, June 21, 2019 4:47 PM