Multi-user support with nhibernate winforms application - winforms

I’m working on a multi-user winforms application (MDI). Our application uses NHibernate and one session per instance.
The problem is if an user makes modification, for example a user adds an item to a list, another user doesn’t see this modification immediately. How do I get around this, what is the recommended approach? Should I be using One-session-per-form or something else?

Every application needs to define it's own unit of work. For your use case, if you are wanting the database to update every time a user adds an item to a list, then that should be your unit of work.
So to answer your question for your unique use case, I would create a new NHibernate session, persist the users changes, and dispose of the session every time a user adds an item to the list.
FYI: You should not feel that there is anything wrong with creating a session per user action if that is indeed the behavior your application desires. Typically in a WinForms application, most people do use a Session-per-Form pattern but then again, that is because most WinForms applications fall into the model of having a form that the user needs to complete before proceeding to the next form. In those cases, you would only want to persist/save the user's changes when they submit the form. But if you application does not fall into that model, then your definition of a unit of work ( session life-cycle ) could be completely different.

Related

WPF MVVM architecture, what is the best practice communicate between model and viewmodel

We are developing a TODO list kind of application in WPF
1)when user logs in we load list of workitem for the user(only basic details)
2)when user opens particular workitem we load all its information from service
3)we use signalr to add new workitem to the user when he is working on the system (singalr will
send the workitemid, and based on it we will fetch basic detail and add it to the workitem list)
Whenever user edits the workitem, if he changes anything in the workitem information, we should move it to UnSaved List, and if he reverts it we need to move the workitem to Opened List
1)when user enters something in screen it directly alters model (letsay workitem model has user model and user model has firstname, this firstname is bound to the textbox), how to communicate this change
to the viewmodel, so that viewmodel will move this workitem to unsaved list.
To find out a workitem is dirty or not, we are keeping Unchanged workitem details inside each workitem, so whenever user changes something in screen we make comparision with this unchanged workitem and move it to unsave or opened list.
2)Is it right approach to have Unchanged workitem inside each workitem, are there any best approach or pattern available?
3)how to differentiate between user changing some value of workitem and system changing values of workitem
let say A and B user sees the same workitem in edit mode, User A edits some value in workitem and
saves it, now user B will receive this information through signalr notification, and we will make a call to service based on this workitemid and will populate the changes in B's screen. Now we should not move the workitem to unsaved list, because changes are done by system.
4)Moreover we will recieve lot of signalr notification related to multiple workitems, we don't
want to make multiple service call as and when signalr notification comes, but whenever user clicks on workitem we would like to see the changes done in that workitem from other user. What is the efficient way of doing this. (We don't want notification to contain all the information, we kept only the Id and type of the notification, so that signalr will be as lean as possible)
5)how to compensate for singalr notification failiures, (we could make some kind of timer which will periodically poll list of notifications based on the last arrived notificationid).
Is this a good approach or any pattern exist for this kind of problems.
I am not quite sure if I understand your questions.
So conceptionaly I think you are thinking about a real-time collaboration sort of software.
There are patterns and algorithm how this is solved in a distributed fashion. You should look for the papers of the google wave protocol (the foundation of the real time editing feature of the google docs tools) http://www.waveprotocol.org/whitepapers. Or research for Operational Transformation: https://operational-transformation.github.io/.
The good think is that there are already libraries out there which handle protocols like this and adapt or extend these concepts.
But finding the right library for handling realtime callobrative work over signalrs whould be a huge win. Don't know if there is also a library that builds on top of signalrs - might be worth a google or bing session.
But I think before you talk about technical details as c# or mvvm it would be a good start to understand the background of real time collaborative communication in distributed systems.
Like I said I hope I got your question correct and since you did not post any source code there is no chance to be more precise...
HTH and leads you into the right direction.

Create does not work in GAE Datastore viewer

When I try to create some entities I don't see the option to input fields. I just see the SaveEntity button.
However I can view all the existing entities.
What is very strange is - there is another entity called VideoEntity for which the create did not work yesterday but works today.
Can somebody help me with this seemingly unpredictable tool ?
Regards,
Sathya
i think the console knows what properties each entity has based on existing data, rather then your models. and the data is only updated periodically. when did you upload your app? maybe waiting a few hours will give the console time to update.
alternatively, you could use the remote api to add your entities, or write a small snippet and upload such as ...
VideoStatsEntity(app='home', ip='116.89.52.67', params='tag=20130210').put()
Writing a simple interface to the data-store to allow you to edit/create models is probably the best thing to do in this case. You know what they contain so you can adjust your interface accordingly, rather then waiting for the admin interface to "catch up" as Gwyn notes.
I believe that there are some property types that are impossible to add via the admin interface that you are using so you'll probably get to the point sooner rather then later of creating a custom interface.
The admin datastore view is good for quickly checking out the contents of the datastore, but ever tried paging through 100's of entries? Not fun.

