Need simple example for ASP.Net API to handle relationships

5 posts, 1 answers
  1. Nuno
    Nuno avatar
    12 posts
    Member since:
    May 2008

    Posted 13 Mar 2013 Link to this post

    Can please anyone provide me with a sample to some crud in ASP.NET API WebServices created with telerik ORm?

    For an example i have this *Domain Classes* (Note the design is not for production, is just for tests! So please ignore the associations below), but to resume:
    I have extended "Product" class with a property that basically gets
     the "Name" - from (this.Name witch comes from "ProductLocale") + (this.Categorys.Name  witch comes from "CategoryLocale").

    Now this is working in a sample console project where i actually tested all CRUD and all goes well (i have also modified "isMAnaged" to True) and i can access everything.

    Then to test Web.API i have generated another webproject webapp1 with this exact model!

     Now when i access in Web.Api like http://webapp1/api/products (i am using Fiddler to get response), but it fails and when debugging it
    falls to null exception on this.Categorys.Name because it simply null the only response get is from the actual this.product not the relationships.

    Now is this a limitation or am i doing something wrong?If there is a way please advise me on this!


    *Domain Classes* - For testing purposes
  2. Nuno
    Nuno avatar
    12 posts
    Member since:
    May 2008

    Posted 15 Mar 2013 Link to this post

    Could Telerik team insert in help of ORM Online Documentation help in the Web.Api section a how-to work with relationships, just like the other WebServices??
  3. DevCraft banner
  4. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 15 Mar 2013 Link to this post

    Hello Nuno,

    Currently the Web API implementation does not support associations as Data Services do.
    This is due to the very limited OData support.
    We are planning to include that feature using the OData verb $expand, but I cannot give you an exact timeline when it will be shipped.
    As soon as we have support for handling relationships and other OData features we will update our online resources accordingly.

    There is a possible work around that will enable you to load product category using some additional code, but it is not flexible enough to be applied everywhere. If you are interested in it, please let us know.

    Can you be more specific for the error that you have experienced?
    Where does it occur?
    What are the reproduction steps that lead to the issue?

    Thank you for your feedback and please excuse us for the inconvenience caused.

    All the best,
    Viktor Zhivkov
    the Telerik team
    OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Check out all of the latest highlights.
  5. Nuno
    Nuno avatar
    12 posts
    Member since:
    May 2008

    Posted 15 Mar 2013 Link to this post

    I , thank you for the quick response.

    Well i was actually testing, so no worries in a quick implementation, it's just that i was thinking that could be something that i was missing.
    I will wait until a release on this will be available.

    I was playing arround with Entity Framework a little with some MSDN samples, and i actually was getting relationships in one model like this one used in web.api + entity framwork 5, so that´s why i was thinking that maybe there was something more that i was missing...

    But i really prefer using your ORM, mainly because i am quite acostumed to ASP.Net ajax controls, (that's what my company uses for a whiel now) and also because your orm with this Services Generation On the fly seems to be quite a time saviour for small teams like mine.

    So i will be in the lookout for a newly release with this option and then i will get into it again.

    Right now i am looking to integrate in our ASP application and exploring scenarios, because we had a project manager and director wich was generating DAl code with other tools and now we need to use new ones as no one knows about the older generation tool.

    I hope i can have good performance with this and can still maintain older code on par of new one, the old code was using the Stored procedures aproach , so this is something new to me and the team.

    We are following your exemples and trying to look for the best approach.

    My main concern is that we have several  tables in this format:
     Ex:
    [Products]        [Products_Locale]    [Culture]
    ProductID        ProductID                 CultureID
    OtherFKID       CultureID                  CultureName            
                            Name                        CultureCode
                            Description

    So what i want is to extend the Product class, so that when i do something like:            
    Product p = new Product();
    p.id = Guid.NewGuid();
    p.CultureID = Culture.CultureID;
    p.Name  = "Name";
    p.Description = "Description";

    I was thing like extending the Product.Generated Class as a new partial class and then put propertys there like:

    public Name
    { get { this.ProductLocales.Name;} set{this.this.ProductLocales.Name = value}}

    Now is this approach correct? could this accept all CRUD operations?
  6. Answer
    Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 19 Mar 2013 Link to this post

    Hi Nuno,

    You could use this approach only in one-to-one association and one-to-many when you are in the child i.e. when your navigation property is an object, not a collection.

    If you want to use this approach with a Web API service, you should take care of one more thing.
    By default when getting an object by Web API, it is detached from the context using the default fetch strategy which means that no associated objects are loaded and the extended properties will not be usable.

    To change this behavior, you should change the default fetch strategy used for loading an object by Web API.

    In order to implement this you could follow the next steps:
    1) Go to EntitiesModelRepository.cs class which is generated by Web API .
    2) Find the repository you are searching for - in your case ProductRepository
    3) Add a default constructor and initialize the inherited fetch strategy:
    1.public ProductRepository()
    2.{
    3.    this.fetchStrategy = new Telerik.OpenAccess.FetchOptimization.FetchStrategy();
    4.    this.fetchStrategy.LoadWith<Product>(product => product.ProductLocale);
    5.}

    For more information about the Fetch Strategy, take a look at this article.

    I hope that helps. Do not hesitate to contact us if you have other questions.
     
    Greetings,
    Boris Georgiev
    the Telerik team
    Free Webinar: OpenAccess Integration in Sitefinity. SIGN UP NOW.
Back to Top
DevCraft banner