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:
- Constructor is not executing and as a result
UserMasterRepository
is null when accessing from action. services.AddMvc();
in startup.cs is doesnot exist- I know I can achieve my requirement by using Ninject but I want to use startup.cs file for that matter
- I do not want to use new key word in controller.
How can I achieve this? Where I am wrong?