interfaces and classes in C# - c#

I get the following error:
test.Services.JobService' does not contain a constructor that takes
0 arguments.
this is the code:
JobService.cs:
namespace TesteUltrafan.Services
{
public class JobService
{
private readonly IRepository _repository;
public JobService(IRepository repository)
{
_repository = repository;
}
public Job CreateJobBanker()
{
var banker = new Job();
string id = Guid.NewGuid().ToString("N");
Console.WriteLine("Novo job banker id: {0}", id);
banker.Id = id;
banker.Type = JobType.Banker;
banker.CreatedAt = DateTime.Now;
Console.WriteLine("Salvando job banker id: {0}", id);
Job jobBanker = _repository.SaveJob(banker);
return jobBanker;
}
}
}
the program.cs:
public class Program
{
public static void Main()
{
var jobService = new JobService(); <---- Here is the error.
Console.WriteLine("Creating job banker, creation:");
Job jobBanker = jobService.CreateJobBanker();
Console.WriteLine("Job banker: {0}", jobBanker);
}
}
Job.cs:
public class Job
{
public string Id { get; set; }
public DateTime CreatedAt { get; set; }
public JobType Type { get; set; }
}
JobType.cs:
public enum JobType
{
Actor = 1,
Agricultural = 2,
Banker = 3,
EngineerCivil = 4,
Another = 5,
}

You either need to create a parameterless constructor in your JobService class (which I would not do because then your class will not work correctly - there will be no reference to any repository) or preferably pass a Repository instance as a parameter when constructing this instance.
So the code should preferably look like this:
public class Program
{
public static void Main()
{
var jobService = new JobService(new Repository());
Console.WriteLine("Creating job banker, creation:");
Job jobBanker = jobService.CreateJobBanker();
Console.WriteLine("Job banker: {0}", jobBanker);
}
}
Where Repository should be a class implementing IRepository interface. Without passing this instance your CreateJobBanker will not work.
What would be though recommended is to use some IoC Container, because your code follows the Inversion of Control principle (probably you are not even aware of this).

Use
var jobService = new JobService(new Repository());
because otherwise your JobService with not have an IRepository instance and will throw a NullReferenceException when you try to use your repository inside JobService.
In your code, you are initialising JobService without giving it a IRepository argument, to get your code to compile you need to define a constructor in JobService that does not have an IRepository argument if you want to do that:
public class JobService
{
private readonly IRepository _repository;
// New Constructor will make _repository null
public JobService()
{
}
public JobService(IRepository repository)
{
_repository = repository;
}
public Job CreateJobBanker()
{
var banker = new Job();
string id = Guid.NewGuid().ToString("N");
Console.WriteLine("Novo job banker id: {0}", id);
banker.Id = id;
banker.Type = JobType.Banker;
banker.CreatedAt = DateTime.Now;
Console.WriteLine("Salvando job banker id: {0}", id);
// NullReferenceException if you define a parameterless constructor
Job jobBanker = _repository.SaveJob(banker);
return jobBanker;
}
}
Now you can construct it in two ways:
var jobService = new JobService();
or
var jobService = new JobService(new Repository());
IRepository implementation:
public class Repository: IRepository
{
private MyDbEntities context = new MyDbEntities();
public Job SaveJob(Job job)
{
// assuming Entity Framework
var id = job.Id;
if (context.Jobs.Any(e => e.Id == id))
{
context.Jobs.Attach(job);
context.ObjectStateManager.ChangeObjectState(jobs, EntityState.Modified);
}
else
{
context.Jobs.AddObject(myEntity);
}
context.SaveChanges();
return job;
}
}

Related

How to make the ActivatorUtilities.CreateInstance method use a different constructor?

