Solved

C# - Interface instantiating question

Posted on 2013-05-21
7
327 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

751 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