Solved

C# - Interface instantiating question

Posted on 2013-05-21
7
319 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now