Solved

C# - Interface instantiating question

Posted on 2013-05-21
7
323 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
  • 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 29

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 29

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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