Cancelling and discard changes in Entity Framework and WPF

I am developing a system where the user have forms to fill and this info is stored in a database. In one of the screens I have the following flow:
Inside a project he can add multiple orders.
Each added order opens a configuration screen which internal information of the order can be set. Also it is possible to add items to the order.
Each added item opens a configuration screen that allows user to set internal informations.
To sum up, the flow is Project -> Order -> Item, where "A -> B" means A contains 0, 1 or more Bs.
This project is being developed using WPF with Entity Framework to manage the database access. I am binding entities from Entity database mapping directly into the screens. This way we have some "advantages" with WPF, as not null fields are painted red, for example. Moreover, the code is much simpler. The entities are only updated (or added) to the database when the project is saved. Therefore, items can be edited, then orders can be edited and they are only saved when the project is saved, since they are dependent of the project ID to be saved.
However, using mapped entities directly has a huge problem: let's say the user edits an Item inside an Order. Then, he edits it again, but this time, instead of pressing "OK", the user presses "Cancel". The changes must be reverted. However, the previous state was lost, since it was not updated in the database and were changed because of WPF binding.
What is the best approach to prevent this problem? I don't know if creating clones work well in Entity Framework. And creating variables that only transfer their values to the object when the confirm command is clicked would be against the MVVM pattern, and would make me lose the "advantages" I have already talked about.
Please help me!
Thanks!

WPF live Update from database

I am building a multiuser WPF application (requirement is a desktop app), database SQL Server 2008.
There are two types of users.
The first type user will enter a record which would be stored in a table.
The second type user initially will be presented with a listbox with the records from the table. The requirement is that the listbox must be updated live (i.e. if a new record is entered by the first user the listbox must be updated).
Currently I have not yet implemented the Database functionality.
I am currently working with ObservableCollection and simulating this scenario.
I would like to know the best approach for achieving this.
Should i use a timer and keep querying the table? Is there a more efficient way?
Is this even possible (should I switch to ASP.NET?) ?
I would appreciate any suggestions and tips that you may have.
Try using SqlDependency, since it can raise an event (OnChange) in your code when the results of a query would change on the server (as the result of an INSERT or UPDATE from another user or process, for example)
An example of its use on CodeProject for a simple live chat application:
http://www.codeproject.com/KB/database/chatter.aspx

Best practice for updating silverlight deployment that is actively being used

I am currently running a SL3 project where we are in a highly iterative development mode with about 25 active test customers. I am making small changes at a clip of about 4 new builds per day. It is important to know this application is mission critical line of business for these 25 people, it is the tool they use all day to do their work so they are using it constantly and often launch their browser and the app in the morning and never close it until the end of the day.
The challenge is that when I make an update to the application I have no clean way to notify the users, in most cases this is ok as it is rare that I introduce a data contract change or something that would be a classic 'breaking' change to the app/service. Users keep plugging along and will get the change next time they refresh.
Right now we have resorted to emailing everyone and telling them to force refresh or close the browser and log back in.
Surely there is a better way...
Right now my train of thought is to have a method on the server that compares client xap versions and determines if the client being used is the most up to date, if so I will notify the user and make them update.
What have you done to solve this problem?
One way of doing it is to use a push mechanism (I used Kaazing Websoocket Gateway but any would do). When a new version of the XAP is released a message (either manually entered into the system by admin or automated triggered by XAP file change event) would be sent to all the clients. In the simplest scenario some notification would be shown to a user (telling him that a new version is released and the application needs to refresh) and then the app would refresh (by simply reloading the page) saving user's state if necessary.
If I would do this I would just keep it simple. A configuration value in web.config and a corresponding service method that simply returns that value (the value itself could be anything, but a counter is probably wise). Then you could have your Silverlight app poll that service method at regular intervals. Whenever the value changes (which you would do manually when you deploy a new version), just pop up a dialog telling the user to refresh the browser or log in/out. This way you don't have to force them to refresh every time. If you go with the idea of comparing xap file versions they will always be required to refresh, even for non-breaking changes.
If you want to take it further you could come up with some sort of mechanism to distinguish between different severity levels. For instance, if the new config value would contain the string "update_forced", you could force the users to reload the app by logging them out automatically (a little harsh, perhaps). If it contains the string "update_recommended", just show a little icon at the top right corner saying that there is a new version and that they should upgrade in their own time.
Granted, this was targeted at Silverlight 3, but with the PollingDuplex client and such in the newer versions of Silverlight, you could publish an "Update Now" bit to the clients, and build a mechanism in the client to alert the user that there is an update that is now out... that they should update it shortly, etc. You may even be able, through serialization and such, to save the state that they are in when they close the app to reload it.
We've done stuff similar with a LOB app that we built, so that as users are changing things, the rest of the userbase sees those changes immediately. Next up will be putting the flags in to change authorization and upgrades "on the fly" if you will.

Resources