Is there a way the tell the ActivatorUtilities.CreateInstance<T>(IServiceProvider serviceProvider); method to try to use other constructors if the first one can't be constructed?
I have a class with multiple constructors:
public ViewModelB(SomeDependency someDependency): this one only takes SomeDependency which is registered in a DI container
public ViewModelB(SomeDependency someDependency, GetUserRequest request): this one takes SomeDependency which is registered in a DI container and a GetUserRequest which has to be passed in manually
And I'm trying to activate them and resolve dependencies like so:
IServiceProvider serviceProvider; //this gets passed from somewhere
Guid userId; //this gets passed manually by the caller
//works
var instanceAWithoutParams = ActivatorUtilities.CreateInstance<ViewModelA>(serviceProvider);
//works
var instanceAWithParams = ActivatorUtilities.CreateInstance<ViewModelA>(serviceProvider, new[] { new GetUserRequest { UserId = userId } });
//does NOT work, it tries to use the first constructor and fails
var instanceBWithoutParams = ActivatorUtilities.CreateInstance<ViewModelB>(serviceProvider);
//works
var instanceBWithParams = ActivatorUtilities.CreateInstance<ViewModelB>(serviceProvider,, new[] { new GetUserRequest { UserId = userId } });
The activation of instanceBWithoutParams fails because it can't resolve the request parameter. It tries to use the first constructor and doesn't check other ones when the activation fails.
Here's what the services look like, they're the same with one difference: the order of the constructors.
public class ViewModelA
{
private readonly SomeDependency _someDependency;
private readonly GetUserRequest? _request;
public ViewModelA(SomeDependency someDependency)
{
_someDependency = someDependency;
}
public ViewModelA(SomeDependency someDependency, GetUserRequest request)
{
_someDependency = someDependency;
_request = request;
}
}
public class ViewModelB
{
private readonly SomeDependency _someDependency;
private readonly GetUserRequest? _request;
public ViewModelB(SomeDependency someDependency, GetUserRequest request)
{
_someDependency = someDependency;
_request = request;
}
public ViewModelB(SomeDependency someDependency)
{
_someDependency = someDependency;
}
}
public class GetUserRequest
{
public Guid UserId { get; set; }
}
Thanks.
I struggled with the same issue. Eventually I came up with this solution:
I would use something like a factory which is able to construct ServiceB by calling a method.
For example:
var serviceBFactory = ActivatorUtilities.CreateInstance<ServiceBFactory>(serviceProvider);
var instanceBWithoutParams = serviceBFactory.CreateServiceB();
var instanceBWithParams = serviceBFactory.CreateServiceB(new Request());
This way you keep you DI clean. But this means that the ServiceBFactory need to know which services need to be injected in a ServiceB. (so that will be a tight coupling) They come as a package.
I've chosen to re-design the view models instead of trying to pass optional parameters next to services from DI (thanks to Steven for the helpful articles: 1 and 2).
There also seems to be no way of making the ActivatorUtilities.CreateInstance<T>(IServiceProvider serviceProvider); method try other constructors after one fails, so here's what my edited solution looks like.
I've moved the initialization of the optional parameter out of the constructor, that way I only have one constructor that only takes injectables. The parameter is then passed separately via the TakeParameter method. The only downside I can think of is that the parameter can no longer be readonly and I can live with that.
My custom activator utility:
public interface IAcceptParameter<T>
{
void TakeParameter(T parameter);
}
public static class CustomActivator
{
public static T CreateInstance<T>()
{
return ActivatorUtilities.CreateInstance<T>(_serviceProvider);
}
public static T CreateInstanceWithParam<T, K>(K parameter) where T : IAcceptParameter<K>
{
var instance = ActivatorUtilities.CreateInstance<T>(_serviceProvider);
instance.TakeParameter(parameter);
return instance;
}
}
Changed view model
public class SomeViewModel : IAcceptParameter<Guid>
{
private readonly SomeDependency _someDependency;
private Guid? _userId;
public SomeViewModel(SomeDependency someDependency)
{
_someDependency = someDependency;
}
public void TakeParameter(Guid parameter){
_userId = parameter;
}
}
How I use it
var instanceWithoutParam = CustomActivator.CreateInstance<SomeViewModel>(serviceProvider);
Guid userId;
var instanceWithParam = CustomActivator.CreateInstanceWithParam<SomeViewModel, Guid>(serviceProvider, userId);
Let say you have a class like this:
public class a
{
public string p { get; set; }
public a()
{
p = "default constructor";
}
public a(string pv)
{
p = pv;
}
}
You can use .GetConstructor method to use a specific constructor:
public class Program
{
static void Main(string[] args)
{
var c = typeof(a).GetConstructor(new Type[] { typeof(string) });
if (c != null)
{
var myA = (a)c.Invoke(new object[] { "new value" });
Console.WriteLine($"Value of p is {myA.p}");
}
}
}

