ASP.NET options/command generator framework? - asp.net

I want to put context-sensitive, dynamic command options on my asp.net pages.
I tried coding my own command structure but it's not very good, and I'm sure there must be a framework for doing this somewhere I can re-use?
Example:
I have a detailsview for some database object, I want to code in the object class what commands are available, based on the state of the object. I then want a UI object I can place on the webform that will pass commands back to the object when user clicks them, or jump to a different link (e.g. when additional parameters are available).
e.g. form might look like this
Product Details
Name: XXXX product
Price: $1.00
Qty: 1
Commands:
> Edit
> New Stock
> Mark as obsolete
So the commands at the bottom would have very little UI code and pass actions back to the object. For example the New Stock command would jump to a new page to ask for a quantity.

I don't know of the framework, but you could create something yourself. Let's say you are using MVP pattern, and assuming that this is a CRUD application, you could tell each view what type of object it is related to, then annotate you object with operations that are available. Then Presenter could call Service to perform the operation. You could name your methods using some convention so that you can wire it up in a Service. It is a lot of work, and unless you have 100s of views it is not worth while. I am building app that is about that size, and I am in process of creating GenericMVP framework, that would make wiring a breeze.

Related

SAPUI5 bind element without backend call

I have a SAPUI5 app with two views. One overview page with a smart table and a details page that also contains a smart table. When navigating from overview to detail page I am binding the detail view to a context with:
oView.bindElement("Product(1)");
In the smart table of the detail view product details should be displayed which are available over a Navigation Property.
"Product(1)/ProductDetails"
The problem is that now two sequential backend calls will be triggered. One from the bindElement and the other one is triggert from the smart table.
Is there any possibility to not trigger a backend call when binding the view or can that call be deferred and executed in parallel with the one from the smart table?
I think it's good to separate the backend calls (not put them in the same batch), so that you can have the detail view ready, without waiting for the Smart Table to retrieve the results of the smart table of the detailed view.
However, if you do want to batch all the backend calls, just have a look on class sap.ui.model.odata.v2.ODataListBinding. It's used by the smart table of the detail page. You can use the parameter batchGroupId to specify that you want the request to be bundled in the specified batch. Use the same batch that the one used in bindElement.
Changing the batch of the Smart Table should be possible in callback routine of onBeforeRebindTable, if you do:
oEvent.getParameter("bindingParams").parameters.batchGroupId = "MyBatch"

Dynamic data drop down using Orbeon builder

I have used this to chain dynamic data drop downs in an Orbeon application using the following services:
1. /xforms-sandbox/service/zip-states
2. /xforms-sandbox/service/zip-cities?state-abbreviation={../state}
3. /xforms-sandbox/service/zip-zips?state-abbreviation={../state}&city={../city}
I have few questions:
I also want to create the same, so can you please point me the code where this services are present. How I should write the service in this case?
{../state} - How it retrieve the state value when it changed?
What is the use of state-abbreviation?
This specific test service is implemented in XPL and XSL, in zip-states.xpl. But it really just is a service that gets called with an HTTP GET by Orbeon Forms, and returns XML. BTW, you can easily test it from your browser, and it could be implemented with any technology.
Whenever the value of the control named state changes, {../state} will return a different value, so the URL for the service will change, so the Dynamic dropdown will load again that URL to retrieve potentially new data.
In this particular example, we want the "abbreviation" to be stored in the data (e.g. "CA"), and the full name (e.g. "California") to be shown in the UI. Again, in this particular examples, values come from states.xml.

Pattern for updating Objects/Documents with Spring-Data MongoDB and Spring MVC

