Getting ViewModelReference in another ViewModel - xamarin.ios

We have an application wich has a typical Master/Detail view. The master view displays a list of ticket titles and if you click on a ticket title you see a view with the ticket details.
The master view has a model wich is a list of "ticket" objects each containing more information about a specific ticket. We bound a handler on click which basically instantiates a new detail view and displays it. This works fine, except that we have to give all the config values as primitive data types to the constructor (we know we can't use objects when instantiating)
We thought about another way to do this but couldn't get it working. In the Init() (of the detail view model) we only pass the ID (of the clicked ticket) and wanted to use Mvx.Resolve to get the master view model. So question one would be, can you fetch arbitrary view models inside other view models?
A second idea would be write a base view model class from which all view models in our app inherit, which registers the view model on instantiation to a service so that you can call that service and fetch the view model from anywhere (mediator pattern). Would that be against the framework?
An answer that I have found is "use messages" but to me it seems sort of bulky, at least how I understood this. The detail view model would have to send a message to the master view model "Ok I'm ready now" and then the master view model would say "Alright, heres the configuration".
So to sum up the questions:
Is it possible to fetch certain view models from the framework inside other view models?
Would implementing this by hand be against the framework?
Did we understand the message approach correctly and if no, what would be a more lightweight/generic way of doing it?
Cheers and thanks
Tom

Usually when I have to share data across View Models, I use a Service to manage it. The service is typically injected via IoC.
I treat View Models as a mediator between the Services and the View. Most of my logic is stored in the Service. The only things I have in the View Model are properties for data binding, commands that dispatch to a service, and other presentation level concerns.
For your scenario, I would have the MasterViewModel pass an identifier to the DetailViewModel, where it will ask the ConfigurationService for the configuration by id.
Hope this helps.

Related

Shared business logic and events in DDD

I am currently checking out the DDD pattern and (tried) to refactor a project of mine accordingly.
Now I got a case where I don't really know what to do: Some business logic can be called by multiple entities. In my case it's possible to mark items as hidden.
The item can be a Product or a Category (or many more).
The related event is called MarkedItemAsHidden, fired by an action handler called MarkItemAsHidden, which accepts the item as first parameter and fires the event based on the item's class.
So I got:
Events: MarkedItemAsHidden
Actions: MarkItemAsHidden($item)
Projector: HidingProjector
But where do I put those classes?
My app has a Domain-directory, which includes:
Product
Category
Cart
...
but I can't really decide where to put these "shared classes".
Some of what you are describing lies outside the boundaries of your Domain.
Your Domain starts from the Application Services. Application Service lie on the boundary of your Domain. The job of the Application Service is to translate external messages into Domain Actions.
For Example: Assuming you have a complete application with UI and Domain. You want to mark an "item" as Hidden.
So you:
Trigger from your "Action Handler" that might be in the UI (a button
that's clicked as an example.). This is outside of the Domain. Your
click generates an event that is handled by the
ApplicationService(ItemAppService). How exactly the event gets to the Application Service is an implementation detail (it's not a DDD concern). For Example: ItemAppService could be a Controller class in a Rest API App. In this example, your event is a HTTP Request and the Implementation Detail would be something like the ASP.net core API framework that routes that HTTP call to your Controller.
ItemAppService.Hide(ID: String) is the method that's ultimately triggered. ItemAppService is in the Domain and Using Domain
Concepts like Factories and Repositories, it can retrieve an instance of "Item".
After retrieving the "Item", it'll then call a DomainService (or the
appropriate Domain Class based on your Ubiquitous Language) that
knows how to hide an item.
It then returns some sort of a response to the caller on the
results.
ItemAppService doesn't care how its hide function is called. You can't pass hide a Domain Entity because that'll mean that the consumer of the Domain action knows what and how to get an Entity.
If your intention is to use Events and Triggers as part of your Domain Model, those concepts must be translated into the Ubiquitous Language. When you do something like "generate an event" inside your Domain, your Domain doesn't care how you "generate an event" so that logic or processing is usually left to something outside of the domain (that's injected in through IOC) that can contain the specific knowledge of how to "generate an event".

Create contact without user

I try to create a contact that is not associated to an user. All "real" users are imported from LDAP. I want to show a global contact list for different purposes like a birthday-list and a phone book. Not all entries in these lists are imported as users. Now I want to create these contacts with the ContactLocalServiceUtil class programmatically. Are there any advices how to do this? There is no method that needs neither a user-id nor a contact-id.
A Contact, as contained in Liferay's API, is always the contact data of a user. Just because the name describes what you need, does not mean that the underlying concept matches as well. You probably need different data for a general purpose phone book anyways and it's probably easier to introduce your own contact class than adjusting the existing model (you can't add fields to API classes anyway - your only way of extending Liferay's ContactModel would be through Expando fields)
Therefor the advice is: Create your own contact class. If you want to react to user data changes when LDAP is updated, you'll need to frequently import the user data and for example create a model listener on Liferay's contact that updates your custom contacts whenever an update from LDAP is coming in.

How entities could be connected with each other inside the domain?

Let's assume there are two domain entities:
UserImages with methods addNewImage(), removeImage($imageId), getImages($from, $count).
UserProfile with fields name, age, mainImageId, etc.
Following functionality is desired inside the domain: when application layer calls UserImages -> addNewImage(), UserProfile -> mainImageId is set automatically in case it was empty.
So, what is the best way and best place to implement an in-domain over-entity business logic? Domain events with observing services, referencing special services from the entities, or somewhat else?
I create all the entities using a some kind of factory, i.e.
$userImages = Domain::userImages($userId); // getting an instance of UserImages
$newImageId = $userImages -> addNewImage(); // adding a new image
I also should mention that I will have a lot of logic like described above in my project.
Thank you very much for help!
First off, as specified in your other question, UserImages is not itself an entity. Instead, there is likely an entity called UserImage which refers to a user's image, singular. A UserImageRepository can then provide access to all images associated with a user, using pagination where necessary.
Now, the use case is:
When a new user image is added, if a user's profile image is not set,
set it to the added image.
The term when indicates a domain event. In this case, the event is UserImageAdded. The bold if and the subsequent set represent the desired behavior in response to the event.
To implement this use case, an event must be published when a user image is added and a handler implementing the desired behavior must be subscribed to this event. This linked domain events article provides a sample implementation in C#, however the concept can be easily ported to PHP. Overall, the required components are:
An application service which handles the adding of user images. This service will publish the event.
A domain event publisher which is called by the application service and allows decoupling of publishers from subscribers.
A domain event handler which handles the UserImageAdded event and invokes the desired behavior.
User sounds like Aggregate for me (it's difficult to give you an opinion without context :) ), but it can solve your problem.
If Profil and Images are differents bounded context, may be you can use Domain events (it's helpful for cross bounded context communication)

Lookup fields in CRM 2011

Entity Application has a look up to Contact. Now, Entity Application Document has a look up to Application. In my Application Document form, I want to display the Contact who is associated with the Application Document. I do not want to be creating a look up to Contact as it is just bad database design.
How can I show the related contact in the Application Document form. It is after all a look up - alright, a two level look up right? I can go from the Application Document to the Application to which it is related and from there to the Contact.
My suggested solution would be to have a read only text field in the Application Document entity. Populate it with Contact (Primary Field) onloadform with a JavaScript.
I'd resolve it as a call to the DB, fetching the linked entity and then fetch it's linked entity. JavaScript seems to be the wises choice in this case.
However, you mention that you find this solution bad, so I might be missing a requirement.

JSF project wiring

I am writing a simple enough program using JSF, and I need some advice about how to go about it. I have a jsp which takes a unique ID and has to find out if the ID exists in 3 different databases. If it does it should display a message telling user, where it exists, otherwise it should give the user option to add the ID to a particular database.
I have the jsp page which has a text field for the input ID and I have a button called "Submit" which should trigger the process of querying the db to see where the ID exists. My question is, how to structure this project, in terms of front end, middle teir and db layer.
I have a JSP page, when the user clicks the Submit button, I have a listener in the managed bean which gets executed. I have also read up that the listeners can either be a managed bean or a separate class. Should I have a separate class which is the listener? If so, should it be a Servlet mapping in the web.xml file, so all request get forwarded to this class. Should there then be a separate DAO class where the actual query
gets executed. We are using hibernate as well.
I would jsut like to hear people comments about how many classes there should be and how a particular ID Check will flow through the program from JSP->Servlet(?)-> DAO and then back to the same jsp. There is only JSP , there will be no other navigation pages.
Any direction will be much appreciated.
I have used Spring in the past, and this would be a breeze fore me with Spring, using the Controllers to delegate the requests to the appropriate service, and then the service would call the DAO class. But here we are using JSF and it has to be a JSF web page. I have not used JSF before so I am unsure about the different components needed. I have the front end jsp and a DAO class with the actual query, just wondering about the middle tier, with business logic. How does the front end request after clicking the Submit button get to the middle tier, what wiring is required? Is it in the web.xml?
I would suggest you take a good long look at Spring framework. Here's a Spring MVC tutorial to get you started.

Resources