Vinicius
Vinicius

Reputation: 61

interfaces and classes in 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,
}

Upvotes: 2

Views: 116

Answers (2)

Paweł Bejger
Paweł Bejger

Reputation: 6366

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).

Upvotes: 8

Sam Leach
Sam Leach

Reputation: 12956

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;
   }
}

Upvotes: 0

Related Questions