C# Moq method in abstract class

Can someone show me how I can mock the result of a method in a base abstract class? See the very basic sample code below to demonstrate the problem and I need to mock the “GetAge()” method’s result. See the commented line at the end with the ending "<----- FIX here" where I think need to add the fix.
Customer Service
public interface ICustomerService
{
string GetCustomerDetailsSrv(int age);
}
public class CustomerService : ICustomerService
{
public string GetCustomerDetailsSrv(int age)
{
return $"Name: John Doe. Age: {age}";
}
}
Base Controller
public abstract class MyBaseController : ControllerBase
{
public virtual int GetAge()
{
return 7;
}
}
Customer Controller
public class CustomerController : MyBaseController
{
private readonly ICustomerService _customerSrv;
public CustomerController(ICustomerService customerSrv)
{
_customerSrv = customerSrv;
}
[HttpGet]
public string GetCustomerDetails()
{
var age = GetAge();
var result = _customerSrv.GetCustomerDetailsSrv(age);
return result;
}
}
Customer Controller Test
[TestClass]
public class CustomerControllerTest
{
private readonly CustomerController _controller;
private readonly Mock<ICustomerService> _mockSrv;
public CustomerControllerTest()
{
_mockSrv = new Mock<ICustomerService>();
_controller = new CustomerController(_mockSrv.Object);
}
[TestMethod]
public void TestGet()
{
//Arrange
int mockAge = 11;
string expectedResult = $"Name: Alice Smith. Age: {mockAge}";
// _controller.Setup(Controller => Controller.GetAge()).Returns(mockAge); <----- FIX here
_mockSrv.Setup(repo => repo.GetCustomerDetailsSrv(mockAge)).Returns(expectedResult);
//Act
var actualResult = _controller.GetCustomerDetails();
//Assert
Assert.IsTrue(actualResult == expectedResult);
}
}
I think the following code achieves what you want.
Creating a Mock from a CustomerController allows the setup the virtual method GetAge while still being able to use the GetCustomerDetails method from the CustomerController class.
[TestClass]
public class CustomerControllerTest
{
private readonly Mock<CustomerController> _mockController;
private readonly Mock<ICustomerService> _mockSrv;
public CustomerControllerTest()
{
_mockSrv = new Mock<ICustomerService>();
_mockController = new Mock<CustomerController>(() => new CustomerController(_mockSrv.Object));
}
[TestMethod]
public void TestGet()
{
//Arrange
int mockAge = 11;
string expectedResult = $"Name: Alice Smith. Age: {mockAge}";
_mockController.Setup(Controller => Controller.GetAge()).Returns(mockAge);
_mockSrv.Setup(repo => repo.GetCustomerDetailsSrv(mockAge)).Returns(expectedResult);
//Act
var actualResult = _mockController.Object.GetCustomerDetails();
//Assert
Assert.IsTrue(actualResult == expectedResult);
}
}

Mocking IEnumerable<T> for each class that implements <T> using Moq

