sbornstein2
asked on
C# - Interface instantiating question
Hello all,
I have a question in regard to calling a method signature in my interface. We have a BaseController class where in that we have interfaces defined such as:
protected ICustomerService CustomerService;
protected IGroupService GroupService;
Then in controllers in an MVC project we instantiate in the constructor for example:
public class HomeController : BaseController
{
public HomeController(ICustomerSe rvice customerService, IGroupService grpService)
{
CustomerService = customerService;
GroupService = grpService;
}
//then in my action I can do for example
var qry = CustomerService.GetAllCust omers(0);
}
May not be 100% on syntax but that should make sense. Now I have a class I am using to implement a custom role provider for the project. Such as this:
public class MyRoleProvider : RoleProvider
{
}
How do I instantiate the same interfaces as done in the controller example? I can't inherit the BaseController in this case it is outside my controller and it would not make sense. This is just a class nothing to do with controllers. Hope this makes some sense. :). Just need to see how can I call the GetAllCustomers in my CustomerService interface in the MyRoleProvider class here?
I have a question in regard to calling a method signature in my interface. We have a BaseController class where in that we have interfaces defined such as:
protected ICustomerService CustomerService;
protected IGroupService GroupService;
Then in controllers in an MVC project we instantiate in the constructor for example:
public class HomeController : BaseController
{
public HomeController(ICustomerSe
{
CustomerService = customerService;
GroupService = grpService;
}
//then in my action I can do for example
var qry = CustomerService.GetAllCust
}
May not be 100% on syntax but that should make sense. Now I have a class I am using to implement a custom role provider for the project. Such as this:
public class MyRoleProvider : RoleProvider
{
}
How do I instantiate the same interfaces as done in the controller example? I can't inherit the BaseController in this case it is outside my controller and it would not make sense. This is just a class nothing to do with controllers. Hope this makes some sense. :). Just need to see how can I call the GetAllCustomers in my CustomerService interface in the MyRoleProvider class here?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
2 armchang
Not quite correct code. The line
should be inside of some method,otherwise you'll have an error.
Not quite correct code. The line
var qry = CustomerService.GetAllCustomers(0);
should be inside of some method,otherwise you'll have an error.
@anarki_jimbel
Yes, I know. That is why he had a comment on it, that it should be put inside a method that returns an ActionResult but thanks anyway.
Yes, I know. That is why he had a comment on it, that it should be put inside a method that returns an ActionResult but thanks anyway.
ASKER
I will try these tomorrow. I was getting an object not set to an instance of an object type error I think though Armchang on the GetCustomer call trying like what you have but I will check again tomorrow. I had to add a blank parameter constructor then below it like what you have but the call was not getting into it with an Object ref error. I will check again.
ASKER
Yes these don't work. The role provider is defined in the web config and it says 'no parameter less constructor' So I add that such as this:
public class MyRoleProvider : RoleProvider
{
protected ICustomerService CustomerService;
protected IGroupService GroupService;
public MyRoleProvider()
{
}
public MyRoleProvider(ICustomerSe rvice customerService, IGroupService grpService)
{
CustomerService = customerService;
GroupService = grpService;
}
private User GetCurrentUser()
{
var qry = CustomerService.GetAllCust omers(0); //error happens - Object reference not set to an instance of an object.
}
}
public class MyRoleProvider : RoleProvider
{
protected ICustomerService CustomerService;
protected IGroupService GroupService;
public MyRoleProvider()
{
}
public MyRoleProvider(ICustomerSe
{
CustomerService = customerService;
GroupService = grpService;
}
private User GetCurrentUser()
{
var qry = CustomerService.GetAllCust
}
}
ASKER
thanks
In your controller class there is no instantiation for ICustomerService interface. You just pass an instance of ICustomerService into a constructor. ICustomerService and IGroupService objets are instantiated somewhere else (obviously, there are classes that implemeny these interfaces).
Similarly, in your RoleProvider or MyRoleProvider you may add a constructor that takes ICustomerService as an argument.