0

C# Project for a bakery chain to log every make including ingredient and actual recipe, etc. Seems a bit too much free-form for someone like me done lots of SQL. MongoDB came to my mind. Started the typical Type, database interface and Repository class, I then realized I'm so used to map out everything into a Type class to define each property/column. Reason I've chosen MongoDB is it can't be determined exactly how many columns. Sometimes they wrote in a certain order and items to the books, other times they'd just wrote totally free-form, random. It's impossible to pre-define them in Type. Of course, workaround is to make up 20 properties/columns as they'll never use this much. I know MongoDB also has schema. But, this way simply goes back to the usual fixed-schema SQL.

If MongoDB is still the database choice, C# might have the following options for Type:

  1. Capture everything into a big string (make and recipe)
Bakery{
int UserId, 
string UserName,
Datetime makeTime,
string make,
string recipe
}
  1. A bit more organized, drawback is it can't handle non-pair collection, for example horizontally 3+ fields.
Bakery{
    int UserId, 
    string UserName,
    Datetime makeTime,
    iCollection<make> makes,
    iCollection<recipe> recipes
    }

public class make
{
string fieldName,
string actualValue
}
public class recipe
{
string ingredientName,
string dose
}
  1. pre-define up to 20 fields per row/document, I'd rather go back to SQL.
Bakery{
    int UserId, 
    string UserName,
    Datetime makeTime,
    iCollection<make> makes,
    iCollection<recipe> recipes
    }

public class make
{
string fieldName1, string actualValue1,
...
string fieldName20, string actualValue20
}
public class recipe
{
string ingredientName1, string dose1,
...
string ingredientName20, string dose20
}

Must be able to query an item and value if exists. Need some expert advice please. Thank you.

4
  • As far as I understand the mongoDb stuff you use your third part, but instead of using fieldname1 - fieldname20 just use another List. Because it's saved much like JSON-Objects you are not bound to 2 dimensional objects in row and column scheme. If your class make consists of a List<Field> where each Field consists of field and value member, you can store a flexible amount of fields to your database. Commented Feb 24, 2017 at 7:25
  • @Sebi Good point. The key is to make it vertical instead of horizontal. How do I handle something related but non-pair? For example, recipe, dose, and temperature. Commented Feb 25, 2017 at 5:56
  • I would think about it completly object oriented. Build up a nice C# Object-Structure. Later you serialize this to mongodb. But this is just my point of view, I never tried this. To keep a relation you need to store the Id-Field of Mongo-Documents I think. docs.mongodb.com/ecosystem/drivers/csharp Commented Feb 25, 2017 at 12:02
  • @Sebi They are so used to free form, human gets it from context, never get confused because they are distinctive, range of numbers never overlap. For example, they write: brand A, sugar, 2 (implies spoon), 300 (implies temperature), 20 (implies minutes). Commented Feb 25, 2017 at 18:31

1 Answer 1

1

You can simply make your makes and receipts as Dictionary<string,string>.

Please check this anwser as an example.

Sign up to request clarification or add additional context in comments.

6 Comments

It's a 2013 post. Do you know if List would be supported now? Reason being is they are so used to free form, human gets it from context, never get confused because they are distinctive, range of numbers never overlap. For example, they write: brand A, sugar, 2 (implies spoon), 300 (implies temperature), 20 (implies minutes). You can tell they don't have keys. Because "sugar" already serves as a key for this whole document. Require unit/dose/label for fields after "sugar" adds overhead for data entry.
Even though it is 2013 post, it doesn't make it wrong or invalid. List is supported. You can always make the property a list of string to achieve this.
Got Type of ExtraElements member must be BsonDocument or implement IDictionary<string, object> when change it to List from Dictionary.
If you are using list, you should not use BsonExtraElements annotation. Remove that.
How about a hybrid? that is a Dictionary<string, object> then populate the object as List<string> or Array[] in order to preserve order.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.