I have the following interface
public interface ICommand<TResult, TModel>
{
Task<TResult> DoWorkAsync(TModel model);
}
Which is implemented by one or more such Command classes:
public class MyCommand1 : ICommand<Response, Model>()
{
public async Task<Response> DoWorkAsync(Model model) {
// do something
}
}
public class MyCommand2 : ICommand<Response, Model>()
{
public async Task<Response> DoWorkAsync(Model model) {
// do something else
}
}
The Respose and Model classes are as follows:
public class Response
{
public bool IsSuccessful {get;set;}
}
public class Model
{
public Guid Id {get;set;}
public string Name {get;set;}
}
Then I have an orchestrator class that has a dependency on an IEnumerable of IEnumerable<ICommand<Response, Model>>
public class MyOrchestrator
{
private readonly IEnumerable<ICommand<Response, Model>> _commands;
public MyOrchestrator(IEnumerable<ICommand<Response, Model>> commands)
{
_commands = commands;
}
public async Task ExecuteAsync(Model model)
{
myCommand1_Response = await _commands
.OfType<MyCommand1>()
.First()
.DoWorkAsync(model);
myCommand2_Response = await _commands
.OfType<MyCommand2>()
.First()
.DoWorkAsync(model);
// other operations
}
}
Now in my test I'm trying to mock the MyOrchestrator class's dependency IEnumerable<ICommand<Response, Model>> for each of the MyCommand types. How can I achieve this?
The problem is that MyCommand2 and MyCommand1 are concrete classes. You'll either need to make them be IMyCommand1 and IMyCommand2 or make DoWorkAsync virtual.
I think you could simplify your orchestator which would make mocking trivial.
public class MyOrchestrator
{
...
public MyOrchestrator(ICommand<Response, Model> command1, ICommand<Response, Model> command2)
{
this.command1 = command1 ?? throw...;
this.command2 = command2 ?? throw...;
}
public async Task ExecuteAsync(Model model)
{
myCommand1_Response = await command1.DoWorkAsync(model);
myCommand2_Response = await command1.DoWorkAsync(model);
// other operations
}
}
Now mocking this is nothing special.
var mockCommand1 = new Mock<ICommand<Response, Model>>(MockBehaviour.Strict);
...
var tested = new MyOrchestrator(command1: mockCommand1.Object, ...);

How to decorate one specific type with Autofac?