I'm trying to come up with a reusable pattern for updating MongoDB Documents when using Spring Data in conjunction with Spring MVC.
The use case can generally be summarized by:
A document is created in Mongo using repository.save()
Parts of that document are then presented in a Spring MVC editable form.
A user submits updated parts of that document which are then saved.
If I use the repository.save() method in step 3, I will lose any data in the document that was not bound to the form. Making the form responsible for the entire document is fragile so this is where it seems the findAndModify() method of the MongoTemplate comes in handy.
To use the findAndModify() method, I've created Form objects that support a toMap() method which takes the Form object's properties as a Map and removes some of the fields (e.g. class and id). This gets me a Map that contains only the fields that I care about from the Form object. Passing the object ID and this map to an update() method on my customized repository, I build Query and Update objects that I can pass to the findAndModify() method.
Using this approach, I'm able to add fields to my objects easily and only worry about instances when there are fields I don't want to update from a form posting. Document fields not manipulated by the Form should be retained. It still seems slightly convoluted to be using both the Repository and MongoTemplate so I'm wondering if there are better examples for how to handle this. It seems like this should be a consistent pattern when working with Mongo and Spring MVC (at the least).
I've created a sample project showing how I achieve this model on GitHub. The Spock Tests show how "updating" a Document using save() will blow away fields as expected and my update() method.
https://github.com/watchwithmike/diner-data
What are other people doing when dealing with partial updates to Documents using Spring MVC and Spring Data?
If you are taking whatever the user supplies and just shoving that in the database you are running the risk of doing something dangerous like updating or creating data that they shouldn't be able to. Instead, you should first query Mongo to get the most recent version of the document, change any fields (it looks like you are using Groovy so you could loop through all the properties and set them on the new document), and then save the new, complete document.
If you are making small, consistent updates (like increasing the number of votes, or something like that), you could create a custom MongoDB query using the MongoTemplate to do an update to a few fields. Check out the spring-data-mongodb docs for more. You can also add custom methods to the MongoRepository that use the MongoTemplate.

Flex-Cairngorm/Hibernate - Is EAGER fetching strategy pointless?

