Webapi Application Impelimenting依赖注入

时间:2017-09-17 21:47:51

标签: c# json asp.net-web-api design-patterns dependency-injection

我正在学习在我的应用程序中实现DI和Pattern,并且我已经为自己创建了一些要求 用一堆端点写了一篇WebAPI应用程序。

POST - /api/math/AddTogether

获取JSON / XML对象... { values: [1,2,3,4,5,6,7,8,9] }

返回{ “value” : 45}

POST - /api/math/EvenNumbers`

获取JSON / XML对象... { values: [1,2,3,4,5,6,7,8,9] }

返回{ “values” : [2,4,6,8] }

GET - /api/math/GetRange/{first}/{second}

{first}{second}是值。

如果我要通过/5/10,我期待...... { “values”: [5,6,7,8,9,10] }

尝试使用服务/提供者层 注入依赖 创建单元测试以使用不同的值测试服务层。

在这个过程中,我的DI对我所做的事情毫无用处,请建议我如何更好地使用它,因为我是新手。我的单元测试正在运行,应用程序正在运行,但我不知道看到DI对我的IInputService有任何用处,因为没有依赖关系。

我的数学控制器

[RoutePrefix("api/math")]
public class MathController : ApiController
{
    private readonly InputService _inputService;

    public MathController(InputService inputService)
    {
        _inputService = inputService;
    }

    //api/math/add/
    [HttpPost]
    [Route("Add")]
    public IHttpActionResult AddTogether([FromBody] int[] numArray)
    {
        if (numArray == null)
        {
            return NotFound();
        }
        var result = _inputService.AddValues(numArray);
        return Ok(new
        {
            value = result
        });
    }

    //api/math/even/
    [HttpPost]
    [Route("Even")]
    public IHttpActionResult EvenNumbers([FromBody] int[] numArray)
    {
        if (numArray == null)
        {
            return NotFound();
        }
        var result = _inputService.EvenNumbers(numArray);
        return Ok(new
        {
            value = result
        });
    }

     //api/math/range/?firstVal=5&secondVal=10
    [HttpGet]
    [Route("Range")]
    public Number GetRange(int firstVal, int secondVal)
    {
        var range = _inputService.GetRange(firstVal, secondVal);
        return new Number
        {
            Numbers = range
        };
    }

}

DI实施

`public class IocConfig
{
    public static void RegisterIoc(HttpConfiguration config)
    {
        var kernel = new StandardKernel(); // Ninject IoC
        //kernel.Load(Assembly.GetExecutingAssembly()); //only required for asp.net mvc (not for webapi)
        // These registrations are "per instance request".
        // See http://blog.bobcravens.com/2010/03/ninject-life-cycle-management-or-scoping/

        kernel.Bind<IInputService>().To<InputService>();


        // Tell WebApi how to use our Ninject IoC
        config.DependencyResolver = new NinjectDependencyResolver(kernel);
    }
}

IInputService

 public interface IInputService
{
    int AddValues(int[] numInts);
    List<int> EvenNumbers(int[] numInts);
    List<int> GetRange(int firstVal, int secondVal);
}

public class InputService : IInputService
{
    public int AddValues(int[] numInts)
    {
        var total = numInts.Sum();
        return total;
    }

    public List<int> EvenNumbers(int[] numInts)
    {
        var evens = new List<int>();
        foreach (var num in numInts)
        {
            if (num % 2 == 0)
            {
                evens.Add(num);
            }

        }
        return evens;
    }

    public List<int> GetRange(int firstVal, int secondVal)
    {
        IEnumerable<int> range;
        var displayList = new List<int>();
        int endVal;
        if (firstVal > secondVal)
        {
            endVal = firstVal;
            range = Enumerable.Range(secondVal, firstVal);
        }
        else
        {
            endVal = secondVal;
            range = Enumerable.Range(firstVal, secondVal);
        }

        foreach (var x in range)
            if (x <= endVal)
            {
                displayList.Add(x);
            }
        return displayList;

    }
}

0 个答案:

没有答案