× Share your code snippets, screen shots etc. here

Interfaces with Generics - HELP ?!

1 year 9 months ago #1 by Phil Hepburn
Interfaces with Generics - HELP ?! was created by Phil Hepburn
Hi Nick, Frank, all guys,

I am following your suggestions and now trying to integrate some suitable Generic Interfaces (of my own making) into my demo code for the 'Stock' sample for Cologne. So I am adapting an existing app.

At the moment I am unsure as to where to place / use the Interface, should it be in each of my business classes, like 'Customer' and 'Order', or in a Base class like 'DataAccess', which I use in some other way in the body of my code.

Let me show you what I have done so far, and you can then advise and criticise me.

Here is what I came up with when I considered my business classes, these methods could be applied to all my classes :-

First of all, I am unsure as to whether I have/need <T> in the method definition / declarations.

For the moment and some early testing I came up with this code seen below :-

And here is some test code to use the Interface :-

For now, I have simply sub-classed my main Order class to 'MyOrder' and implemented the Interface.

For the moment I am just returning dummy test data - with no processing code. And here are some runtime results :-

So guys, my questions are, do I need to have the 'T' in my methods ?

Am I best in implementing the interface in the business classes themselves - or another class altogether ?

Basically HELP !!! I feel close but it feels as if I am going about things wrongly.

Try to give me an answer which is understandable to a newbie to Generic Interfaces, so I can get it working and also explain in in my eNotes that I am writing.

Fingers crossed,

Cheers, and Best regards,
Wales, UK.

Please Log in or Create an account to join the conversation.

1 year 9 months ago #2 by Otto Christiaanse
Replied by Otto Christiaanse on topic Interfaces with Generics - HELP ?!
I'm not sure if I'm correct, but I think it is as follows:
If the class knows the generic, the methods doesn't need to specify it.
If the class doesn't know the generic parameter you want to use in the method, you have to specify it in the method.

Please Log in or Create an account to join the conversation.

1 year 9 months ago #3 by Nick Friend
Replied by Nick Friend on topic Interfaces with Generics - HELP ?!
Hi Phil,

Most likely you won't need type declarations in your methods, because the compiler will already know what T is from the class declaration. The only reason you'd need to add a type parameter in a method signature would be if it's a type that hasn't been declared at the class level.

So the second interface is likely to be the correct approach.

As to exactly at which level to implement the interface, you need to start writing some code and let the logic guide you. It's very likely that you'll end up with a chain of generic classes... so somewhere further down the line you'll have another generic class with <T> where T is IStockEF<T>

You'll then be able to write code like



Please Log in or Create an account to join the conversation.

1 year 9 months ago #4 by Phil Hepburn
Replied by Phil Hepburn on topic Interfaces with Generics - HELP ?!
Thanks Nick,

I had a feeling that it would turn out like you say.

I also realise that I will only get the feeling by testing, and trial and error.

I think its fair to say I have got the general feel for the syntax etc., just don't as yet know how to apply it to my business problems, and my Entity Framework data needs.

For the moment, your extensive DevShare 2015 sample is a bit to complex for me to work back from - until I get more hands on experience. making it work in X# is not the same things as understanding its structure, purpose, of build-up ;-0)

I will re-apply myself in the morning.


Please Log in or Create an account to join the conversation.