I will try to be as concise as possible. I'm using Flex/Hibernate technologies for my app. I also use Cairngorm micro-architecture for Flex. Because i'm beginner, i have probably misunderstand something about Caringorm's ModelLocator purpose. I have following problem...
Suppose that we have next data model:
USER ----------------> TOPIC -------------> COMMENT
1 M 1 M
User can start many topics, topics can have many comments etc. It is pretty simple model, just for example. In hibernate, i use EAGER fetching strategy for unidirectional USER->TOPIC and TOPIC->COMMENT relations(here is no question about best practices etc, this is just example of problem).
My ModelLocator looks like this:
...
public class ModelLocator ....
{
//private instance, private constructor, getInstance() etc...
...
//app state
public var users:ArrayCollection;
public var selectedUser:UserVO;
public var selectedTopic:TopicVO;
}
Because i use eager fetching, i can 'walk' through all object graph on my Flex client without hitting the database. This is ok as long as i don't need to insert, update, or delete some of the domain instances. But when that comes, problems with synchronization arise.
For example, if i want to show details about some user from some UserListView, when user(actor) select that user in list, i will take selected index in UserList, get element from users ArrayCollection in ModelLocator at selected index and show details about selected user.
When i want to insert new User, ok, I will save that user in database and in IResponder result method i will add that user in ModelLocator.users ArrayCollection.
But, when i want to add new topic for some user, if i still want to use convenience of EAGER fetching, i need to reload user list again... And to add topic to selected user... And if user is in some other location(indirectly), i need to insert topic there also.
Update is even worst. In that case i need to write even some logic...
My question: is this good way of using ModelLocator in Cairngorm? It seems to me that, because of mentioned, EAGER fetching is somehow pointless. In case of using EAGER fetching, synchronization on Flex client can become big problem. Should I always hit database in order to manipulate with my domain model?
EDIT:
It seems that i didn't make myself clear enough. Excuse me for that.
Ok, i use Spring in technology stack also and DTO(DVO) pattern with flex/spring (de)serializer, but i just wanted to stay out of that because i'm trying to point out how do you stay synchronized with database state in your flex app. I don't even mention multi-user scenario and poling/pushing topic which is, maybe, my solution because i use standard request-response mechanism. I didn't provide some concrete code, because this seems conceptual problem for me, and i use standard Cairngorm terms in order to explain pseudo-names which i use for class names, var names etc.
I'll try to 'simplify' again: you have flex client for administration of above mentioned domain(CRUD for each of domain classes), you have ListOfUsersView(shows list of users with basic infos about them), UserDetailsView(shows user details and list of user topics with delete option for each of topic), InsertNewUserTopicView(form to insert new topic) etc.
Each of view which displays some infos is synchronized with ModelLocator state variables, for example:
ListOfUsersView ------binded to------> users:ArrayCollection in ModelLocator
UserDetailsView ------binded to------> selectedUser:UserVO in ModelLocator
etc.
View state transition look like this:
ListOfUsersView----detailsClick---->UserDetailsView---insertTopic--->InsertTopicView
So when i click on "Details" button in ListOfUsersView, in my logic, i get index of selected row in ListOfUsers, after that i take UserVO object from users:ArrayCollection in ModelLocator at mentioned index, after that i set that UserVO object as selectedUser:UserVO in ModelLocator and after that i change view state to UserDetailsView(it shows user details and selectedUser.topics) which is synchronized with selectedUser:UserVO in ModelLocator.
Now, i click "Insert new topic" button on UserDetailsView which results in InsertTopicView form. I enter some data, click "Save topic"(after successful save, UserDetailsView is shown again) and problem arise.
Because of my EAGER-ly fetched objects, i didn't hit the database in mentioned transitions and because of that there are two places for which i need to be concerned when insert new topic for selected user: one is instance of selectedUser object in users:ArrayCollection (because my logic select users from that collection and shows them in UserDetailsView), and second is selectedUser:UserVO(in order to sync UserDetailsView which comes after successfull save operation).
So, again my question arises... Should i hit database in every transition, should i reload users:ArrayCollection and selectedUser:UserVO after save in order to synchronize database state with flex client, should i take saved topic and on client side, without hitting the database, programmatically pass all places which i need to update or...?
It seems to me that EAGER-ly fetched object with their associations is not good idea. Am i wrong?
Or, to 'simplify' :) again, what should you do in the mentioned scenario? So, you need to handle click on "Save topic" button, and now what...?
Again, i really try to explain this as plastic as possible because i'm confused with this. So, please forgive me for my long post.
From my point of view the point isn't in fetching mode itself but in client/server interaction. From my previous experience with it I've finally found some disadvantages of using pure domain objects (especially with eager fetching) for client/server interaction:
You have to pass all the child collections maybe without necessity to use them on a client side. In your case it is very likely you'll display topics and comments not for all users you get from server. The most like situation you need to display user list then display topics for one of the selected users and then comments for one of the selected topics. But in current implementation you receive all the topics and comments even if they are not needed to display. It is very possible you'll receive all your DB in a single query.
Another problem is it can be very insecure to get all the user data (or some other data) with all fields (emails, addresses, passwords, credit card numbers etc).
I think there can be other reasons not to use pure domain objects especially with eager fetching.
I suggest you to introduce some Mapper (or Assembler) layer to convert your domain objects to Data Transfer Objects aka DTO. So every query to your service layer will receive data from your DAO or Active Record and then convert it to corresponding DTO using corresponding Mapper. So you can get user list without private data and query some additional user details with a separate query.
On a client side you can use these DTOs directly or convert them into client domain objects. You can do it in your Cairngorm responders.
This way you can avoid a lot of your client side problems which you described.
For a Mapper layer you can use Dozer library or create your own lightweight mappers.
Hope this helps!
EDIT
What about your details I'd prefer to get user list with necessary displayable fields like first name and last name (to display in list). Say a list of SimpleUserRepresentationDTO.
Then if user requests user details for editing you request UserDetailsDTO for that user and fill tour selectedUser fields in model with it. The same is for topics.
The only problem is displaying list of users after user details editing. You can:
Request the whole list again. The advantage is you can display changes performed by other users. But if the list is too long it can be very ineffective to query all the users each time even if they are SimpleUserRepresentationDTO with minimal data.
When you get success from server on user details saving you can find corresponding user in model's user list and replace changed details there.
Tell you the truth, there's no good way of using Cairngorm. It's a crap framework.
I'm not too sure exactly what you mean by eager fetching (or what exactly is your problem), but whatever it is, it's still a request/response kind of deal and this shouldn't be a problem per say unless you're not doing something right; in which case I can't see your code.
As for frameworks, I recommend you look at RobotLegs or Parsley.
Look at the "dpHibernate" project. It implements "lazy loading" on the Flex client.

