Change navigation property accessibility

5 posts, 2 answers
  1. Paulhenri
    Paulhenri avatar
    7 posts
    Member since:
    Jan 2013

    Posted 24 Jan 2013 Link to this post

    Hi all!

    I have a Measure class and a Result. I have a relationship "A Measure can have many Result", and a navigation property IList<Result> Results in my Measure class.

    Now, to make my development easier, i wan't to have a property IDictionary<MyEnum, Result> ResultDictionary to use instead of Results. So I would not use Results at all from ouside, and use a method AddResult that would be like
    public void AddResult(Result result)
    {
    //to keep ma data persistent:
    this.Results.Add(result);
    //to keep my dictionary up to date:
    this.ResultDictionary .Add(result.Key, result);
    }
    My question is, is there a better way to achieve this? (I'm using the visual designer)

    If not, is it possible to change the accessibility of the navigatinal property Results, to set it as private, so I make sure that I don't add an item to it without updating my dictionary?
    And last question, I want to initialize my dictionary when my IList<Result> is loaded from the DB, how to do so?


  2. Answer
    Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 28 Jan 2013 Link to this post

    Hello Paulhenri,

    You can achieve this following these steps:

    1) You should change the access modifier for a navigation property by using the Batch Operations Dialog 
    - To start the Batch Operations dialog, right-click on an empty area in the Visual Designer and then select Batch Operations...
    - From the top left part of the window choose Display Mode -> Members.
    - Find and Select the navigation property for which you want to change the access modifier.
    - From the top right part of the window choose Operation -> Change Member Access Modifier and Set Access Modifier to: Private.
    - Execute the operation.
    - Save the domain model.

    You can also find a demonstration of the Batch Operations dialog on Telerik TV.

    2) After that you should implement the logic for the Dictionary. I would recommend you to expand your class with a partial class and add there a property which returns a dictionary. 

    For your convenience I prepared the following code demonstrating this approach.
    01.public partial class Measure
    02.{
    03.    private Dictionary<int, Result> _resultDictionary;
    04.    public IDictionary<int, Result> ResultDictionary
    05.    {
    06.        get
    07.        {
    08.            if (this._resultDictionary == null || !this._resultDictionary.Any())
    09.            {
    10.                this._resultDictionary = Results.ToDictionary(res => res.Key);
    11.            }
    12. 
    13.            return this._resultDictionary;
    14.        }
    15.    }
    16.}

    3) By default Telerik OpenAccess ORM uses Lazy Loading which means that when objects are returned by a query, related objects are not loaded at the same time. By default, they are loaded until explicitly requested using a navigation property. The code above will load the dictionary this way.

    If you want to use Eager Loading for the navigation property and the new dictionary and load them along with the Measure object, you should create a FetchStrategy.

    For example:
    1.using (EntitiesModel1 context = new EntitiesModel1())
    2.{
    3.    FetchStrategy fetchStrategy = new FetchStrategy();
    4.    fetchStrategy.LoadWith<Measure>(m => m.Result);
    5. 
    6.    context.FetchStrategy = fetchStrategy;
    7.}

    For using FetchStrategy you should include Telerik.OpenAccess.FetchOptimization namespace.

    You can take a look at the dedicated video about Fetch Strategies on Telerik TV.

    Note that the Dictionaries that you add in partial classes will be accessible in a read-only manner, meaning that if you add an item to a dictionary, the link between the parent and the child object will not be automatically persisted in the database. Also, for performance reasons, what I have recommended involves loading the collection into a dictionary only once, so any changes in the database that might occur afterwards will not be represented in your dictionary (unless you take manual actions to reload it).

    If you need any further assistance with OpenAccess ORM do not hesitate to contact us again.

    Greetings,
    Boris Georgiev
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  3. DevCraft banner
  4. Paulhenri
    Paulhenri avatar
    7 posts
    Member since:
    Jan 2013

    Posted 28 Jan 2013 Link to this post

    Thanks for your reply Boris.

    However, in the Batch Operation Dialog, I can't find the Operation Change Member Access Modifier.

    The Batch Operation are the same as in the help page.

  5. Answer
    Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 28 Jan 2013 Link to this post

    Hello Paulhenri,

    Please accept my apologies for the inconvenience caused.

    By default the operation for changing the access modifier is not visible in the dialog. The reason is that we would prefer to hide some operations that might harm the model if not used carefully, but still make them available when they are really needed. As this particular operation is not risky, we are planning to make it publicly available in Q1 2013.

    To access the operation before the Q1 2013 is released, you should perform one simple step. 
    Please open the "..\Telerik\OpenAccess ORM\dsl2012\Extensibility\BatchOperationsDialog" folder and create an empty file named "Diagnostics.xml", then restart the Visual Studio. Originally it should be located in C:\Program Files or C:\Program Files(x86) depending on your operating system x86 or x64 accordingly.

    I hope that helps.
     
    Kind regards,
    Boris Georgiev
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  6. Paulhenri
    Paulhenri avatar
    7 posts
    Member since:
    Jan 2013

    Posted 28 Jan 2013 Link to this post

    Works like a charm!
    thanks
Back to Top
DevCraft banner