1

I'm developing a client-server desktop application system in C#. The client is merely a thin client that displays whatever the server tells it to. The server communicates directly with a local database which is used for storing metadata for a variety of systems. Each system will have its own table, whose structure is unique for that system.

I'm trying to understand how I can tell the desktop application about the structure of the database without "hard-coding" anything, if that makes sense. I believe I need to look into Object-relational mapping but I'm not quite sure where to start, as I've never taken on a project of this complexity before. The idea would be that my server configuration somehow defines this mapping, and it would possibly be extensible so I can add more mappings for more systems in the future, or possible change the mapping.

How I see this system working is like this:

A user on the client uploads a file containing the metadata to the server. The server would determine for which system type this file is for, then parse the file to extract the metadata, and enter it into the database. The way I had initially envisioned this happening is that I would have a generic abstract class/interface which would need to be derived for each system type. The derived class would then implement a parsing function specific to that system type so it can extract the metadata from the file. But the problem here is that I have no way of entering the extracted data into the database, since the server doesn't know the underlying structure of the derived class.

Can anyone point me in the right direction? I've found NHibernate, which kind of sounds like what I might need, but I'm not 100% sure. Part of the problem is, as I stated earlier, I don't have a full understanding of some of these concepts. Can anyone recommend some online resources that might help me to understand Object Relational Mapper(ORM) in general, and specifically how it pertains to .Net Framework?

Greg
  • 11,302
  • 2
  • 48
  • 79
Reticulated Spline
  • 1,892
  • 1
  • 18
  • 20
  • "The client is merely a thin client that displays whatever the server tells it to."-Wat "I'm trying to understand how I can tell the desktop application about the structure of the database without "hard-coding" anything" wat, structure means you've hard coded something. "A user on the client uploads a file containing the metadata to the server. The server would determine for which system type this file is for, then parse the file to extract the metadata, and enter it into the database." that's called a controller(and data exchange format). – FlavorScape Jul 02 '13 at 20:32
  • if you don't want structure or hardcoded anything, just use a NoSQL database. If you want structure and ORM use Entity Framework – FlavorScape Jul 02 '13 at 20:33
  • since the server doesn't know the underlying structure of the derived class. -- sounds like you don't even need ORM. Do you actually have to have relationships in your data? – FlavorScape Jul 02 '13 at 22:11

2 Answers2

5

The ultimate question here isn't which Object Relational Mapper (ORM) to use, but ultimately what such a mapper does. This particular layer is often interlaced into a Data Access Layer (DAL).

You see the function here is essentially a technique to convert between incompatible types. Essentially it creates a Virtual Object Database that can be utilized. The importance of this layer is to link your Domain Logic / Business Layer to your Data Access Layer or Object Relational Mapper. This essentially helps decouple logic that doesn't require access to your data structure- Which makes the decoupling between your User Interface, Domain Logic, and Data Access Layer more coherent.

The Interface has no knowledge of how your Data Layer is speaking to your data structure. Currently I've been mixing the Data Access Layer and Object Relational Mapper due to the similarities but they are quite different.

How is a Data Access Layer different from an Object Relational Mapper

Compared to traditional techniques of exchange between an object-oriented language and a relational database, an Object Relational Mapper often reduces the amount of code that needs to be written. This benefit is apparent- but includes a massive pitfall. They tend to overly abstract what is occuring, which obscures what actually is happening.

Another thing to consider, if you overly rely on the ORM it may produce a poorly designed database.

Object Relational Mapper Theory

As you can see from the diagram, that is the theory behind an Object Relational Mapper. Where as a Data Access Layer often found within an N-Tier Architecture or Service Oriented Architecture.

Data Access Layer

The major difference between the two is:

Object-Relational Mapping tools provide data layers in this fashion, following the active record model. The ORM/active-record model is popular with web frameworks.

