Interfaces with Generics - HELP ?!

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Interfaces with Generics - HELP ?!

Post 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 :-
NFinterface_11.jpg
NFinterface_11.jpg (67.64 KiB) Viewed 385 times
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 :-
NFinterface_01.jpg
NFinterface_01.jpg (61.79 KiB) Viewed 385 times
And here is some test code to use the Interface :-
NFinterface_02.jpg
NFinterface_02.jpg (65.19 KiB) Viewed 385 times
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 :-
NFinterface_03.jpg
NFinterface_03.jpg (26.79 KiB) Viewed 385 times
NFinterface_04.jpg
NFinterface_04.jpg (22.42 KiB) Viewed 385 times
NFinterface_05.jpg
NFinterface_05.jpg (48.46 KiB) Viewed 385 times
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,
Phil.
Wales, UK.
User avatar
Otto
Posts: 174
Joined: Wed Sep 30, 2015 6:22 pm

Interfaces with Generics - HELP ?!

Post by Otto »

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.
NickFriend
Posts: 248
Joined: Fri Oct 14, 2016 7:09 am

Interfaces with Generics - HELP ?!

Post by NickFriend »

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
T.FindMaxID();

HTH

Nick
User avatar
Phil Hepburn
Posts: 743
Joined: Sun Sep 11, 2016 2:16 pm

Interfaces with Generics - HELP ?!

Post by Phil Hepburn »

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.

Regards,
Phil.
Post Reply