Framework Extensibility based on GWT/GIN/GUICE - gwt

I hope my question is not to generic.
I created a framework in gwt/gxt which helps developer to create very quick a web application with a set of pre definied widgets.
Now over the days they want to have new functionality which are very related to a specific customer and I found it very difficult to extend my framework. I am using the MVP pattern on client side with GIN and GUICE on server side. The client components which needs to be extended also have related services on the server to load their data.
I already thought about creating new guice modules and over"bind"(dont know if this is the right word) the existing ones but than the developer has to rewrite the complete module or its service.
Does somebody already has some experiences with designing a framework with gwt on client and server side? If yes how can I provide my framework with easy possiblities to extend with customer specific stuff. Maybe there are some nice patterns out there which I could use.
Thanks a lot

If I understand right you question, you can do something similar to this idea I got now.
In your framework, you could provide a module with all your modules, like this:
public class MyAPI {
public static List<Module> MODULES = Arrays.asList(new FooModule(), new BarModule(), new OtherModule());
public static Module getMyAPIModule() {
return new AbstractModule() {
protected void configure() {
for(Module m : MODULES) install(m);
}
}
}
}
Then, in your app, when you create the injector, or in your servlet module, you can do a install(MyAPI.getMyAPIModule()); and it will work

Related

How to access Entity Framework DbContext entities in Server-Side Blazor Components

I'm new to .NET Core and Blazor, with mostly WebForms and MVC experience.
All the Blazor documentation and tutorials I've found use a separate API project and access data through HttpClient and Json serialization/deserialization. I see why this would be necessary for client-side Blazor using WebAssembly, but for Server-Side Blazor using SignalR what's the best way to access the database directly from the components' .razor files using an Entity Framework DbContext?
For example, in an MVC controller you can just do something like:
private ApplicationDbContext context = new ApplicationDbContext();
and then query the data by doing something like:
var things = context.Things.Where(t => t.ThingAttributes == something);
Is there an approach that is this clean and efficient when working with components in server-side Blazor?
Sorry for the broad nature of this question, feel free to point me to blogs, docs, or tutorials I should have already read. Thanks!
What you call a controller should be turned into a service class, that retrieves data from the database, and pass it to the calling methods. You should add this service to the DI container in the Startup class. To use this service in your components you should inject it like this:
#inject DataService myDataService
I think that the Blazor templates come with sample how to define such a service and use it in your components.
Here's a link to a sample by the Blazor team how to create a service and how to use it in your components. The service doesn't use Entity Framework, but this is something really minor I'm sure you'll cope with.

GXT with RestyGWT

I've searched as much as I could but did not find any answers/examples for my question.
I'm completely new to Web UI development but have a decade and a half of experience in Java and other languages. I seem to be completely lost in the sea of available options for the client side, but for the server side I have a Rest server (Play) running already. I can't and don't want to use a complete package for both client and server b/c I want to pass JSON back and forth between the server and the client. This way I can use multiple different clients: web, Excel, Swing, etc. I want to keep it flexible like this.
So far I pretty much decided to use GXT for the client side, and found RestyGWT to be sitting in the middle. This is what causes my problems. So far, I have not been able to find a single example of a GXT + RestyGWT combination. Just a single example (a Grid for example) would be extremely helpful, since I have no experience with J2EE, beans, or any of that.
Any help or examples with GXT + RestyGWT would be greatly appreciated!
What have you tried? RestyGWT is serialization and transport, so ideally you set up a loader that describes what you need based on your widgets (grid? paging toolbar? filters?), and then pass it a DataProxy implementation that knows how to take config objects, and asynchronously send back loaded data objects. Each grid example that loads from the server uses a loader, but a different proxy (and optionally reader) based on whether it us using RPC, RequestFactory or XML/JSON over HTTP. There is also a JSONP example, and while it isn't using the Grid, it is still loading items to a ListStore, so could easily be attached to a grid.
DataProxy is a simple interface - it is given a config object and a callback to invoke when the load is finished or to notify if an error occurred. In your implementation of this interface, call your service with the necessary details of the config, and then invoke the callback when results are ready.
If you want an example of how RestyGWT works you can have a look to
one of my blog article. It is a pure GWT example but should work with GXT as well. GXT is mostly about graphical components for GWT.
In 2 words you need to
1) Define your restServices interfaces
public interface HelloClient extends RestService {
#GET
public void getHellos( MethodCallback<List<Hello>> callback);
}
2) Create your client
HelloClient client = GWT.create(HelloClient.class);
3) Use it
client.getHellos(new MethodCallback<List<Hello>>() {
public void onSuccess(Method method, List<Hello> response) {
//...
}
public void onFailure(Method method, Throwable exception) {
//...
}
});

