Skip to content

Property which returns value of property in backing field #24277

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
prodenx opened this issue Feb 25, 2021 · 1 comment
Closed

Property which returns value of property in backing field #24277

prodenx opened this issue Feb 25, 2021 · 1 comment

Comments

@prodenx
Copy link

prodenx commented Feb 25, 2021

I am wondering if it is possible to achieve next case

My entity has backing field of owned type. The owned type has navigation property to another entity

Product (entity)
             |_ _productTitle (backing field, owned type)
             |                         |_ _.Value (navigation property)
             |                                         |_ _ Value (Property)
             |                                         
             |_ _Title => _productTitle.Value.Value;        // It is ugly, I know. Sorry for naming

The idea (main goal) is to incapsulate logic of Title property inheritance (Product title can be inherited from title of parent node in product tree) inside of Product entity. And also that Title property of Product would be allowed to be used in linq query.

Some code

// Product entity
public abstract class ProductTreeSpecificationBased: Entity<Guid>
{
    private Title _title { get; set; }                    // backing field of owned type
    public string Title => _title.Value.Value;    // property which is used by product consumer and in queries
}

// Title owned type
public class Title: ValueObject, IInheritedProperty
{
    public ProductTitle Value { get; private set; }

    public bool IsInherited { get; private set; }

    public Title(ProductTitle value, bool iIsInherited = false)
    {
        Value = value;
        IsInherited = isInherited;
    }
}                           

// Product Title entity
public class ProductTitle : Entity<Guid>
{
    [Required]
    public string Value { get; private set; }     

    public ProductTitle(Guid id, string value)
    {
        Id = id;
        SetValue(value);
    }
}
  1. I know/guess that I can use backing fields of owned type. It can be achieved via
.OwnsOne(typeof(Title), "_productTitle", x => 
{
    x.Property("Value")
    **// how to specify navigation?**
}) // **is it correct way?**
  1. How further to configure model to achieve the main goal?

I know about #6674. I found the example about Samurai, but I do not really understand how that works and example was provided in 2017, but actually issue was closed by milestone 5.0.0, which was released on previous year.

@anranruye
Copy link

As you said, the real problem is not using field-only navigation properties(or 'backing fields').

And also that Title property of Product would be allowed to be used in linq query.

This is not supported, because Title property of Product entity is not mapped to an actual database column. Even worse, using Product.Title in linq query may cause client evalution.

To use Product.Title in linq query, you should create a real db column, and keep its value same with the value in ProductTitle table. See https://docs.microsoft.com/en-us/ef/core/performance/modeling-for-performance#update-cache-columns-when-inputs-change

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants