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.

EnableReferences use case

See original GitHub issue

I’m trying to understand the usage of the EnableReferences. I’ve created this sample to wrap my head around the concept and I’m getting an unexpected result.

Basically, I have a BookStore with Books (with all the books) and Genres that have references to the books.

Expected

The resulting XML of the Genres section should hold references to each book.

Actual result

The resulting XML holds references to a list of Books instead.

How to reproduce

This is the offending code:

void Main()
{

	string OutputXml = System.IO.Path.GetTempFileName() + ".xml";

	var b1 = new Book();
	var b2 = new Book();
	
	var allBooks = new[] { b1, b2, };

	var fs = new BookStore()
	{
		AllBooks = allBooks,
		Genres = new[]
		{
			new Genre()
			{
				Books = allBooks,
			},
			new Genre()
			{
				Books = allBooks,
			},
		}
	};

	var serializer = new ConfigurationContainer()
		.Type<Book>(b => b.EnableReferences())
		.Create();

	var serialized = serializer.Serialize(new XmlWriterSettings() { Indent = true }, fs);

	System.IO.File.WriteAllText(OutputXml, serialized);

	var psi = new ProcessStartInfo(OutputXml)
	{
		UseShellExecute = true,
	};
	Process.Start(psi);
}

class BookStore
{
	public IEnumerable<Book> AllBooks { get; set; }
	public IEnumerable<Genre> Genres { get; set; }
}

class Book
{	
}

class Genre
{
	public IEnumerable<Book> Books { get; set; }
}

As you see, both genres have a reference to allBooks. The serializer identifies that I want to reflect this in the XML, but I would like to have the reference to the actual books inside allBook.

NOTE

In order to get the desired results I would have to use the following code:

void Main()
{

	string OutputXml = System.IO.Path.GetTempFileName() + ".xml";

	var b1 = new Book();
	var b2 = new Book();
	
	var allBooks = new[] { b1, b2, };

	var fs = new BookStore()
	{
		AllBooks = allBooks,
		Genres = new[]
		{
			new Genre()
			{
				Books = new[] { b1, b2 },
			},
			new Genre()
			{
				Books = new[] { b1, b2 },
			},
		}
	};

	var serializer = new ConfigurationContainer()
		.Type<Book>(b => b.EnableReferences())
		.Create();

	var serialized = serializer.Serialize(new XmlWriterSettings() { Indent = true }, fs);

	System.IO.File.WriteAllText(OutputXml, serialized);

	var psi = new ProcessStartInfo(OutputXml)
	{
		UseShellExecute = true,
	};
	Process.Start(psi);
}

Once the Books are assigned to different references (2 different arrays), the correct XML is produced.

I’ve configured the serialized calling the config.Type<Book>(b => b.EnableReferences()) because I’ve expected this would cause the Book to be reference from another places in the XML, but I think I’ve misunderstood its usage.

Is this a bug or I’m failing to tell the serializer what I want 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
Mike-E-angelocommented, Jul 22, 2020

DOH @SuperJMN I apologize for the confusion here. You are giving me quite the memory workout! 😅 It turns out there are two ways of enabling references:

EnableReferences() : Enables references on the entire container. Type<T>().EnableReferences(x => x.Id) : Enables references specifically for the type, with the provided member being the identity member.

Unfortunately, despite my best wishes, there is no Type<T>().EnableReferences(). What you are actually doing is calling it on the Type’s parent ConfigurationContainer thereby enabling it for the entire container. This explains why both Book and Book[] are being counted.

The fluent api suffers from some design issues like the above. You think you’re applying it to the type, but you’re really accessing the parent container. It was my first attempt at a fluent API, what can I say. 😁

2reactions
Mike-E-angelocommented, Jul 21, 2020

“Super” happy to report that this fixes #360, too! 🎉

😁

Read more comments on GitHub >

github_iconTop Results From Across the Web

When do I use references? - c++
Use references in case you want to pass a large size argument to a function or method or in case you want to...
Read more >
Common Use Cases - Visual Studio Live Share
The primary goal of Visual Studio Live Share is to enable developers to collaborate with each other more easily, without introducing any ...
Read more >
c++ - What are R-value references used for?
A common use case is the “move constructor,” invoked when an object is being copied from a temporary that's about to expire (a...
Read more >
How To Use Mendeley Reference Manager ... - YouTube
I'll show you everything you need to know including how to add references to Microsoft Word. DOWNLOAD LINKS Mendeley Reference Manager: ...
Read more >
When would you want two references to the same object?
A less common case when you use one of two objects depending on a ... indexByName.add(t.name, t); process(indexByName["some name"]);.
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