Solved

C# - Interface instantiating question

Posted on 2013-05-21
7
329 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

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!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

628 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