Updating property via code

Good noon everybody!
I am looking to update the property of an object. What I want to do is set the viewmodel prop “CurrentAddress” to “Address” if they are not equal to one another. The current address is hidden and is not to be displayed in either the front end or in the back office of the application. Just update the value current address if address is changed.

The problem is I don’t see an easy way to update values in the database through code as far as I can tell. Other services provide rather straight forward CRUD operations. However all of these services can only show the method definitions. Not their implementations.

So I was wondering is there some clear cut way of updating a value directly through code? For example mapping the model to and from something that would allow for the update.
Best regards.

Litium version: 7.2.3

Which entity in the database do you want to update?

It’s not clear what you want to accomplish.

It’s one of our own simply called Workshop. It’s for a page template which has an “Address”, a “CurrentAddress” and “Coordinates” string properties.

What’s supposed to happen really is: Someone makes a change to address in back office: Address != CurrentAddress -> Make Api call, fetch coordinates and set current address to address. Now if the Address isn’t updated we are not making any unnecessary calls to retreive said coordinates. Yet if someone changes the Address, once again the coordinates will be fetched and CurrentAddress is set to the value of Address.

It’s really easy to get values to display in a view for example. I am wondering if there is a convenient way to make updates?? Maybe something along the lines of EF (without my logic impl ofc):

using (var context = new DBEntities())
{
var workShop = context.workShops.First();
workShop.CurrentAddress = workShop.Address;
context.SaveChanges();
}

best regards.

You can use Litium.Events.EventBroker for updating various entities.
Take a look at this:
https://docs.litium.com/documentation/architecture/events-handling

Use a Service to update if a service exists for that entity otherwise use DataService
https://docs.litium.com/documentation/architecture/data-service

Hi thanks for the resources, we have been looking into that but the following code does not work. (think we are missing some commit?)

Currently we are using code below, but field after AddOrUpdateValue is executed and on following queries returns null for the field.

update =>

using (var query = _dataService.CreateQuery<Page>())
{
	var page = query
		.Filter(f => f
			.Bool(b => b
				.Must(m => m
					.SystemId(pageId))))
		.FirstOrDefault();


	var update = page.MakeWritableClone();

	if (culture != null)
		update.Fields.AddOrUpdateValue(fieldId, culture, value);
	else 
		update.Fields.AddOrUpdateValue(fieldId,value);

       // directly after call and subsequent calls
       update.Fields.GetValue<string>(fieldId, CultureInfo.CurrentCulture) // NULL
}

The field in itself is defined =>

new FieldDefinition<WebsiteArea>(PageFieldNameConstants.fieldId, SystemFieldTypeConstants.Text)
{
      MultiCulture = true,
      Hidden = true,
      Editable = false,
      SystemDefined = true
},

Using the DataService to fetch pages by their known system identifier is wrong way and will have performance implication because all request is touching database. Instead you should use the PageService.Get(systemId)-method that will use the cache and reduce the load on the system.

You set the value with culture and fetching the value with CultureInfo.CurrentCulture, is the culture and CultureInfo.CurrentCulture the same?

Hi, yes CurrentCulture is in our case sv-SE.
Also tried in VS Immediate Window to

page.Fields.GetValue<string>(fieldId, culture);

But still null.

Did you check the updated page or the original page? page.Fields vs update.Fields in your examples.

I tried recreating your field and your use case locally, and it works for me as long as I use the correct culture.

Yes. Though during testing with the debugger i would get the data in the update object but not on page object i think i need to do => _pageService.Update(update)?

Because after that _pageService.Get(pageId) works though for some reason it only worked as long as I was in scope. If I would refresh page in BO or press Publish the next _pageService.Get(pageId) it would be null again.

Now suddenly i get the data without any code changes. But PageModel that gets passed in ModelBuilder Build method that field does not exists and is null when mapped to the ViewModel.

Update:
Now again after closing debugger and starting again i’m getting null after _pageService.Get(pageId)
I have a feeling this is something with cache?

var page = _pageService.Get(pageId);
var update = page.MakeWritableClone();

if (culture != null)
	update.Fields.AddOrUpdateValue(fieldId, culture, value);
else
	update.Fields.AddOrUpdateValue(fieldId, value);

_pageService.Update(update);

Please, see this post also Need to add only one channel to a page while creating through code

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.