I am learning to implement DI and Pattern in my application and I have created some requirements for myself Wrote a WebAPI application with a bunch of end points.
POST - /api/math/AddTogether Take a JSON / XML object… { values: [1,2,3,4,5,6,7,8,9] } Return { “value” : 45}
POST - /api/math/EvenNumbers Take a JSON / XML object… { values: [1,2,3,4,5,6,7,8,9] } Return { “values” : [2,4,6,8] }
GET - /api/math/GetRange/{first}/{second} {first} and {second} are values. If I were to pass /5/10 I’d expect… { “values”: [5,6,7,8,9,10] }
Trying to Make use of a service/provider layer Dependency injected Created unit tests to test your service layer with different values.
In this process my DI is looking no use for what I did and please suggest me how can I use it for better as I am new to it.My unit tests are working and application is working but I don't see any use of DI to my IInputService as there are no dependencies.
My Math controller
[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 Implementation
`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://ift.tt/2h8VEZA
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;
}
}
Aucun commentaire:
Enregistrer un commentaire