Using the CQS pattern I have a query like this:
public class MyQuery : IQuery<View<SingleView>>
{
public string Token { get; set; }
public Criteria Criteria { get; set; }
}
public class MyQueryHandler : IQueryHandler<MyQuery, View<SingleView>>
{
private readonly IProductRepository _productRepository;
public MyQueryHandler(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public View<SingleView> Handle(MyQuery query)
{
var data = // get my data
return data;
}
}
Off course I have many queries and their belonging QueryHandlers. Now I have a situation that I want to decorate only the MyQueryHandler. I don't want to decorate the rest of the queries.
I am using Autofac, but I just can't get it to work.
Here is how the queries are registered:
builder.RegisterAssemblyTypes(assemblies)
.As(type => type.GetInterfaces()
.Where(interfaceType => interfaceType.IsClosedTypeOf(typeof(IQueryHandler<,>)))
.Select(interfaceType => new KeyedService("queryHandler", interfaceType)));
// Register query decorators
builder.RegisterGenericDecorator(
typeof(LogQueryDecorator<,>),
typeof(IQueryHandler<,>),
"queryHandler");
And here is the decorator I want to use for MyQueryHandler:
public class SaveMyQueryData : IQueryHandler<MyQuery, View<SingleView>>
{
private readonly IQueryHandler<MyQuery, View<SingleView>> _queryHandler;
private readonly IProductRepository _productRepository;
public SaveMyQueryData(
IQueryHandler<MyQuery, View<SingleView>> queryHandler,
IProductRepository productRepository)
{
_queryHandler = queryHandler;
_productRepository = productRepository;
}
public View<SingleView> Handle(MyQuery query)
{
var result = _queryHandler.Handle(query);
// do something with result
return result;
}
}
So how can I register SaveMyQueryData as a decorator just for MyQueryHandler using Autofac?
I finally have it working the way I want. Here is my solution:
Make the decorator open instead of a closed type:
public class SaveMyQueryData : IQueryHandler<Q, R>
{
private readonly IQueryHandler<Q, R> _queryHandler;
private readonly IProductRepository _productRepository;
public SaveMyQueryData(
IQueryHandler<Q, R> queryHandler,
IProductRepository productRepository)
{
_queryHandler = queryHandler;
_productRepository = productRepository;
}
public Q Handle(Q query)
{
var result = _queryHandler.Handle(query);
var view = result as View<SingleView>;
// do something with view
return result;
}
}
Then when registering the type using Autofac, do this:
builder.RegisterGenericDecorator(
typeof(SaveMyQueryData<,>),
typeof(IQueryHandler<,>),
context => context.ImplementationType == typeof(MyQueryHandler));
The last line contains the magic. With the third parameter of RegisterGenericDecorator you can specify a condition when to apply the decorator. This is new since Autofac 4.9 and this solved my problem.

How to mock IRepository<T>?

I want to mock a unit of work interface with repositories inside for unit test purpose. So far I am able to do it like below.
namespace Liquid.Service.UnitTest
{
using Liquid.DataAccess.Interface;
using Liquid.Domain;
using Domain.Interface;
using Moq;
using System.Collections.Generic;
using System.Linq;
internal class Helper
{
internal Mock<IUnitOfWork> MockUnitOfWork(ICollection<Dummy> dummies = null,
ICollection<ProductType> productTypes = null)
{
dummies = dummies ?? new List<Dummy>();
productTypes = productTypes ?? new List<ProductType>();
var dummyRepositoryMock = MockDummyRepository(dummies);
var productTypeRepositoryMock = MockProductTypeRepository(productTypes);
var unitOfWorkMock = new Mock<IUnitOfWork>();
unitOfWorkMock.Setup(x => x.DummyRepository)
.Returns(dummyRepositoryMock.Object);
unitOfWorkMock.Setup(x => x.ProductTypeRepository)
.Returns(productTypeRepositoryMock.Object);
return unitOfWorkMock;
}
private Mock<IDummyRepository> MockDummyRepository(ICollection<Dummy> dummies)
{
var dummyRepositoryMock = new Mock<IDummyRepository>();
dummyRepositoryMock.Setup(x => x.FindById(It.IsAny<int>()))
.Returns((int arg1) => dummies.Where(x => x.Id == arg1).SingleOrDefault());
dummyRepositoryMock.Setup(x => x.Add(It.IsAny<Dummy>()))
.Callback((Dummy arg1) => dummies.Add(arg1));
return dummyRepositoryMock;
}
private Mock<IProductTypeRepository> MockProductTypeRepository(ICollection<ProductType> productTypes)
{
var productTypeRepositoryMock = new Mock<IProductTypeRepository>();
productTypeRepositoryMock.Setup(x => x.FindById(It.IsAny<int>()))
.Returns((int arg1) => productTypes.SingleOrDefault(x => x.Id == arg1));
productTypeRepositoryMock.Setup(x => x.Add(It.IsAny<ProductType>()))
.Callback((ProductType arg1) => productTypes.Add(arg1));
return productTypeRepositoryMock;
}
}
}
You see that I've created two method to mock DummyRepository and ProductTypeRepository but because it has same implementation, I think it is redundant for every repositories I have.
Below is the Repositories and IRepository code.
namespace Liquid.DataAccess.Interface
{
using Liquid.Domain;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public interface IDummyRepository : IRepository<Dummy>
{
}
public interface IProductTypeRepository : IRepository<ProductType>
{
}
public interface IRepository<TEntity> where TEntity : class
{
IList<TEntity> GetAll();
Task<List<TEntity>> GetAllAsync();
Task<List<TEntity>> GetAllAsync(CancellationToken cancellationToken);
IList<TEntity> PageAll(int skip, int take);
Task<List<TEntity>> PageAllAsync(int skip, int take);
Task<List<TEntity>> PageAllAsync(CancellationToken cancellationToken, int skip, int take);
TEntity FindById(object id);
Task<TEntity> FindByIdAsync(object id);
Task<TEntity> FindByIdAsync(CancellationToken cancellationToken, object id);
void Add(TEntity entity);
void Update(TEntity entity);
void Remove(TEntity entity);
}
}
How can I use a same method to mock every repositories implementation which inherits IRepository?
UPDATE :
The test is just a simple add and check like below.
[Test]
public void ProductTypeService_Add()
{
// GIVEN
var productTypeData = new ProductType()
{
Id = 1,
Description = "ProductType1"
};
// WHEN
var unitOfWorkMock = new Helper().MockUnitOfWork();
var productTypeService = new ProductTypeService(unitOfWorkMock.Object);
productTypeService.Add(productTypeData);
unitOfWorkMock.Verify(x => x.SaveChanges());
// THEN
Assert.That(productTypeService.FindById(1) != null);
Assert.That(productTypeService.FindById(2) == null);
// WHEN
var productTypeData2 = new ProductType()
{
Id = 2,
Description = "ProductType2"
};
productTypeService.Add(productTypeData2);
// THEN
Assert.That(productTypeService.FindById(2) != null);
}
IMHO you are testing the wrong thing; namely you are testing that an in-memory collection (a List<T>) can store data and the data can be found in the collection. This always yields true because that is the purpose of in-memory collections.
Instead of doing this you either need to create integration tests which will use the actual implementation of the underlying repository (such as Entity Framework) or just test the behavior of your service like this:
[Test]
public void ProductTypeService_WhenAddingNewItem_CallsSaveChanges()
{
var unitOfWork = new Mock<IUnitOfWork>();
// setup the properties of the mock here...
var service = new ProductTypeService(unitOfWork);
service.Add(new ProductType { Id = 2, Description = "some product" });
unitOfWork.AssertWasCalled(_ => _.SaveChanges());
}
This way, you test that your service calls SaveChanges() method; actually saving the data is the responsibility of the repository and as I said above, testing that a list can store data in memory is useless.
I think you have over complicated your question and thus your solution. You don't need interfaces for your various repositories like IDummyRepository and IProductRepository if you are not adding any value to it.
Your data classes
public class Dummy
{
public int Id { get; set; }
}
public class ProductType
{
public int Id { get; set; }
}
Your ProductTypeService (I can only assume this)
public class ProductTypeService
{
private readonly IUnitOfWork _unitOfWork;
public ProductTypeService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public void AddProductType(ProductType productType)
{
_unitOfWork.ProductTypes.Add(productType);
}
}
Your IUnitOfWork
public interface IUnitOfWork
{
IRepository<Dummy> Dummies { get; set; }
IRepository<ProductType> ProductTypes { get; set; }
}
Your IRepository remains unchanged so I won't copy paste it here!
Finally your unit test
[TestFixture]
public class Class1
{
private Mock<IUnitOfWork> _unitOfWorkMock;
private Mock<IRepository<Dummy>> _dummyRepositoryMock;
private Mock<IRepository<ProductType>> _productTypeRepositoryMock;
[SetUp]
public void Setup()
{
_unitOfWorkMock = new Mock<IUnitOfWork>();
_dummyRepositoryMock = CreateMock<Dummy>();
_productTypeRepositoryMock = CreateMock<ProductType>();
_unitOfWorkMock.Setup(u => u.Dummies).Returns(_dummyRepositoryMock.Object);
_unitOfWorkMock.Setup(u => u.ProductTypes).Returns(_productTypeRepositoryMock.Object);
}
[Test]
public void product_type_service_should_add_item_to_the_underlying_repository()
{
var productTypeService = new ProductTypeService(_unitOfWorkMock.Object);
var productType = new ProductType {Id = 10};
productTypeService.AddProductType(productType);
_productTypeRepositoryMock.Verify(r => r.Add(It.Is<ProductType>(p => p.Id == productType.Id)), Times.Once());
}
private Mock<IRepository<T>> CreateMock<T>() where T : class
{
var mock = new Mock<IRepository<T>>();
// do more common magic here
return mock;
}
}

Categories