question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Specify some types in the same container but not others, is this possible?

See original GitHub issue

Describe the bug I’ve recently finished running through the Pluralsight course on data modelling in cosmos db and I thought it might be a fun exercise to see if I could implement the final container/type construction using the repository tooling. But I am unsure if either I’m not doing it right or it’s not possible.

To Reproduce I started off simple and worked through the meta data into the same container. For those which don’t have access to the course it’s essentially

Tag:

{
 "id" :"",
 "name" : "",
 "type": "tag"
}

Category:

{
 "id" :"",
 "name" : "",
 "type": "category"
}

Container: ā€œmetaā€ Partition Key: ā€œ/typeā€

(not pasted an image from the course slide as unsure about copyright etc).

Reproduction code:

using Microsoft.Azure.CosmosRepository;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

const string databaseName = "datamodelling";

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddCosmosRepository(options =>
        {
            // do not do this; demo only to make obvious
            options.CosmosConnectionString = "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";

            options.DatabaseId = databaseName;
            options.ContainerPerItemType = false;

            options.ContainerBuilder.Configure<Category>(builder =>
            {
                builder.WithContainer("meta")
                    .WithPartitionKey("/type");
            });

            options.ContainerBuilder.Configure<Tag>(builder =>
            {
                builder.WithContainer("meta")
                    .WithPartitionKey("/type");
            });

        });
    })
    .Build();

var tagsRepository = host.Services.GetService<IRepository<Tag>>();
await tagsRepository.CreateAsync(new Tag() { Name = "production" });
await tagsRepository.CreateAsync(new Tag() { Name = "staging" });
await tagsRepository.CreateAsync(new Tag() { Name = "development" });

var categoryRepository = host.Services.GetService<IRepository<Category>>();
await categoryRepository.CreateAsync(new Category() { Name = "Laptops" });
await categoryRepository.CreateAsync(new Category() { Name = "Desktops" });
await categoryRepository.CreateAsync(new Category() { Name = "Workstations" });

var tags = await tagsRepository.GetAsync(t => true);

//[Container("meta")]
//[PartitionKeyPath("/type")]
public class Tag : Item
{
    public string Name { get; set; }

    protected override string GetPartitionKeyValue()
    {
        return base.Type;
    }
}

//[Container("meta")]
//[PartitionKeyPath("/type")]
public class Category : Item
{
    public string Name { get; set; }

    protected override string GetPartitionKeyValue()
    {
        return base.Type;
    }
}

As you can see from the above I have tried the attributes to define the container and partition keys. I have also tried the builder options to define container and partition key values. And also various combinations of them both, but no luck.

Expected behavior Both types go into the container named ā€œmetaā€

Actual behavior Both types go into the container named ā€œcontainerā€ which is the default container name in the package.

image

Environment summary SDK Version: latest (2.11.0) .NET Version: 6.0.0 OS Version (e.g. Windows, Linux, MacOSX): Windows 10

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mumby0168commented, Jan 16, 2022

Hi, since you are using the container builder, you need to set the container per item type property to true, this gives you full control over where each type is stored. Hope this helps, we need to add this to the docs really.

1reaction
mumby0168commented, Jan 16, 2022

Ah OK, will give it a go. The whole item per container set as true but trying to put more than one item in a single container confused me. Will let you know. Thanks 😊

Yes, I can see where you’re coming from, with that set to false it will put all item’s in the same container, using just the /id as the partition key, turning this to true is like saying, ā€œI am taking control nowā€ if that makes sense. You tell each IItem which container and what partition key to use.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I store objects of differing types in a C++ container?
Well, the first question would be: Why do you think you need to store objects of different, totally unrelated types in the same...
Read more >
concept name(s): container of different types which can be ...
The name of the concept you're looking for is a Union Type. Contrast this with an intersection type, which is where all elements...
Read more >
Generics: How They Work and Why They Are Important
The class in Listing 3 demonstrates how to create the same container that was previously created, but this time using a generic type...
Read more >
Documentation - Advanced Types
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
Kubernetes multi-container pods and ...
Inter-process communications (IPC) Containers in a Pod share the same IPC namespace, which means they can also communicate with each other ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found