Using MVVM Light references in model

On a project I'm working on I'm trying to develop a generic mechanism for sending back messages from anywhere in the code base to the front end for possible logging, displaying to the user etc.
Messages could be sent because of an error/exception, to report progress etc. Initially I thought of using something like log4net to report the message data and have a custom appender at the application level to consume these messages and then display them/log them as appropriate. However I'm not sure that this is the best approach because of a couple of issues
1) The project is split across a number of separate assemblies and I've seen that there are issues with configuration of log4net across multiple dlls.
2) This messaging scheme is more than just logging, so using a logging framework may be too restrictive.
At the moment I'm using C# custom message events to send data from assemblies and registering a handler at the application level to capture them.
However I've realized that the the Messenger class in MVVMLight is exactly what I'm looking for, a way to send a generic data packet (class) across assemblies. But because I could be sending messages from model code I'm wondering if this is adding unnecessarily dependencies to MVVMLight in to the model code.
My impression is that the Model code should be as dependency free as possible so that it could be dropped into any application framework without modifications.
What do you guys think?
If that's what you're trying to do then you should use the Messenger class.
How likely is it (really) that the model code is going to be used by another application? If it's something generic (user preferences, security, etc) then you might want to use it elsewhere but if you're providing bespoke business data then YAGNI.
From a good practices perspective I'd suggest doing something like this:
public interface IMessenger<T>
{
public void Send(T message);
}
public class MessengerProxy:IMessenger<ErrorDto>
{
public void Send(ErrorDto error)
{
Messenger.Default.Send(error);
}
}
public class AnotherModel
{
public AnotherModel(IMessenger<ErrorDto> errorRegister)
{
_errorRegister=errorRegister;
}
private RegisterError(Exception ex)
{
var dto=new ErrorDto(ex,"My Title");
_errorRegister.Send(dto);
}
}
Register the MessengerProxy in MVVM Light and have it injected into the constructor of your Model classes. In my model class, above, I inject the interface into the constructor. This gives me access to the Messenger but allows me to change the underlying implementation if I need to later on.

The "proper" way to implement a REST API service using JBoss AS

I started a new job in a company that loves using the word, "enterprise." As such, everything they've written is in Java. I come from a very web-development heavy background, mainly working with LAMP stacks.
Now, up until three days ago I knew nothing about Java other than people used it and that it is a programming language. Googling up on it, the Java language itself seems simple enough. However, when people say, "Java" it seems they are referring to more than just the language, such as the various frameworks and application servers. It's a little over-the-top, and am having some trouble getting up to speed with "Java."
An upcoming project involves me creating an exposed REST API for one of the products. Seems easy enough. However, I have some questions about how to proceed....
I'm working with JBoss AS for the first time; not sure if there's an equivalent in PHP so I can understand what JBoss does exactly, but I suspect there's a "proper" way of doing things. Here's what I was thinking of doing:
1) Created a package with a single servlet, like so:
package com.awesome.myrestapi;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HiggiltyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HiggiltyServlet() {
super();
}
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
// #todo ideally, do something more RESTfully useful and less vindictive
out.println( "<html><body>HAHA! all ur api requests are belong to us</body></html>" );
out.close();
}
}
2) As you can see, I was thinking of just overriding the service method to serve my REST API requests.
3) Updated my web.xml file accordingly, so that the url-pattern would match "higgilty", thus making my URL endpoint something like....
http://localhost/awesomeproject/higgilty
Now, I feel like I might be doing something wrong. Am I going about this the right way, or am I totally off the mark?
Any help is greatly appreciated.
You should probably use a REST-oriented framework like Restlet, Jersey or RESTeasy. This will help you deal with various things like splitting URIs, having a system based on resources and representations and perhaps content-type negotiation (if you need it).
Restlets can run within a servlet container or as standalone applications. RESTeasy is a JBoss project, but I wouldn't dismiss the other frameworks just because of that, since JBoss AS should in principle be able to run applications written with the other frameworks (I've run Restlet applications within JBoss AS successfully,, although I don't use it regularly).
Agreed it's probably best to use a framework.
If you want to roll your own, you need to write something to parse the URL and route to the appropriate method. The URL pattern matching you get with web.xml is pretty limited.
Also you'd probably want to override the HttpServlet methods that correspond to HTTP methods - doGet, doPost, etc.

