Implementing Interface and initialise it in MVC Controller

3.4k Views Asked by At

I am working with the web application and I have a class which have all the business logic which implement Interface. I am trying to referenced Interface and class in startup.cs and trying to initialise in Controller. My business logic class is like this:

 public class User_MasterDataAccesss: IUserMasterRepository
 {
   .... business logic
   public int validate(string name)
   {
     ...... doing something
   }
 }

I have an interface which have definition for the validate function like this

public interface IUserMasterRepository
{
    int validate(string name);
}

I am trying to bind the interface in startup.cs like this

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
    }
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.           
       // services.AddMvc();

        // Add application services.
        services.AddSingleton<IUserMasterRepository, User_MasterDataAccesss>();

    }
}

and I am trying to initialise the interface in controller like this

public class AuthenticationController : Controller
{
    private  IUserMasterRepository UserMasterRepository;
    public AuthenticationController() { }
    public AuthenticationController(IUserMasterRepository userMasterRepository)
    {
        this.UserMasterRepository = userMasterRepository;
    }
    ......... action result actions which is using the UserMasterRepository 

 }

My problem is as follows:

  1. Constructor is not executing and as a result UserMasterRepository is null when accessing from action.
  2. services.AddMvc(); in startup.cs is doesnot exist
  3. I know I can achieve my requirement by using Ninject but I want to use startup.cs file for that matter
  4. I do not want to use new key word in controller.

How can I achieve this? Where I am wrong?

1

There are 1 best solutions below

0
Iswar On BEST ANSWER

Installing ASP.NET 5 runtime and tooling solve my problem.