 So there are not many people, so I won't mind if you come closer because it's going to be a lot of code and text. So I won't mind if you leave, I mean, so feel free, come close and leave whenever you want to. So we'll get started. My name is, so okay, I'll just write, so this is the web framework talk, and my name is Bushtaq. I work for ThoughtWorks. Okay. So this is about using play as a modern web framework and Scala as the programming language for building enterprise web application. This was supposed to be a bigger talk when I submitted, but then I got a 20-minute slot. So I wanted to cover two big topics. One was because we are using a different set of technology, the way we are doing testing, especially dependency injection-related part, is unique, and I wanted to highlight that. And the second part is concurrency, that if you play, it's like Node.js, it's end-to-end non-blocking, but it uses futures and promises to do the concurrency. I don't think I will have much time to talk about concurrency. So let's start with the introduction to the framework and dependency injection and testing. And in the end, maybe we can have a brief demo about the concurrency. So how many of you have heard of play, and Scala? How many of you hate Scala? Okay, very few. So Scala gets a lot of bashing, and I think there are reasons for that, but we like it. There are a lot of cool, powerful things, hopefully I'll be able to show you that. Let's start with the structure of our framework. If you are then Ruby programming, Rails programming, Rails, when you create the base template of the project, it comes with a default, and something similar happens for the play projects as well. So you have app where your app code will reside, and then test will have your test, and then your conf will have your application configurations, routes, and stuff like that. The build is defined in the project, okay, that's something. So when you create a template of a play project, this structure will come out of the box, which is a good beginning, right? So after that, let's look at end-to-end flow. So for example, I am building a grad application for employees, right? Let's take a very simple example that I should be able to, okay, let me run this application. So I should be able to have a form where I create a new employee. I say EMP1, where the email is EMP1 at gmail.com. I don't change the home office and role, but address, I give something, random, code 3, and then I create. It will create and then show me some details of that employee, okay, and then I will also get a link to create another one. So to begin with, let's see how it works, just the first flow of getting the form, right? So it all starts with the routes file. People who are familiar with Rails would understand what that means. So I define a route that if there is a get request on an endpoint new, then I need to do some action and that action is defined in a controller. So that is a new M controller. What it will do, it will take a form, employee form and call something which is views, right? It looks like a method. So it's a method which takes employee form of type, form of employee, but it's actually a template and what exactly happens is I define my templates like this, which are compiled into the class files, same class files like Java class files, and they become functions which I can call. So very briefly, what is this template? This template takes a form of employee and then will create the UI using helpers. There are a lot of helpers, other frameworks also have that. I will have a helper for all the fields that I want and then for the employee and for its address. I have some defaults and stuff like that. After I submit the form, it needs to go and post this form to a particular endpoint and that endpoint is this, create, create employee. So what happens on that endpoint is a post request on this one and which is now handled with the action in createEmploy. Now you notice that I'm not using a string here to specify that the form is submitted to create employee slash create employee endpoint, rather what I'm using is basically a first class object and a call on that. Curiously, it starts with route. So just to, I think this is very, very unique. So you'll have to pay some attention. So my action, we haven't seen createEmploy yet, but my action is defined in controllers employee controller and which maps to this route. So this is a forward route. This is the endpoint, this is the action. This is what they call reverse route, which is this will point to the string representation of that route, which means that if I change my string representation by making this employee 1, 2, 3, my code will not change. I don't have to change this because this is a representation which compiler has generated and then I'm using it and it will automatically map to the correct one as I change my strings. So which is cool, it will catch a lot of silly things, silly mistakes that you do. So what happens is that I have to refer it to the endpoints in my application itself for redirection, for form submission, for slings. What is the most brute force way? Just a string representation of the endpoint that redirects to this string and then it will rails does something very, very meaningful, it will give a name to a route. So even though you change your string representation and stuff like that, as long as your name doesn't change, you can use that to refer to your route, so it's some guarantee. What can happen is that your name can become slightly away from the meaning of the route. If you change your route completely, then name you will feel like changing the name and again you are back to the same problem. Here what you are using is not the name but the compiler generated reverse route. Which means that when we try play with this application, you may notice that once we change the value of this, okay, let's do a quick thing. So let's go to the employee, let's go to inspect. You see that this endpoint, can you see that? So this is a post method for create employee, right, appropriately created. Now let's do something like changing this route, say 1, 2, 3. But we don't have to do anything and change the form, instead we just have to reload. So the process of reloading will compile the routes, create the appropriate reverse routes and then we'll use it for do the mapping. And now you see what is the reverse route which is created, which is the change string, right? So this is a small thing but which is very, very valuable, okay? So now let's go and see what have we done in the create employee. So in create employee, again we have this form, we bind it from the request. And then that crazy fold name, fold is functional programming idea where if you have to handle with the error cases and the success cases, you can specify it in one go and you do some logic there. Let's ignore that for the moment, right? The main logic is that, okay, if there is an error, you send a bad request. But you send the form back with the errors populated so that user can fix the errors. If there is a success, you make a call to the service and save that information, right, and do the redirect after that. So that is about end to end flow. Let me just quickly see what is next, okay? Let's see another example of this. So now we saw that if I go back, this is employee one and this is the HTML representation of employee one. Now if I want a JSON representation, then again very similar to this is EMP one, right? So if you get a JSON back, and how does it happen? Similar to Rails, Rails has a same end point, same action, can serve both JSON as well as HTML if you handle it that way. So our action, which is the find action here is doing that. It's saying that if there is a HTTP header accepts HTTP headers HTML, then render HTML. If it is JSON, then give me the JSON representation of that particular object. And what we do here is that I set my accept header to application JSON. But if I change, if I do not give this by making an error, then what will happen? I will get HTML back, right? The same action, same end point, but it will give me the HTML back. So this is again a very small, but nice thing. Similarly, you can post. So I can go to an employee and post a JSON. Instead of creating via form, I can post and it will do all the validations. So I say that, okay, post this, it is done. But instead of Pune, I guess my home office is an enumeration. So if I say Pune12, it will tell me invalid JSON, but it will also tell me what is the validation error. It will say that there is a validation error home office in valid value. And you can do something meaningful around that validation, right? And this happens pretty much automatically as we'll see. So let's see what happens in this find. So once I get this find, again going back to the routes, find is mapped to this end point, employee colon name. Colon name will parse the URL and will bind the name part with a variable. That variable will be available to me to be parsed to the controller action, which is fine. Now, this is again type safe and all that. So if you make a mistake here of type, it will catch it. So for example, instead of string, it was a int. This idea may not show it, but it will, if I try to just reload this, it will catch it and it should show me error. Now this is again a cool feature. So I don't use it very often because we drive application via tests most of the time. But when you are exploring, when you are learning, this is very helpful that all the errors, not only compile time errors. But even the runtime errors are very well minimized and presented to you in the browser. So currently I'm seeing a compile time error that int and required string. So this is really, really stupid. So let's fix that, but you can also do something interesting. So for example, here, if you do say 1 by 0, I haven't tried this. But if I just refresh this, it should, that is a runtime error. And it should show me that error also in the context, right? It should say that 1 by 0 and there is a runtime error, which is divided by 0, okay? So this is some, I'm talking about the framework right now. Some cool things here. I told you that that is all async and non-blocking in the end will talk what that means. So ignore that for a moment. What I'm doing is calling a employee service. How do I get employee service here? So I say my controller extends a controller because it's a controller and it mixes in a dependency called employee service component. Now this is in difference to what you will do in a typical dependency injection framework, right, if you're using spring or any DI mechanism. What you will do is, you will do a constructor-based dependency. Employee controller will have all the repositories as, how much time do I have? Just five minutes, okay? So maybe I should just focus on that part and then. So controller will have all the dependencies injected and then the framework will initialize it. Here it's somewhat different. And what happens is, if you notice, let's go to employee service. I'm just going to collapse this service, right? Employee service itself depends on something else, which is employee repo, I'm going to collapse this. And what you see here is that everything is a component and it has two parts. One is a class, which is like a class, but it is a inner class, interestingly. And this is a singleton, but it could also be a factory. This is a mechanism of creating an instance of the class that you control. Both of them are wrapped together in a component, and what you do is directly depend on the component. So I, employee service depends on employee repo, and by the virtue of that, I directly get access to the employee repo, right? Why? Because this is coming from here, right? So if you notice, in this mechanism, you will not use the class constructors to pass the dependencies, the way you will do otherwise. Because class constructors, actually you can argue that are not supposed to pass the dependencies like repositories and services and all that. They are for constructors, like names and some values and the way you will pass arguments to methods, right? And I think this is really short time. I want to drive this point really, really well. So let me try to do something that, if you have a class adder, for example, right, which has a parameter a integer, right? And you have a method add, which takes b, right? Which is an integer, and you have a plus b, right? This is like a class in a method, method which takes one argument and does some addition, right? This is very familiar to us. How do I use this? I say that x is equal to new adder of 10, and then I say x dot add to 30. And this will give me 40. If I do it on the next line, this say to 300, this will give me 310, right? So now what happens is that the context that this is the adder of 10 is kind of encapsulated within the class, right? And that is the simple principles of object orientation that we use. And while calling a method, I just give the part which is remaining, right? Now, this is very nice because you can keep that context hidden, right? And just take whatever is difference. But this gets lost when you start to do the constructor based dependency injection. What happens is, each constructor, right? If it's taking a repo as a dependency, as an alternative, what I can think is that repo is my context that I already know. So for example, instead of this a in here, I can have def a int is equal to 10, right? Or I can keep it abstract or I can give a value. If I do it like this, then I don't have to do it like this. So in a way, now what I have done is kind of fix that dependency A without having my user to pass or the framework to initialize. I have fixed that dependency A within the adder. And you know what, anyone which uses instance of adder will get that dependency out of the box. So the main question is that, how do I swap that dependency? Okay, quickly, let's do that. So I'm going to go to the test of this, so let's go in here. And just let's look at one of the tests. Before I look at the test, let's look at how I'm creating an assembly. So you have this components and, okay, just two minutes, okay, cool. So you create an assembly by extending that component, so that you create an instance object assembly. But at the time of creation, you swap components. So you say that instead of using a real repository, you use a mock repository. And instead of using a real play app, mock the play app, right? So by doing that, you have swapped the entire wiring of this application in this particular instance. And then, now if you test, you will be able to test in a traditional way. So you will have your test data employee, and you will have, using Mockito, you will set the expectation that on the employee repo, if there's a save call with this value, I get a future. And this is my service under test. So I'm now testing my save call by mocking the employee repo. And how is it possible, even without having constructors and dependencies, by swapping it using the with construct. I think we are just towards the end. So the message here is that if you use Kala with play, there are a lot of different opportunities for doing, for structuring your code. It is very powerful. I think I can show you, maybe offline if you're interested, different types of tests which are possible. And then concurrency itself is a big story, which is not specific to Kala, but play, play offers you a lot of easy constructs to build your web services. I'll just stop the question. Quick, quick one. Okay. It's not single, so what do they call it? Mixing composition, right? It's a trade-based mixing composition, which looks similar to multiple inheritance, but it is not. Why? Because all the mixings get linearized at the time of instance creation, not at runtime. This is what I mean by instance creation time. When I define this line, at this line, the whole mixing chain is linearized vertically so that each one has a determined parent, right? But at the time, before the instance is created, your super is open. Super is not like C++ multiple inheritance, C, the super is not bound. It's a bit detailed and a bit complicated. We can take it offline. It's like all of them. That is play-for-all-specification. It's nothing to do with the language. Play is the compiler output and shows it in the browser. Assuming that most of the web developers like to work that way, I don't think that is true. In a big project, I would really want to refresh and see, right? I will write my test because it's so big code-based. I want to just exercise my code. But it's a good, nice way to get started. And sometimes, that depends on your application. Can I take one more or two? Yeah? We will discuss it offline. But I think it's a concept of you made it as a property or even not a property. You made it as a field of your class. And you said that you can inject it, swap it. So that is because you need to see how this is happening. So how do I create my mock? I create my mock by extending. And then I override my web instance creation, right? I just say that this is a mock operative in which the mechanism of instance creation is a mock. When I do the mix-in, it will get wired. So now when the service call save happens, it will call on the mock. So you better set the expectation, otherwise there will be an error. So overriding is the mechanism of doing that. Now, overriding is not the mechanism that you will use for your app code. It's not a good practice to do a implementation in inheritance in a big way. But for tests, this is good because this is the only line. So you can have the whole single file where all the mock library is set up. But this is only for testing that you keep overriding just for the purpose of swaping. Cool? I think we can take the questions. Thank you.