Where the Data Access Layer is layer which simplifies access to data stored in a persistent storage of some kind. Applications using a data access layer can be either database server dependent or independent. If the data access layer supports multiple database types, the application becomes able to use whatever databases the DAL can talk to. In either circumstance, having a data access layer provides a centralized location for all calls into the database, and thus makes it easier to port the application to other database systems (assuming that 100% of the database interaction is done in the DAL for a given application).

As you can now see the similarities and differences, but what about the data structure? Am I forced within a database? The short answer is no. You have some of these choices:

  • Relational Data: You would use a more traditional Database approach.
  • NoSQL: Object Relational Mappers* have arose that follow the NoSQL approach.

This will provide greater flexibility based upon the requirements of your project. A wonderful question on Stack Overflow actually has a real-world with a NoSQL approach. (here)

Four highly distinct benefits are:

  • Productivity: The data access code is usually a significant portion of a typical application, and the time needed to write that code can be a significant portion of the overall development schedule. When using an ORM tool, the amount of code is unlikely to be reduced—in fact, it might even go up—but the ORM tool generates 100% of the data access code automatically based on the data model you define, in mere moments.

  • Application design: A good ORM tool designed by very experienced software architects will implement effective design patterns that almost force you to use good programming practices in an application. This can help support a clean separation of concerns and independent development that allows parallel, simultaneous development of application layers.

  • Code Reuse: If you create a class library to generate a separate DLL for the ORM-generated data access code, you can easily reuse the data objects in a variety of applications. This way, each of the applications that use the class library need have no data access code at all.

  • Application Maintainability: All of the code generated by the ORM is presumably well-tested, so you usually don’t need to worry about testing it extensively. Obviously you need to make sure that the code does what you need, but a widely used ORM is likely to have code banged on by many developers at all skill levels. Over the long term, you can refactor the database schema or the model definition without affecting how the application uses the data objects.

Obviously take those with a grain of salt, because an application is tested doesn't mean it can't be test further or made better.

You can find a large list of Object Relational Mappers:

Those are just a few of the large quantity available, a link with a list. Some are great, some are not. The ultimate goal will be to find which one adheres closely to your desired application goal-

Hopefully that really helps you out.

Community
  • 1
  • 1
Greg
  • 11,302
  • 2
  • 48
  • 79
  • Thank you for the replies, but I'm still very confused. I think I lack a lot of the fundamental understanding that is required for this type of... abstract thinking, if you will. Can you recommend some resources where I can learn more about this? – Reticulated Spline Jul 03 '13 at 20:09
  • @ACS Yeah, you'll want to look into N-Tier, Entity Framework, Data Access Layer. Microsoft has a great article if you google C# Data Access Layer. – Greg Jul 03 '13 at 20:42
  • 1
    Forget all the algo blah blah and consider this: You have a class in C# called "User" (that's your table), the "User" class has a property called "Pseudo" (that's your column), now most of these ORM things just allow you to say: Hey! Take this DB Connection string of my database and map it to my class "User" ..tada ORM. In the end it simply allows you to connect to a database and use the tables/columns as simple C# objects without having to write any SQL. Or it allows you to simply create your C# model, and then tell it to create the database for you, and manage things with little to no SQL. – Robert Hoffmann Jul 04 '13 at 11:56
  • You can consider "Object Relational Mapping (ORM)" to be just another fancy name for "Database Abstraction Layer (DAL)" – Robert Hoffmann Jul 04 '13 at 11:58
  • @Robert You are 100% correct, you just managed to simplify the meaning compared to my answer. Though it still does have drawbacks; but that is a great addition and nice comment. – Greg Jul 05 '13 at 18:38
3

Check out OrmLite https://github.com/ServiceStack/ServiceStack.OrmLite

Syntax is very nice, actually a lot of things from service stack are nice.

They also list a bunch of alternatives here http://www.servicestack.net/benchmarks/

Robert Hoffmann
  • 2,366
  • 19
  • 29