Zoli
Opinion seeking, Best Approach for Managing Form State in a .NET MAUI Application?
Hi,
I have a .NET MAUI application with many pages and viewmodels. There is a form page where I want to prompt the user to either start a new form or resume filling out the existing one whenever they return to the page.
I’ve read that viewmodels should not be singletons. My idea is to create a CustomFormViewModel, which would contain a property for FormPageViewModel (holding the user-input properties). I would then inject this CustomFormViewModel into the page's viewmodel.
What do you think of this approach? Any other suggestion to keep the state?
Thanks a lot
1 replies
✅ UNIQUE constraint failed
I have the following relationships
One workout can have multiple exercises and each exercise can have one exercise variant I would like to implement this but I am getting an exception when saving a newly created workout
An error occurred while saving the entity changes.See the inner exception for details. // ---> Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 19: 'UNIQUE constraint failed: ExerciseVariant.Id'.My classes ExerciseVariants is bulked inserted from an api when the app first launched.
21 replies
User ID on Client Side or Not?
Hi,
I’m building a Feed application, somewhat like Facebook. So far, I’ve implemented it in a way that avoids fetching or using user IDs on the client side. For example, when fetching posts or liking/commenting on one, I send a request to the server with just the post ID. On the server side, I handle the like or comment by finding the post using its ID, which is tied to a user ID, and update it as needed. This way, I don’t need to use the user ID on the client side. Similarly, when fetching data for myself as an authenticated user, I get my user ID directly from my identity on the server, so there’s no need to pass my user ID explicitly.
However, if I want to implement a profile page, I’ll need to locate and display a specific user’s profile. For this, I assume I’ll need the user ID available on the client side. Would this approach be fine?
What is your opinion about?
Thanks in advance!
5 replies
Determining Feed Ownership for Edit/Delete Functionality in a client application
In a feed-based .NET MAUI application with authenticated users, how should ownership of feeds be identified to allow editing and deletion?
1. Should feeds be fetched in two separate requests (one for the logged-in user's feeds and another for all other feeds), then merged client-side with a flag (e.g., Mine = true) to indicate ownership?
2. Alternatively, should all feeds include their ownerId, enabling the client to identify ownership based on the logged-in user's ownerId? Is it secure and appropriate to expose ownerId values to the client in this approach?
3. Fetch feeds with a single API call, but let the server mark ownership before sending the response (e.g., include an isEditable or isOwned property). This combines the best of both?
4 replies
Resolving Entity Framework Core Tracking Issues with Shared Relationships in .NET MAUI and SQLite
Context:
I am developing a .NET MAUI application using Entity Framework Core with SQLite as the local database provider. The application involves managing workouts, exercises, and exercise variants. Below is a detailed explanation of my workflow and the issue I am encountering:
Workflow
Saving Default Exercise Variants Locally
Upon user login, I fetch a predefined list of exercise variants (with fixed IDs) and save them locally using the following code:
Retrieving All Exercise Variants
To retrieve all locally stored exercise variants, I use the following query:
Creating and Saving a Workout
When creating a workout, I define it with associated exercises as shown below:
Continue in the comment section
6 replies
How to Filter Firebase Real-Time Data for the Logged-In User in a Maui Application?
In my Maui application, I have successfully implemented email/password authentication and real-time data synchronization. To filter data so that only the logged-in user's relevant documents are shown, should I add a new property (e.g., ownerId) to each document and perform the filtering on the client side?
I saw WhereEqualTo could filter but is this the right approcach to filter on the client by the ownerid?
But it means I need to store the userId on the client, is it safe?
(I saw some relevant questions but they are 6-10 years old and not maui/.net related if matters at all)
13 replies
What is the proper way of keeping the state of vm when closing and returning back to the Maui app?
I have a complex form with various input fields such as entries, checkboxes, etc. When the user closes the app and navigates back to the app, I want to ensure that if they return to the form, they can either continue from where they left off or start a new form.
One of my idea is implement Auto-Save in the ViewModel
In the viewmodel implementing an auto save strategy so when any property is changed it saves to cache using Barrel, so it can be loaded from there.
cons this form has few nested collections too means i need to subsctribe to all items and all items's property when added to detect the change, any good advice or libary or tips to make it better?
5 replies
Issue with Nested One-to-Many Relationships in SQLite-Net Extensions: LevelTwoModels Property Return
I am working on a model hierarchy using SQLite and SQLite-Net Extensions. The model structure consists of three levels: RootModel, LevelOneModel, and LevelTwoModel. Each model contains properties that establish relationships with the next level, as follows:
RootModel has a one-to-many relationship with LevelOneModel. LevelOneModel has a one-to-many relationship with LevelTwoModel. Here is my implementation:
public class RootModel
{
[PrimaryKey, AutoIncrement]
public Guid Id { get; set; }
public string? Name { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<LevelOneModel> LevelOneModels { get; set; }
}
public class LevelOneModel
{
[PrimaryKey, AutoIncrement]
public Guid Id { get; set; }
public string? Name { get; set; }
[ForeignKey(typeof(RootModel))]
public Guid RootModelId { get; set; }
[ManyToOne]
public RootModel RootModel { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<LevelTwoModel> LevelTwoModels { get; set; }
}
public class LevelTwoModel
{
[PrimaryKey, AutoIncrement]
public Guid Id { get; set; }
public string? Name { get; set; }
[ForeignKey(typeof(LevelOneModel))]
public Guid LevelOneModelId { get; set; }
[ManyToOne]
public LevelOneModel LevelOneModel { get; set; }
}
I am using SQLite-Net Extensions' InsertWithChildren and GetWithChildren methods for data manipulation. While the relationships between RootModel and LevelOneModel work as expected, the LevelTwoModels property in LevelOneModel always returns null when retrieved.
What could be causing this issue, and how can I resolve it to ensure all relationships are correctly handled and retrieved?
All three table is created.
For insert:
await SqliteDataStore.Instance.Database.InsertWithChildrenAsync(Data);
For get:
return await SqliteDataStore.Instance.Database.GetAllWithChildrenAsync<RootModel>();
12 replies
I’m looking for feedback on the security setup, which uses .NET MAUI, ASP.NET Core, and MongoDB.
Overview of the Architecture
1. API Authentication:
To authenticate API calls from the MAUI client, I use an API key that is securely stored in Azure Key Vault and accessed by the API as needed.
2. Database Connection:
The connection string for MongoDB is also stored in Azure Key Vault, allowing the API to retrieve it securely, so no sensitive information is stored within the API itself.
3. User Registration and Authentication:
When a new user registers through the MAUI app, a document is created in MongoDB’s user collection, with the password stored as a secure hash.
On login, the API generates a JWT token, saves it in MongoDB, and sends it to the MAUI client, where it’s stored using Secure Storage. Any previously saved token is replaced with the new one in both the database and on the client.
4. Data Ownership and Access Control:
After logging in, users can create and save data, which the API stores with an owner ID field set to the user’s ID.
When data is retrieved, the owner ID is passed to the API to filter results, so users can only access records that match their own ID.
5. Sensitive Data Management:
No sensitive information, such as connection strings, API keys, or password-hashing keys, is stored directly in the API; all are injected securely from Azure Key Vault.
On the MAUI client, the API key is stored in appsettings.
Questions
1. Storing the API Key on MAUI:
Is appsettings on the client side an appropriate place for storing the API key, or is there a more secure alternative?
2. Overall Security:
Do you have any suggestions to enhance the security of this architecture?
Thanks in advance for your input!
116 replies
How to get documents for specific user by Cloud Firestore?
I am developing a .NET MAUI application where users can register and sign in. After logging in, they can perform CRUD operations on documents. Each document saved to Cloud Firestore includes a user_id or owner_id property to indicate the owner.
What is the best way to filter documents so that I only retrieve those associated with a specific user?
Currently, I am using:
CollectionReference documentsRef = db.Collection("documents");
and then querying for documents where UserId equals the currentUserId.
Is this the correct approach, or does this method initially retrieve all documents from the collection (for all users) before filtering by UserId?3 replies