I have used NHibernate to develop applications previously, so this will be my first time developing using mongodb and the c# driver.
In NHibernate it is possible to setup a configuration file to map the classes to tables. Doing this allowed me to map different concrete classes to different tables.
It was then possible to use the same c# code with a different configuration file in another application. Having specified a different mapping the objects would then be written and retrieved from different tables.
All the polymorphic mapping was isolated to the configuration thus a database management class could take an interface as a parameter (removing the burden from me to manage the INSERT commands)
With Mongodb I am struggling to find a similar solution. Most of the examples use hard coded names to retrieve the collection to allow read/ write.
Is there a solution within the mongodb c# driver that would allow me to isolate the mapping from the code ?
If I can not do the above are the some attributes I could placed in the concrete classes to divert the reading and writting to a different collection
Note: I need to avoid writting all the data in one collection
Related
Background:
I'm building a C# data migration tool to move data from an older application (with SQL Server database) to our new application (also using SQL Server database), but I am going through our Web API rather than direct inserts into the new database to reuse business logic and whatnot. I'm using Entity Framework for reading from the legacy database.
Issue:
The older database system, for reasons unknown to me, uses an archive table in addition to the table with the latest version of records. For example, there may be a "person" table and then also an "a_person" table with multiple archived copies of previous records. I am planning to keep these archived records within the same table, just chained together in a Point In Time architecture. So they are essentially identical columns, but because of EF6, these are two different objects which means I'm doubling all my code when I attempt to move values from "person" and "a_person" to the newest data object which will be sent to the API. If it was just the one example, no big deal, but there are about half a dozen tables that have this pattern.
I'm trying to think of the best way to handle this. I initially thought about added interfaces to the generated EF6 classes like semantic sugar to allow passing to a common method, but I still would need to cast that back to the original classes so it doesn't buy me anything.
Next I thought to serialize each of the tables into a json string that I can deserialize into a Dictionary - then have a generic method which would pull my values out. However, I feel like that may be unnecessarily slow.
Most recently I'm thinking about going more back to my original idea with interfaces, but partial classes to the EF6 that implements a common interface and an implementation that can return the different values of the parent EF6 class. So both "parent" and "a_parent" entities would have partial classes which implement an interface and which return all the values for the parent. Again, though, this just feels like a fancier way of duplicating my code of accessing the values.
Serializing and deserializing feels like the only way to truly eliminate the duplicate code. While the length of time the migration takes isn't a critical factor, I'd rather not create the most sluggish solution possible. I guess there's also Reflection. Would Reflection be preferred over the serializing and deserializing?
The solution I settled on and was quite happy with, was based on the comment from AlwaysLearning - I unified the two records.
I have been writing BLLs for whatever new tables I create in the database and use them in the Data access layer. I was wondering if someone knows if there is an inbuilt option using .NET for generating BLL classes for a table in sql server database.
It depends on what you want on your objects or the behavior you want to give to them.
You already have Entity Framework as a possibility, that already does something very similar to what you describe.
There is also LLBL Gen Pro (not free, but amazing) that probably does everything you want.
You can also make your own code generator tool combining a template engine (like T4), with queries against your master database, perhaps using MicroORM to simplify DB access.
I'm dynamically importing data into a database where I create new tables on the fly and store the metadata so that I can access those tables later via dynamically constructed SQL. My question is, for C#, is there a library out there that I can use that can abstract away some of the details of the SQL itself? The situation I'm running into is with sequences (although there are others). In Oracle accessing a sequence looks like this
select foo.nextVal from dual;
In Postgres...
select currval('foo_id_seq');
For my project I don't know what the final database will be and I don't like the idea of running through the project fixing a bunch of errors due to bad SQL.
I looked at NHibernate and it looks like tools like that (Linq to SQL) require an existing object model in place. I don't have an object model because all of my data is dynamically provided and I don't know the number of columns, data types, etc.
Any suggested approach to this problem is appreciated.
If the data you're trying to store has a dynamic structure, then it really sounds like a relational database may not be the best choice. It's strengths rely on data being statically structured and well defined. You might be better served with a document oriented store like MongoDB which is designed for dynamic schemas. If you used something like MongoDB, I think your question around abstracting query generation for dynamically changing schemas goes away.
That said, some relational databases like SQL Server have good support for XML data types which allow you to specify an arbitrary structure within your static schema. SQL Server also allows you to query directly into XML data types and even index them, which means you can query on the server side without the need for transferring the XML back to the client, deserializing, etc. To decide if this will perform well enough for your needs you'll have to test with data that will represent your production load.
I'm developing a .NET web service while trying to maintain a layered architecture that keeps my Models in one project and DB access (DAL) in a different one. The idea behind this is that if I have to change the DB technology, it's just a matter of creating a differnet DAL, while the rest of the application remains unchanged.
In the data-access layer that I'm developing, I am using Mongo DB C# Driver.
I've seen that:
Properties named "ID" will be mapped by the C# driver as the database's "_id" (Convention over configuration);
Int + Auto-increment in MongoDB is not a good idea;
Using Guid's as ID in MongoDB isn't a good idea either;
The recommended data type for the ID of documents stored in MongoDB is ObjectID. The C# driver provides a class to represent this;
However, if I use this data type (from MongoDB.Bson) in my Models, then they will become dependent on the MongoDB C# Driver and I don't want that: I want my models to be DB-independent; only my DALs can depend on whatever data access technologies I use.
So what data type should I use for my POCOs' IDs in order to have guarantee uniqueness in the DB? Would a string representation of a Guid be horrible in terms of performance?
Your feedback is welcome.
Good question.
From Experience, I can say that you're right: both GUIDs and auto-increment aren't the best idea (with GUID being a lot better than auto-increments), but not only for the reason mentioned in the SO question you linked to, but mostly because you need to be aware of the implications of monotonic vs. non-monotonic keys.
With the ObjectIds, I see three options:
Map between domain model and DAL. In the domain model, you could use the objectid's string representation. That's a bit annoying, but it forces you to separation of concerns.
Use your own data type and implement a type converter / mongodb serializer. I haven't tried that but I don't see why this wouldn't work.
Accept the MongoDB dependency. After all, if you really swap out your database, that will be a huge task. Different databases have very different characteristics and require very different data models. The whole "swap out the database" in a minute is bogus IMHO, it's never that easy and a database is a much leakier abstraction than anyone wants to admit. Trying to keep independent is a PITA. Anyway, doing a seek-and-destroy on the word ObjectId will be less than 1% of the other work.
I am writing an application in C# that takes data from approximately 100 unique file sources and maps it into existing objects for analysis and storage to a database. These files are typically CSV or TAB. I'm currently using the Lumenworks library for parsing the actual CSV, so I already have the ability to reference fields by name.
Many of these fields are direct 1:1 mapping, but some items need to either be split, concatenated, or otherwise converted.
Rather than use a factory with a concrete class for each source, I see an opportunity to make all of this configurable using a library of common methods for mapping individual fields, reserving the custom datasource specific coding for some of the edge-case mapping.
I'm thinking that I could put an front end on this that would allow a couple of our admins to creating mappings for new datasources on-the-fly.
What would be a good design to consider for this type of task?