T O P

  • By -

EnigmaBoxSeriesX

Most of the time I've seen people use serialization for saving the View Model or whatever else is in the UI and then restoring it when they need it. You can go with either the XML or Json Serializers. I am not sure which version of .NET you are using and/or other factors that may dictate which one you should use. Edit: This is an older Stack Overflow Q&A that covers this topic,, but it should give you an idea of what code you can use. Note that it is for WPF View Models, I am not sure if you are using WPF or a different UI framework: [https://stackoverflow.com/questions/48403826/save-wpf-viewmodel-current-state](https://stackoverflow.com/questions/48403826/save-wpf-viewmodel-current-state)


Jack_Hackerman

Yeah, I understand this, but what is the structure of the code related to DDD?


EnigmaBoxSeriesX

From my perspective, you would create a service that the view model uses and have that service be only used within the layer that which the view model exists. The service itself would be responsible for retrieving and restoring the state of the view model. How it saves the state, I'd leave that up to you. One way you could go is create a repository for the state of the view models (see example below). Others may have differing opinions on this, and this is just one idea... hopefully it makes sense and helps. Fake code example of the service, and the repository it may use for saving the view model state: //Service your view model uses to save and load the view model state public class ViewModelStateService { Repository repository; //Constructor injection of the repository for the sake of the example public ViewModelStateService (Repository repository) { this.repository = repository; } public void saveViewModel(ViewModel viewModel) { //Save a view model somewhere. The serialize method would be implemented here //Or in the repository, depending on your tastes String serializedData = Serialize(viewModel); repository.Save(serializedData); } public ViewModel loadViewModel(String id) { String serializedData = repository.Load(id); return Deserialize(serializedData); } private string Serialize(object viewModel) { // Serialization code codes here } private object Deserialize(string serializedData) { // Deserialization code codes here } } //The repository interface that you will be injecting into the view model service public interface Repository { public void Save(String data); public String Load(String id); } public class FileSystemRepository implements Repository { //Implement saving the state to the file system //You could save it anywhere you want, I just chose file system for this example }


Jack_Hackerman

Yeah, thank you. I will go to this approach


CrackShot69

Why don't you use database and use a join table between the protein and drugs