A collection with two keys and an index

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books. So for that, I have a C# class BookCollection that will:



  • Add, delete, update, and read a Book.

  • Maintain a collection of Books with unique Id, unique Title and unique Rank also:

    • Unique Id even after serialize/deserialize. For that We have a property NextId and a Dictionary<int, Book>

    • Unique Title that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have the HashSet<string>


    • Rank is kind of order of preference for each Book. For example, We can have book1, book2, and book3 with an order of preference (Rank) of 3, 1, and 2. Which means, Rank should be unique and between [1..NumberOfBooks]. Also When changing a Rank for one book, We need to update the Rank for all other books accordingly. In the title, I said index, because to me Rank is the same as an array index.


Here is my code:



public class Book

public int Id set; get;
public string Title set; get;
public int Rank set; get;


public class BookCollection

public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;

public BookCollection()

m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);


public bool Add(Book item)

item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public bool Remove(Book item)

bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;


public bool Update(Book item)

if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public IEnumerable<Book> GetAllBooks()

return m_Dictionary.Values;


public bool Contains(Book item)

return m_Dictionary.ContainsKey(item.Id);


private void updateRanks(int rank, bool increment)

foreach (var book in m_Dictionary.Values)

if (rank <= book.Rank)

book.Rank = increment ? book.Rank + 1 : book.Rank - 1;






I want to build a collection that could handle all CRUD operations in the most performant way.



Any suggestion How to make this collection better?







share|improve this question



















  • Update is more like Insert. Cannot change rank or title?
    – paparazzo
    May 12 at 15:56










  • @paparazzo We could change every Books attribute except Id. So changing the Rank is like insert in a List
    – Mhd
    May 12 at 20:47










  • Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
    – paparazzo
    May 13 at 13:59
















up vote
0
down vote

favorite












I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books. So for that, I have a C# class BookCollection that will:



  • Add, delete, update, and read a Book.

  • Maintain a collection of Books with unique Id, unique Title and unique Rank also:

    • Unique Id even after serialize/deserialize. For that We have a property NextId and a Dictionary<int, Book>

    • Unique Title that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have the HashSet<string>


    • Rank is kind of order of preference for each Book. For example, We can have book1, book2, and book3 with an order of preference (Rank) of 3, 1, and 2. Which means, Rank should be unique and between [1..NumberOfBooks]. Also When changing a Rank for one book, We need to update the Rank for all other books accordingly. In the title, I said index, because to me Rank is the same as an array index.


Here is my code:



public class Book

public int Id set; get;
public string Title set; get;
public int Rank set; get;


public class BookCollection

public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;

public BookCollection()

m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);


public bool Add(Book item)

item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public bool Remove(Book item)

bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;


public bool Update(Book item)

if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public IEnumerable<Book> GetAllBooks()

return m_Dictionary.Values;


public bool Contains(Book item)

return m_Dictionary.ContainsKey(item.Id);


private void updateRanks(int rank, bool increment)

foreach (var book in m_Dictionary.Values)

if (rank <= book.Rank)

book.Rank = increment ? book.Rank + 1 : book.Rank - 1;






I want to build a collection that could handle all CRUD operations in the most performant way.



Any suggestion How to make this collection better?







share|improve this question



















  • Update is more like Insert. Cannot change rank or title?
    – paparazzo
    May 12 at 15:56










  • @paparazzo We could change every Books attribute except Id. So changing the Rank is like insert in a List
    – Mhd
    May 12 at 20:47










  • Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
    – paparazzo
    May 13 at 13:59












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books. So for that, I have a C# class BookCollection that will:



  • Add, delete, update, and read a Book.

  • Maintain a collection of Books with unique Id, unique Title and unique Rank also:

    • Unique Id even after serialize/deserialize. For that We have a property NextId and a Dictionary<int, Book>

    • Unique Title that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have the HashSet<string>


    • Rank is kind of order of preference for each Book. For example, We can have book1, book2, and book3 with an order of preference (Rank) of 3, 1, and 2. Which means, Rank should be unique and between [1..NumberOfBooks]. Also When changing a Rank for one book, We need to update the Rank for all other books accordingly. In the title, I said index, because to me Rank is the same as an array index.


Here is my code:



public class Book

public int Id set; get;
public string Title set; get;
public int Rank set; get;


public class BookCollection

public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;

public BookCollection()

m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);


public bool Add(Book item)

item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public bool Remove(Book item)

bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;


public bool Update(Book item)

if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public IEnumerable<Book> GetAllBooks()

return m_Dictionary.Values;


public bool Contains(Book item)

return m_Dictionary.ContainsKey(item.Id);


private void updateRanks(int rank, bool increment)

foreach (var book in m_Dictionary.Values)

if (rank <= book.Rank)

book.Rank = increment ? book.Rank + 1 : book.Rank - 1;






I want to build a collection that could handle all CRUD operations in the most performant way.



Any suggestion How to make this collection better?







share|improve this question











I'm working on WEB API project and I just start coding the first CRUD operations. So far, I'm using a JSON file as a "database" to serialize/deserialize a list of Books. So for that, I have a C# class BookCollection that will:



  • Add, delete, update, and read a Book.

  • Maintain a collection of Books with unique Id, unique Title and unique Rank also:

    • Unique Id even after serialize/deserialize. For that We have a property NextId and a Dictionary<int, Book>

    • Unique Title that could be changed later on (that's Why We can't rely on it as a "real" ID). For that We have the HashSet<string>


    • Rank is kind of order of preference for each Book. For example, We can have book1, book2, and book3 with an order of preference (Rank) of 3, 1, and 2. Which means, Rank should be unique and between [1..NumberOfBooks]. Also When changing a Rank for one book, We need to update the Rank for all other books accordingly. In the title, I said index, because to me Rank is the same as an array index.


Here is my code:



public class Book

public int Id set; get;
public string Title set; get;
public int Rank set; get;


public class BookCollection

public int NextId set; get;
private readonly IDictionary<int, Book> m_Dictionary;
private readonly ISet<string> m_HashSet;

public BookCollection()

m_Dictionary = new Dictionary<int, Book>();
m_HashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);


public bool Add(Book item)

item.Id = NextId++;
if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_Dictionary.Add(item.Id, item);
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public bool Remove(Book item)

bool found = m_Dictionary.ContainsKey(item.Id);
if (!found) return false;
m_Dictionary.Remove(item.Id);
m_HashSet.Remove(item.Title);
updateRanks(item.Rank, false);
return true;


public bool Update(Book item)

if (m_Dictionary.ContainsKey(item.Id)) return false;
if (m_HashSet.Contains(item.Title)) return false;
m_HashSet.Remove(m_Dictionary[item.Id].Title);
m_Dictionary[item.Id] = item;
m_HashSet.Add(item.Title);
updateRanks(item.Rank, true);
return true;


public IEnumerable<Book> GetAllBooks()

return m_Dictionary.Values;


public bool Contains(Book item)

return m_Dictionary.ContainsKey(item.Id);


private void updateRanks(int rank, bool increment)

foreach (var book in m_Dictionary.Values)

if (rank <= book.Rank)

book.Rank = increment ? book.Rank + 1 : book.Rank - 1;






I want to build a collection that could handle all CRUD operations in the most performant way.



Any suggestion How to make this collection better?









share|improve this question










share|improve this question




share|improve this question









asked May 12 at 14:49









Mhd

1364




1364











  • Update is more like Insert. Cannot change rank or title?
    – paparazzo
    May 12 at 15:56










  • @paparazzo We could change every Books attribute except Id. So changing the Rank is like insert in a List
    – Mhd
    May 12 at 20:47










  • Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
    – paparazzo
    May 13 at 13:59
















  • Update is more like Insert. Cannot change rank or title?
    – paparazzo
    May 12 at 15:56










  • @paparazzo We could change every Books attribute except Id. So changing the Rank is like insert in a List
    – Mhd
    May 12 at 20:47










  • Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
    – paparazzo
    May 13 at 13:59















Update is more like Insert. Cannot change rank or title?
– paparazzo
May 12 at 15:56




Update is more like Insert. Cannot change rank or title?
– paparazzo
May 12 at 15:56












@paparazzo We could change every Books attribute except Id. So changing the Rank is like insert in a List
– Mhd
May 12 at 20:47




@paparazzo We could change every Books attribute except Id. So changing the Rank is like insert in a List
– Mhd
May 12 at 20:47












Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
– paparazzo
May 13 at 13:59




Actually can change ID as it has a set. If you change Rank directly it is not guaranteed to be unique.
– paparazzo
May 13 at 13:59















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194263%2fa-collection-with-two-keys-and-an-index%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194263%2fa-collection-with-two-keys-and-an-index%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?