How to bring Spring Roo & GWT together

I am trying to develop a Spring Roo/GWT app with the newest integration of GWT in Roo.
Getting the scaffolding to work is very straightforward, but I don't really understand how the RPC works there.
Can someone of you provide a simple example how to do a simple service to connect client/server within Spring Roo and GWT.
Would be very helpful for a start, as I couldn't find any resource on that.
thx & regards,
Flo
Flo,
Not sure if you are up on google wave at all, but that does seem to be one place to keep apace of the current effort. Specifically this wave is available to the public:
RequestFactory Wave
It covers the details (well emerging details) about the RequestFactory API.
The basic idea is that your domain model objects are needed on the server side and the client side. Using hibernate can cause issues with the class files and people have wound up having two sets of model objects, and using custom GWT-RPC to make server requests and marshall/un-marshall between the client- and server-side model objects. Not an ideal solution. Even if you can use the same model objects, the overhead of the RPC is a drag.
Enter RequestFactory and we see that google engineers are probably getting paid what they are worth. Take a look at the sample code generated from the .roo - specifically ApplicationRequestFactory.java.
package com.springsource.extrack.gwt.request;
import com.google.gwt.requestfactory.shared.RequestFactory;
public interface ApplicationRequestFactory extends RequestFactory {
ReportRequest reportRequest();
ExpenseRequest expenseRequest();
EmployeeRequest employeeRequest();
}
This is an interface that provides request methods for each of the domain objects. There is no implementation of this class defined in the project. It is instantiated in the EntryPoint with a call to GWT.create(...):
final ApplicationRequestFactory requestFactory =
GWT.create(ApplicationRequestFactory.class);
requestFactory.init(eventBus);
Within the com.springsource.extrack.gwt.request package you will see an ApplicationEntityTypesProcessor.java which is cleverly using generics to package the references to the domain classes for use later in the presentation. The rest of that package though are events and handlers for each model object.
Specifically there are four auto-generated classes for each object:
EmployeeRecord.java - This is a DTO for the domain object.
EmployeeRecordChanged.java - This is a RecordChanged event to provide a hook method onEmployeeChanged.
EmployeeChangedHandler.java - This is an interface that will be implemented when specific behaviour for the onEmployeeChanged is needed.
EmployeeRequest.java - This is an interface used by the ApplicationRequestFactory to package up the various access methods for a given object.
Keep in mind there is a lot of code generated behind the scenes to support all this. And from M1 to M2 a lot has been cleaned out of what is visible in a GWT project. I'd expect there to be more changes, but not as drastic as M1 to M2 was.
So finally these events can be used as in the UI package to tie together the domain and the UI. ReportListActivity.java:
public void start(Display display) {
this.registration = eventBus.addHandler(ReportRecordChanged.TYPE, new ReportChangedHandler() {
public void onReportChanged(ReportRecordChanged event) {
update(event.getWriteOperation(), event.getRecord());
}
});
super.start(display);
}
Again I refer you to the wave for more info. Plus the expenses.roo demonstrates how to use Places and has a rather slick Activity framework as well. Happy GWTing.
Regards.
The functionality you are referring to is currently under heavy development (or so the guys at Google want us to believe ;)) so the API and internal workings are not final and will most likely still change before the final release of GWT 2.1 (this was stated a few times during the GWT sessions during Google IO 2010). However, you can browse the Bikeshed sample in the trunk to see a working (hopefully ;)) example. There's also the 2.1 branch that appears to contain the up-to-date (?) sample (and the cookbook that was promised on Google IO).
Personally, I'd wait with switching your code to the new RPC model till the guys working on GWT say it's safe to do so ;) (but it's definitely a good idea to get accustomed with the general idea now - it's not like they will change everything :D).