Solved

C# - Interface instantiating question

Posted on 2013-05-21
7
325 Views
Last Modified: 2013-05-22
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(ICustomerService customerService, IGroupService grpService)
     {
               CustomerService = customerService;
               GroupService = grpService;
     }

     //then in my action I can do for example
     var qry = CustomerService.GetAllCustomers(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?
0
Comment
Question by:sbornstein2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Accepted Solution

by:
armchang earned 300 total points
ID: 39186294
hi,

Well, you don't really need to and if I say that, I meant that you didn't have to instantiate from the BaseController because it is not advisable. There are many ways to solve this and the first one is to:

Instantiate like what you did in the BaseController. Pass the interface call and call the method.

public class MyRoleProvider : RoleProvider
{
     protected ICustomerService CustomerService;
     protected IGroupService GroupService;

     public MyRoleProvider(ICustomerService customerService, IGroupService grpService)
     {
               CustomerService = customerService;
               GroupService = grpService;
     }

     var qry = CustomerService.GetAllCustomers(0);

}

or second,

study using the Model-View-Presenter (MVP) design pattern and apply that to the MyRoleProvider class

Thanks,

A
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39186299
In some sense there are no difference between just a class and controller - both are classes.

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.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 39186301
2 armchang

Not quite correct code. The line      

var qry = CustomerService.GetAllCustomers(0);

Open in new window


should be inside of  some method,otherwise you'll have an error.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:armchang
ID: 39186322
@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.
0
 

Author Comment

by:sbornstein2
ID: 39186373
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.
0
 

Author Comment

by:sbornstein2
ID: 39187346
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(ICustomerService customerService, IGroupService grpService)
     {
               CustomerService = customerService;
               GroupService = grpService;
     }

     private User GetCurrentUser()
     {
               var qry = CustomerService.GetAllCustomers(0);  //error happens - Object reference not set to an instance of an object.
     }
}
0
 

Author Closing Comment

by:sbornstein2
ID: 39187927
thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question