Applying Unity in dynamic menu

I was going through Unity 2.0 to check if it has an effective use in our new application. My application is a Windows Forms application and uses a traditional bar menu (at the top), currently.
My UIs (Windows Forms) more or less support Dependency Injection pattern since they all work with a class (Presentation Model Class) supplied to them via the constructor. The form then binds to the properties of the supplied P Model class and calls methods on the P Model class to perform its duties. Pretty simple and straightforward.
How P Model reacts to the UI actions and responds to them by co-ordinating with the Domain Class (Business Logic/Model) is irrelevant here and thus not mentioned.
The object creation sequence to show up one UI from menu then goes like this -
Create Business Model instance
Create Presentation Model instance with Business Model instance passed to P Model constructor.
Create UI instance with Presentation Model instance passed to UI constructor.
My present solution:
To show an UI in the method above from my menu I would have to refer all assemblies (Business, PModel, UI) from my Menu class. Considering I have split the modules into a number of physical assemblies, that would be a dificult task to add references to about 60 different assemblies. Also the approach is not very scalable since I would certainly need to release more modules and with this approach I would have to change the source code every time I release a new module.
So primarily to avoid the reference of so many assemblies from my Menu class (assembly) I did as below -
Stored all the dependency described above in a database table (SQL Server), e.g.
ModuleShortCode | BModelAssembly | BModelFullTypeName | PModelAssembly | PModelFullTypeName | UIAssembly | UIFullTypeName
Now used a static class named "Launcher" with a method "Launch" as below -
Launcher.Launch("Discount");
Launcher.Launch("Customers");
The Launcher internally uses data from the dependency table and uses Activator.CreateInstance() to create each of the objects and uses the instance as constructor parameter to the next object being created, till the UI is built. The UI is then shown as a modal dialog. The code inside Launcher is somewhat like -
Form frm = ResolveForm("Discount");
frm.ShowDialog();`
The ResolveForm does the trick of building the chain of objects.
Can Unity help me here?
Now when I did that I did not have enough information on Unity and now that I have studied Unity I think I have been doing more or less the same thing. So I tried to replace my code with Unity.
However, as soon as I started I hit a block. If I try to resolve UI forms in my Menu as
Form customers = myUnityContainer.Resolve<Customers>();
or
Form customers = myUnityContainer.Resolve(typeof(Customers));
Then either way, I need to refer to my UI assembly from my Menu assembly since the target Type "Customers" need to be known for Unity to resolve it. So I am back to same place since I would have to refer all UI assemblies from the Menu assembly. I understand that with Unity I would have to refer fewer assemblies (only UI assemblies) but those references are needed which defeats my objectives below -
Create the chain of objects dynamically without any assembly reference from Menu assembly. This is to avoid Menu source code changing every time I release a new module. My Menu also is built dynamically from a table.
Be able to supply new modules just by supplying the new assemblies and inserting the new Dependency row in the table by a database patch.
At this stage, I have a feeling that I have to do it the way I was doing, i.e. Activator.CreateInstance() to fulfil all my objectives. I need to verify whether the community thinks the same way as me or have a better suggestion to solve the problem.
The post is really long and I sincerely thank you if you come til this point. Waiting for your valuable suggestions.
Rajarshi
As I can see from this code
Form customers = myUnityContainer.Resolve<Customers>();
all your code need to know about the customer - is that it's a Form class. So if you use xml configuration for unity you can do the following:
<type type="Form" mapTo="Customer" name="Customer">
</type>
And then you'll be able to resolve it like this:
Form customers = myUnityContainer.Resolve<Form>("Customer");
and there is no need to refference your UI assembly. Offcourse it should be presented in the bin directory or GAC. In this case if you'll develop new Assembly - all you need is to change config and put in in bin or gac.
If you want to make unity configuration from db then you'll have to add referrence to your ui, becouse you'll have to call Register("Customer").

Resources