I'd like to learn database applications in C# and I'm about to select some framework. I heard many recommendations of NHibernate, however I haven't decided yet.
Anyway, I'd like to know if there's any real-life example (with sources) of NHibernate in C#, to learn best practices etc.? I know all of them are probably covered in the docs, but working example helps a lot understanding the proper development pattern.
Check out Ayende#Rahien, there's a lot of blog entries on NHibernate there. Another good source is NHibernate Forge. To get you started this would be especially helpful: NHibernate Getting Started Guide. Finally here you can find a downloadable working example of a NHibernate project based on the Northwind database.
The open source project S#arp Architecture is so far the best example I'm aware of for NHibernate:
S#arp Architecture
Even if it feels sometimes a little bit overdesigned, it follows best practices in implementing NHibernate in a multi-tier architecture.
I personally learned a lot about ORMs and good loosely coupled architecture going through
the code of the project.
Another worthwhile article about NHibernate can be found on CodeProject:
NHibernate Best Practices
Real life example: Orchard uses NHibernate, Fluent NHibernate and Linq To NHibernate for data access. It is an opensource CMS build by Microsofties. Source code is available at CodePlex.
NHibernate is a very solid ORM which allows you to create POCO classes, keeping your domain model very clean and allowing easier testability.
Entity Framework is improving rapidly. The UI tools in EF4 are okay for small applications but it is difficult to manage that kind of development with version control, rapidly changing DB schema etc. With EF4 Code First approach (in CTP stage at the moment) EF4 will will be much more attractive.
I have used LLBLGen in past with great success.
For beginners, Webmatrix can get you started very quickly. Check Rob Connery's video on how to use it with testing framework here
As others have said, NHibernate is a solid framework to work with. It's quite mature and has a lot of features that let you control exactly what will happen to your date.
That said if you're just starting out with database programming you may also want to consider LinqToSql. It's not nearly as powerful as NHibernate, and it has been more or less EOLed. However, what you give up in power you gain in simplicity. It will allow you to get a basic database application up and running probably within minutes and allow you to experiment as you first learn. Despite the simplicity of getting your first app up and running it's still powerful enough to be used to build something like Stack Overflow.
Some good Linq to Sql beginner resources:
NerdDinner
ScottGu's series: Using LINQ to SQL (link is to part 9 because that has links to 1 through 8 at the top). Although this one doesn't have an application download (best I can tell) it basically walks you through building an app.
If you have your heart set on learning NHibernate, you can also start with the NerdDinner tutorial linked above and take a look at Ayende's writeup on porting it to NHibernate as well as a series on mapping NerdDinner with NHibernate. There's also a series of articles on using NHibernate in a WPF application (most other tutorials focus on web apps).
Resources:
Ayende's Reviewing NerdDinner and Reviewing NerdDinner: The Select N+1 pitfall
NerdDinner with Fluent NHibernate Part 1 - The domain model
NHibernate and WPF (again, links to part 4 that has link to parts 1 - 3).
One thing I can't emphasize enough is once you get past the initial "cool...I can talk to the DB" phase, grab a trial license for an appropriate profiler (list of profilers at the top) for your platform. It will let you see what exactly your ORM is sending to the DB. It can be very valuable for learning how what you're doing gets translated into SQL as well as troubleshooting bugs you run into.
Regardless of which path you choose you should also take a look at Data Access Practices Using Microsoft .Net: A Nerdly Comparison and Data Development GPS: Guidance for Choosing the Right Data Access Technology for Your Application Today. Although those only cover Microsoft technologies, it'll give you a high level overview of what's available out there and when it may and may not be appropriate for your projects (in the future).
Take a look at NHibernate addins project http://code.google.com/p/unhaddins/
Though this is not real life production example, the sample applications there are pretty close to real life applications | scenarios. The examples there are written by well known and respected programmers and they show some serious OOP and design patterns skills. This is definitely not for beginners.
Related
I am looking Sharp Architecture alternative which use Entity Framework code first as ORM. Is there any mature project like Sharp Architecture with EF code first ?
Architecture is a blueprint. Once you see Sharp Architecture you should be able to simply think about it and change it to work with EF. If you are not able to do that you probably don't have enough skills with required APIs - that is th first thing you have to change before start dealing with architecture. No architecture will save you from understanding APIs and without understanding them you cannot do a good architecture correctly using features of your APIs.
Architecture should be driven by needs of your application. The approach where you want to bend needs of your application to fulfill some architecture blueprint is terrible wrong. First you have to define what should your architecture solve for you and after that you can ask if there is any blueprint already doing it (and nothing more).
Most of articles and sample architectures are just pushing a lot of patterns without actual need for them. Number of patterns and layers don't make a good architecture. In most cases it just makes the system overachitected and hard to maintain. These samples are mostly for explaining how to implement some patterns.
Bad news there is no exact replacement, Good news its easy to create something your own like I did.
I have been using S#arp for a long time and it is a good Architecture, I developed lots of applications with it but it looks like there is no more movement with that open source project so late last year I decided to move on an recreate something similar using the same principles but with Entity Framework.
Key components such as NHibernate, NHibernate.Validator and Castle Windsor was replaced to use Entity Framework, LINQ to Entites, System.ComponentModel.DataAnnotations and Autofac respectively. The layers remained the same like the Domain, Presentation, Task, Framework and Infrastructure.
On how I created it please have a look at this post on its detailed explanation.
http://www.macaalay.com/2015/10/20/creating-a-solid-architectural-foundation-from-scratch-is-not-really-that-hard/
I also created a code generator which I called Effinator, which generated CRUD operations and basic UI from your database design
so far I've never dealed with serious DB programming in "native" languages (I'm using the "native" word here as opposition to web-based languages like PHP, thus I assume C# is one of those "native" languages. Probably I should use different word, but I don't know any).
Thus I'm looking for a good example of DB application in C#.
This example should show how to:
add, remove, edit records
list records
handle relations
most important for me is to learn the proper way of doing the above things, in example - avoiding loading the whole table to memory (hey, it might have 500 000 records [or more ;) ])
A good example of what I'm looking for is P4A demo - products catalog.
This however is written in PHP ;) http://p4a.crealabsfoundation.org/demo/
First, good question because I don't know of any really good DeskTop/Db sample applications.
You will have to pick a technology first, C# is just the language. You've only indicated Desktop, that leaves you with a lot of choices:
WinForms + DataSets (classic 2003 vintage)
WinForms + DataReaders (more lowlevel)
WinForms + Entity Framework
WPF + DataSets
WPF + Entity Framework
WPF + MVVM + Entity Framework
SilverLight + Entity Framework
SilverLight + Data Services
More combinations are possible, and there are other (3rd party) ORMs such as NHibernate.
The WinForms+Datasets/DataReaders is the oldest and simplest tech but not very OOP. You will find a lot of small samples and videos on WindowsClient.NET. Don't overlook the VisualBasic samples.
I will call it very good for small applications, less suitable for larger ones.
The Entity Framework (EF) is newer, lots of (recent) blogging about details. I found one small but rather complete sample on MSDN (but I couldn't find an accompanying article). Do take a look if you consider an ORM.
Very good for larger applications as it supports better layering and (unit-)testing.
There are various examples in C#/.Net on codeplex (take for example MVC Music Store if you want to go with MVC and EF) and many other websites.
The examples you're looking for depend on what application you're building. You can go for Linq2SQL or EntityFramework (Linq2Entities) when working with the databases, if you're not very familiar with SQL (though it helps - a lot), or the classes in System.Data.SqlClient for example.
Either option allows you to do the operations you mentioned above.
Regards...
My company is interested in porting a large business application to .NET. We plan on developing a desktop version and a silverlight version. I mostly researched the CSLA framework (got rocky's book, halfway through already) and found it a bit over-engineered, the data layer side didn't seem so polished either.
Is there any other frameworks that claim to do what CSLA is doing? I'm not talking about ORM tools (e.g L2S, EF, NHibernate.) I'm interested in a framework that supports business rules, easy n-tier architecture, objects are domain driven and not database driven, security on the business objects etc...
I know I can find small frameworks that will do some of the work required (Enterprise Application Block comes to mind) but I'm looking for one that has everything included.
I would be interested in hearing more about why you think CSLA is over-engineered. I have found it to be very feature rich but most of the features just implement standard .NET framework interfaces and so all the plumbing comes free and you definately have to use it.
Your requirements seem to be a great fit for CSLA. Other frameworks (such as ORMs) contain validation/business rules but the major issue is that you are (in most cases) stuck with your data schema. This leads to objects that are not friendly for UI development and force you to know the intricacies of your database.
Here's a good blog post (archived version) courtesy of "Adam on the Net" discussing and comparing the following:
Castle Project
Spring.NET
Enterprise Library
CSLA
If I were you I would either pick Spring.NET or just start building your own framework around ASP.NET MVC and Fluent NHibernate. Then slowly add your own building blocks as and when you need them. Enterprise library blocks are good but heavy according to me and have lot of things that you may not really need.
We are looking into using an ORM and I wanted some opinions/comparisons
The basic criteria we have for an ORM is: Easy to use/configure(short learning curve), flexible, the ability to abstract it away, easy to maintain
Here is a list of what ORM we are looking at and what our initial impressions are
Open Access - seems really easy for simple stuff, but doesn't seem to have a lot of flexibility, cost isn't an issue we already own it
Ling to SQL - looks very simple to use and configure but is missing some functionality
Active Record - NHibernate made simple
SubSonic - looks very feature rich, but haven't really played with it much
here are the ORMs we have looked at and ruled out
Entity is still in beta
NHibernate has far to much of a learning curve (we don't have 3 weeks to delicate to learning it)
I'd say you should take a look at DataObjects.NET (http://www.x-tensive.com). It's feature rich and pretty easy to use. It does, though, absolutely tie you to your object model, as it decides what the database structure should be based on what your object model looks like. That being said, if you want to be able to disregard the existence of the database, it's quite nice. We've used it for years and have had great success.
We currently use SubSonic (2.0.3) and it has been an absolute lifesaver. I cannot stress enough how awesome it is. HOWEVER, we are now looking at switching away from it for various reasons (probably to NHibernate or Entity). Here are my Pros and Cons of it:
Pros:
Very simple to setup and use.
Lots of great & useful, tools and features
Uses the "convention over configuration" philosophy, so very little configuration. It "just works". (As long as you do things the way it wants... :) )
Cons:
Your database design is very tightly coupled to your domain design. Make a change in your DB, and you need to change your code/domain design.
By default, SubSonic uses the ActiveRecord pattern for all data access instead of the Repository pattern, which makes it more difficult to "abstract it away". (Although I believe with v3.0 that you can swap out the default ActiveRecord templates to use the Repository pattern).
Lots of pessimistic rumours flying around about the future of SubSonic. But rumours are just that: Rumours.
For all the paired pennies it may be worth:
If you don't have 3 weeks now to learn your ORM of choice (whichever you choose), you may have to find 3 weeks to learn it later when it doesn't map something exactly how you thought it would.
If you have a model that's moderately complicated, ORM is non-trivial. You'll wind up needing to know how your ORM works so you can tell it to map things the way you want.
Which is all another way of saying "Know thy tools", of course. :)
Most folks will have a smattering of experience with one or two of those, but few will have exposure to all. I recommend a proof-of-concept effort with each of your favorites. Get each one set up, spending no more than n hours per ORM tool (n = however much time you decide is reasonable.) You don't have to implement your entire object model, a functional subset will do.
By the time you're done, you will have worked through the setup and some usage of all of them. You can then write up a post-mortem and the team can decide which has the best pain-to-feature ratio.
Use T4 templates to create your own. There are several established patterns available on the internet especially for T4 templates.
Knowledge of T4 also will allow you to script out items that might have a large scope than macros, but a smaller scope than writing a custom app to generate the script you need.
Hope this helps!
I very much agree with BryCoBat (upvoted). I wanted to also add that if you already own Open Access, then the company very likely has people somewhere internally who are already very comfortable with it, including code examples you look at in your own domain for both trivial and non-trivial tasks. In other words: use what you know.
If you're not using Telerik controls, LINQ to SQL should be the one to select for fast learning - there is huge amount of different tutorials, videos, books in the web.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I'm shopping for an ORM tool. I'm agonizing over the purchase of either CodeSmith (which is currently available at a substantial discount) versus an ORM tool.
LINQ to SQL is off my list; SubSonic 2.x is off the list (I don't want to invest in that dead end knowing that SubSonic 3.0 is coming. NHibernate seems like overkill as does LLBLGEN. I've only briefly evaluated EF but don't get quickly get a warm and fuzzy feeling from it.
Am I crazy thinking that CodeSmith is a rational alternative to off-the-shelf ORMs? Will CodeSmith pay for itself in other ways?
Please note that I am in no way related to any vendors and this isn't a cheap shot SO question just for the sake of generating product noise! I am looking for honest advice and opinions about CodeSmith as an ORM tool (with its provided, or community available) templates.
In fact, hibernate is a good ORM tool. But it stops there!
Code smith capabilities can be more than just a relational mapping staff! I use code smith to generate some UI forms, business layers (templates), data access layers, patterns, and so on.
But to work with code smith, you may need good experience with system design or use their templates which I don’t like to use but I like as an example.
Code smith approach has one special drawback; you have to design your system considering the database implementation first. Nowadays, in object analysis approach, people success in implementing business logic & entities just before any database implementation – they forget about this.
Decision is hard; I've constantly read important names such as Scott W. Ambler, Kent beck, Robert C. Martin and people from The Pragmatic Programmers series which recommends ORM Tool to speed up development. They said that ORM Tool developers are concerned with all database issues (pooling, connections, database vendor specifics, etc). So when we have to design data access layers we have to consider all these aspects too.
I believe that these ORM tools come along with an overburden. I don't know yet how these tools would behaviour in low budget projects (I mean not good hosting servers or any kind of shared resources).
I’ve seen inexperienced developers not taking this into account as they try to evangelise their beloved tools. But in java projects, hibernate is already a widespread and well-known tool. I have no doubt the great projects has been delivered using this technology but I have seen anyone and again java developers may need to teach us (.net developers) how to build great solutions. (Sorry, we have to admit.)
The only thing I would recommend is to consider your context. Are you doing a new system? You need work in pattern? Have you ever try to consider such code generator and ORM tools altogether?
I do prefer code smith because I generate entire solutions at once, not just data access layer. Code generation is very important and it is not for less that Microsoft has imitated code smith approach in visual studio.net 2008 and so on.
Good luck
Code Smith is not an ORM, it's just a code generator IDE.
You can generate a DAL using code smith based off your database but that would defeat (one of) the purposes of using an ORM which is basically that it generates the DAL dynamically so you don't have to write the code.
If you're really trying to compare the two, then maybe you might get some benefit using code smith because you would have absolute control over the code that gets generated, but I'm not sure that benefit would outweight the drawback of what could turn out to be spending months to write the code smith templates to generate a DAL based off your database.
And then you have to considering what happens when you make a change to your database, you will most likely have to run code smith and build everytime you do so. A good ORM will allow you to configure your database changes in schema, and then it will generate dynamically so you don't have to worry (as much) about making changes to the database.
NHibernate is the way to go. It is an enterprise-grade ORM. And with the convention-based auto-configuration from the FluentNHibernate library, configuration is ridiculously simple if you stick to a single convention (you get to specify the conventions, or there are defaults).
With NHibernate, your domain objects are pure C# objects. No weird base classes. No codegenned files that need to get refreshed every time you decide to make a change.
I use netTiers daily and love it. Documentation for it sucks, but it has been such a time saver for me at the office. A lot of people gripe about the amount of code it produces, but from what I've seen, the code it creates is a lot faster than hand coding much of it. It also generates all the sprocs you will need for your basic CRUD stuff. The access methods it produces are pretty awesome as well, getting by all your unique keys, foreign keys and primary keys.
I think the usage of Codesmith is viable. But you should look into frameworks that use it. Net Tiers is an application framework that can be built upon to knock out a good DAL.
Why would something like LLblgen be overkill ? We use it at work, and after a somewhat steep learning curve, it's very nice :). You should at least give that and nhibernate a try.
Not sure what this has to do with codesmith though, thats a codegenerator, you could roll your own ORM, but since there are already good ones out there, I dont think it's worth the time and effort unless you want to do it for learning purposes.
Go with netTiers and .netMVP. NetTiers is a very nice DAL and .netMVP is a framework I just released to support abstraction at the UI level. :)
http://community.codesmithtools.com/CodeSmith/m/templates/42499.aspx
I used LLblgen several years ago. I hope what I saw has been fixed. We looked at the in-line SQL that was produced and saw, for selecting one row of data with the primary key passed in:
SELECT DISTINCT * FROM TABLE WHERE primark_key_id = #primarykey.
Really, DISTINCT? I always begged to do store procs instead, but what shot down by the project leader. I am not sure what time was saved by writing inefficient code.