Welcome to this solution for the C-sharp generic Task Management challenge.
So the first thing that we want to do is we want to create an interface.
So right click on your application click on add and then simply go to.
Or we can do it as a class.
That's fine.
And then here we can click on interface right.
And then call it AI tasks.
Now let's think about that.
We have two parts here.
Every task should return a result right.
And in our interface we want to provide a method so that every thing that implements this interface
has a method called perform because we can perform a task.
Right.
So let's turn this interface into a generic one.
Simply go ahead, add angle brackets and write down um t result for type result.
Because as I've said, each task returns a result.
The contract that we want to build here is that we have a method which returns t result type, whatever
that type is.
Right.
And it's called perform.
So that's a method with the return type set to t result of our generic interface.
Great.
So that's the first part.
Now let's set up our two tasks that we have for our application.
That will be an email task and the report task.
So let's create a new class.
Right click on the project.
Click on add and let's call it Email Tasks.
Let's hit enter.
And now start using that interface that we have created.
Internal class email task is using.
So add a colon our AI task generic interface for sure we have to specify type.
And for the sake of this challenge or this project, every task that we have, no matter if it's the
email task or the report task, returns a string.
So let's write down string.
Now, since we have to build the contract, let's take a look here.
He's complaining that we are not implementing the perform method.
So what we can do here is we can simply create a public string.
Right.
We have to return a string.
Perform.
Just like that.
Let's return an empty string just for now okay.
So the error is gone.
That's fine.
Let's add two properties here.
The first prop right down prop will be a string called message.
And the second property right down prop again string will be called recipient.
Cool.
Now let's just create that string here.
Let's interpolate.
So add a dollar sign and then say email send to.
Then put in the recipient with message.
And then put in the message here.
Just like that.
Great.
Now that will be our first task.
It uses the generic interface that you can see right here.
So we have to make sure that our perform method returns a string.
Because that interface I task that we have created says that the perform method has to return the type
that we use for our generic interface.
So if we create a task with the interface set to string, our perform method has to return a string.
Two we have already discovered that in previous lectures, right?
It's just another context.
Now.
Now let's create our second task which is the report task.
Right click on your application or in your project add class report tasks internal class report task.
Again it uses the I task again with the string.
Let's create our method here public string perform.
We simply return an empty string for now.
And then we have one property right down prop.
It's a string report name.
Report interpolate report name generated successfully.
Great.
So again, to sum it up, we have two tasks the email task.
If it performs, we'll get a message email sent.
Right.
We're just uh, well, simulating the operations for sure because we are just focusing on the architecture.
And for the report task we just generate a string report and then the report name generated successfully.
Great.
So we know already know that all the tasks that we are creating in future for our application should
use the I task interface so that we are really typesafe and that we have all the methods that we need.
So let's move on.
We need a task processor.
So go ahead and add a new C sharp class.
Let's call it Task Processor dot CS.
And this is our generic class that processes our tasks.
We have two generic parameters here.
So let's call it t task for type task.
And the second one would be t result.
Then we set up a constraint.
Here we say where t task is using our I task interface that we have right here that uses our t result.
So just to sum it up we generate a generic class.
Then we have two types defined for that generic class t task and t result.
And we say the constraint is where t task.
So the first one here is using our I task interface.
Right.
And for the type of that interface because it's a generic interface you can see it right here I task
t result right.
It's a generic interface.
We submit whatever type we are setting here as the second type parameter or argument for our generic
class.
All right.
So I know that's already pretty advanced.
But you know we're talking about generics.
One of the most advanced topics in C sharp.
So if there is a time to dig deep and to build complex stuff, time is now.
Great.
Now let's just add three things.
The first one would be just any kind of field that stores the information about the current task, a
constructor that sets the current task, and then finally a method that executes the task.
So it simply calls the perform method on any kind of task, no matter if it's the report task or the
email task, any kind of task that implements our generic interface.
So private T task our first generic parameter here, or first generic type private t task.
And we want to call it task.
Then we create a constructor Ctor tab and then task processor.
What we want to do here is we take our t task call it task.
Then we set our field.
So task right here can say this dot task equals to task right.
They share the same name task.
And task.
But this refers to task processor the instance and to this field right here.
So if I hover above that you can see that it's a field.
So it is this one here.
And if I hover above that one task on the right side it's the parameter.
So that one here don't get confused.
We could also say just to make it obvious we quick task param.
Right then it would be this one.
So task equals to task param totally fine if you want to do that.
Um but as I said like you should get used to share the same name in the parameter as for field for example.
And using this instead, or what you can find sometimes would be an underscore here.
Then you would say task equals to task.
This is also something that you could see in some more advanced applications.
Great.
So I will leave it just like this using the this keyword.
All right.
And finally we create our method.
So public t result it returns a t result.
Call it execute.
And then simply take the task and call our perform method, which returns a T result.
We just have to make sure that we are returning it here.
So return task perform.
Great.
So that's the generic logic that we have, right?
We set up a constraint.
We have a task.
And the result we set up the task.
So if we create an instance of our processor we can submit the task.
And then we can simply take our processor and call the execute method which then runs the task perform
method and returns the actual result whatever that task returns.
So that's highly dynamic and generic.
Finally, we can move to our program and start using what we have created, our generic task management.
So let's create a new task.
VAR email task equals to new email task just like that.
And then we can set up a message here.
Hello.
This is a test email.
Add a comma.
And then for the recipient we're going to say example at example.com.
Now let's set up our second task.
VAR report task equals to new report task.
And inside here we simply set our report name equals to annual report for example.
And now we can set up our task processes.
So we need two one for the email task one for the report task.
So create a var email processor.
Equals to new then call task processor.
Create a new task processor.
We set the task to be email task.
Then we add a comma.
And we expect as result a string right.
And then finally since this is the constructor, we can submit the task here to initialize everything.
So email task in that specific case.
That's the first processor.
Let's repeat it for our second one var report processor equals to new task processor of type.
This time we have our report task.
And again we expect a string here.
And then we submit our report task object that we have created here.
Brilliant.
Now finally we can simply take our email processor dot and hit execute finally.
Then we do the same for our report processor dot execute.
Those calls will return strings so we can simply wrap them inside of a c w.
Console.WriteLine.
So take the email processor, execute, put it into the right line just like that and we do the same
here c w Console.WriteLine.
Take the report processor execute and also put that into the Console.WriteLine.
I just wanted to make sure that you see we call the execute on the email processor.
And then in the second step just wrap that into the console right line.
Great.
Now save it like that.
Start your application.
And you should now be able to see that a task of type email got processed and also a task of type report.
So let's just give it a second to load.
Now you can see email is sent to example at example.com with message.
Hello.
This is a test email and report annual report generated successfully.
Brilliant.
So this is a more real world example of how you would create a generic system that is very easy to extend
because in future if you would add or like to add a new type of task, whatever, this would be like
an upload task or no matter what else, right?
You could simply create a new class, use your eye task interface, create a return value, then create
an instance of your task processor and simply run that one highly dynamic.
Alrighty, and this is how you can create a task management system in C sharp with a generic architecture.