0

suppose that i have a base class

class Document
{
//some properties
}

and two derived classes:

public class Order:Document
    {
    //some properties   
    }
public class Request:Document
{
    //some properties   
}

now i want make a layered application that have a service layer. also i want have a base service layer interface such as:

public interface IBaseService<T> where T:Document
{
    IList<T> GetAll();
}

that my layer service classes implement it:

public SaleService:IBaseService<Document>;

So i must define IList<Document> GetAll() to implement base service, but i want my SaleService has IList<Order> GetAll() and IList<Request> GetAll() methods, instead IList<Document> GetAll(). how can i do it?

4
  • You can't because you set one IList<T> method in IBaseService<T> interface. Commented Apr 14, 2013 at 15:51
  • It sounds like you have to have two different GetAll() methods on the same class, with different return types. You can't do that in C#. Please try to clarify your question. Commented Apr 14, 2013 at 15:52
  • yeah i have two different GetAll() methods on same class with different return types. Commented Apr 14, 2013 at 15:55
  • You should simply use public SaleService:IBaseService<Order> ...; , otherwise the <T> parameter is superfluous. Commented Apr 14, 2013 at 16:01

1 Answer 1

4

You will need to implement IBaseService<T> explicitly with the different generic parameters:

public class SalesService : IBaseService<Order>, IBaseService<Request>
{
    IList<Order> IBaseService<Order>.GetAll()
    {
        //return orders
    }

    IList<Request> IBaseService<Request>.GetAll()
    {
        //return requests
    }
}

The implemented methods are only visible through the interface, so you'll have to cast to call them e.g.

IBaseService<Request> service = new SalesService();
IList<Request> request = service.GetAll();

Your client code should only depend on the IBaseService<T> interface, so you shouldn't need to do this very often.

Sign up to request clarification or add additional context in comments.

3 Comments

but by implementing explicitly IBaseService<Order> and IBaseService<Request>, modifier of GetAll methods are private and i couldn't use them on my client side code. and if change modifiers of GetAlls to public, i get this error:"The modifier 'public' is not valid for this item"
@Masoud - Yes, they're only visible through the interface so you'll need to cast - see the update. You can implement one of the IBaseService<T> interfaces implicitly if you want to call GetAll on the SalesService class directly.
thanks, i tested it, it worked. also it worked with IBaseService<Request> service = new SalesService(); but another question, how can i declare IList<Document> instead IList<Request> in last line of code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.