简体   繁体   English

C#中的接口和类

[英]interfaces and classes in C#

I get the following error: 我收到以下错误:

test.Services.JobService' does not contain a constructor that takes 0 arguments. test.Services.JobService'不包含带0个参数的构造函数。

this is the code: 这是代码:

JobService.cs: 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: 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: Job.cs:

public class Job
{
    public string Id { get; set; }
    public DateTime CreatedAt { get; set; }
    public JobType Type { get; set; }
}

JobType.cs: 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. 您需要在JobService类中创建一个无参数构造函数(我不会这样做,因为那样您的类将无法正常工作 - 将不会引用任何存储库)或者最好在构造此实例时将Repository实例作为参数传递。

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. 其中Repository应该是一个实现IRepository接口的类。 Without passing this instance your CreateJobBanker will not work. 如果不传递此实例,您的CreateJobBanker将无法正常工作。

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). 虽然建议使用一些IoC容器,因为您的代码遵循Inversion of Control原则(可能您甚至不知道这一点)。

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 . 因为否则您的JobService没有IRepository实例,并且当您尝试在JobService使用您的存储库时将抛出NullReferenceException

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: 在您的代码中,您正在初始化JobService而不给它一个IRepository参数, JobService您的代码编译,您需要在JobService中定义一个没有IRepository参数的构造函数,如果您想这样做:

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: IRepository实施:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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