简体   繁体   中英

Call Parameterized constructor using Service ASP.NET Core

I am new to .net core and wondering if the following is achievable or not.How can I call My service class constructor sending my request data as parameters.

My Request Model class

public class TestRequestcs 
    {
        public int aaa { get; set; }
        public int bbb { get; set; }
    }

My service Interface

public interface ITestService
    {
        string ReturnResult();
    }

My Service Class

 public class TestService : ITestService
    {
        public int aaa { get; private set; }
        public int bbb { get; private set; }
        public TestService(int a,int b)
        {
            this.aaa = a;
            this.bbb = b;
        }
        public TestService()
        {
        }
        public string ReturnResult()
        {
            return (this.aaa + this.bbb).ToString();
        }
    }

My controller

 public class ValuesController : ControllerBase
    {
        private readonly ITestService _TestService;

        public ValuesController(ITestService TestService)
        {
            _TestService = TestService;
        }

        [HttpPost]
        [Route("TestService")]
        public ActionResult TestInterFaceService([FromBody] TestRequestcs _data)
        {
            try
            {
                if (!ModelState.IsValid)
                    return BadRequest(ModelState);
                **// How to invoke _TestService(_data.aaa,_data.bbb);**
                var Result = _TestService.ReturnResult();
                return Ok(Result);

            }
            catch (Exception ex)
            {

                return BadRequest(ex);
            }
        }

    }
  1. Make two interfaces that have a single property/method that returns an int:
interface IAAA
{
    int AAA { get; }
}

interface IBBB
{
    int BBB { get; }
}
  1. Make your TestService constructor accept instances of said interfaces:
public class TestService : ITestService
{
    public TestService(IAAA a, IBBB b)
    {
        this.aaa = a.AAA;
        this.bbb = b.BBB;
    }
}
  1. Create implementations of said interfaces:
public class AAAImpl : IAAA
{
    public int AAA => return 42;
}

public class BBBImpl : IBBB
{
    public int BBB => return 1337;
}
  1. Map the interfaces to their implementations in ConfigureServices in Startup.cs :
services.AddSingleton<IAAA, AAAImpl>();
services.AddSingleton<IBBB, BBBImpl>();

.NET Core's dependency injection will do the rest.

Just remember, interfaces are the answer to everything ;).

Change service Interface to string ReturnResult(int aaa, int bbb); Change controller var Result = _TestService.ReturnResult(_data.aaa, _data.bbb);

normally hold data in service class is not a good design, use method parameter is more better. if you really need store data in service class, you can in controller mannually create new TestService instead of injecting service, but this is even worse design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM