Web API OData V4 Using a Singleton Part 7

This post is part 7 of the Web API and OData V4 series. This article demonstrates how to use an OData singleton with Web API. The singleton class has a list of child entities which is used to select contained entities from the SQLite database.

Part 1 Getting started with Web API and OData V4 Part 1.
Part 2 Web API and OData V4 Queries, Functions and Attribute Routing Part 2
Part 3 Web API and OData V4 CRUD and Actions Part 3
Part 4 Web API OData V4 Using enum with Functions and Entities Part 4
Part 5 Web API OData V4 Using Unity IoC, SQLite with EF6 and OData Model Aliasing Part 5
Part 6 Web API OData V4 Using Contained Models Part 6
Part 7 Web API OData V4 Using a Singleton Part 7
Part 8 Web API OData V4 Using an OData T4 generated client Part 8
Part 9 Web API OData V4 Caching Part 9
Part 10 Web API OData V4 Batching Part 10
Part 11 Web API OData V4 Keys, Composite Keys and Functions Part 11

Code: https://github.com/damienbod/WebAPIODataV4SQLite

The singleton entity is defined as follows:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.OData.Builder;

namespace WebAPIODataV4SQLite.DomainModel
{
    public class SkillLevels
    {
        [Key]
        public int Id { get; set; }

        [Contained]
        public List<SkillLevel> Levels { get; set; }
    }

    public class SkillLevel
    {
        [Key]
        public int Level { get; set; }

        public string Description { get; set; }

        [Contained]
        public virtual PlayerStats PlayerStats { get; set; }
    }
}

The SkillLevels will be the OData singleton. This entity has a list of contained SkillLevel entities. The SkillLevel entity has a Contained PlayerStats list which is selected from the database.

Now the OData singleton can be defined in the model:

SingletonConfiguration<SkillLevels> skillLevels = builder.Singleton<SkillLevels>("SkillLevels");
builder.EntityType<SkillLevel>();

The OData model can then be made accessible using an ODataController. The SkillLevelsController makes it possible to select the singleton or the child Levels or the PlayerStats pro level:

using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using WebAPIODataV4SQLite.DomainModel;

namespace WebAPIODataV4SQLite.Controllers
{

    public class SkillLevelsController : ODataController
    {
        private readonly SqliteContext _sqliteContext;

        public SkillLevelsController(SqliteContext sqliteContext)
        {
            _sqliteContext = sqliteContext;
        }

        [EnableQuery(PageSize = 20)]
        public IHttpActionResult Get()
        {
            return Ok(GetFixedSkillLevels());
        }

        [EnableQuery(PageSize = 20)]
        [ODataRoute("SkillLevels/Levels({key})")]
        public IHttpActionResult GetPlayerStats([FromODataUri] int key)
        {
            return Ok(GetFixedSkillLevels().Levels.FirstOrDefault(t => t.Level == key));
        }
        
        [EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
        [HttpGet]
        [ODataRoute("SkillLevels/Levels({key})/PlayerStats")]
        public IHttpActionResult GetPlayserStats([FromODataUri] int key)
        {
            return Ok(_sqliteContext.PlayerStatsEntities.Where(t => t.SkillLevel == key));
        }

        private SkillLevels GetFixedSkillLevels()
        {
            return new SkillLevels
            {
                Id =1,
                Levels = new List<SkillLevel>
                {
                    new SkillLevel {Description = "Legend", Level = 1},
                    new SkillLevel {Description = "Master", Level = 2},
                    new SkillLevel {Description = "Senior", Level = 3},
                    new SkillLevel {Description = "Intermediate", Level = 4},
                    new SkillLevel {Description = "Junior", Level = 5},
                    new SkillLevel {Description = "Novice", Level = 6}
                }
            };
        }
    }
}

Now the application can be tested:

http://localhost:59145/odata/SkillLevels
or
http://localhost:59145/odata/SkillLevels?$expand=Levels

odataSingleton_01

http://localhost:59145/odata/SkillLevels/Levels(2)

http://localhost:59145/odata/SkillLevels/Levels(2)/PlayerStats

odataSingleton_03

If you want to check or view the metadata, you can query as follows:

http://localhost:59145/odata/SkillLevels?$format=application/json;odata.metadata=full
http://localhost:59145/odata/SkillLevels/Levels(2)?$format=application/json;odata.metadata=full

Web API OData V4 makes it very easy to use or implement OData singleton models. This can be mapped, referenced like EntitySets or Collections of EntitySets.

Links:

http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/

http://blogs.msdn.com/b/odatateam/archive/2014/03/13/containment-is-coming-with-odata-v4.aspx

http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-22#OData

3 comments

  1. Hi Thanks for putting my site on your blog.

    1. No problem, thanks for your blog
      Greetings Damien

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.