C# database example project - c#

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...

Related

Accessing Remote Data via a VPS in iOS

I am looking to store information on my VPS that will be used by my application to present data to a user. I've looked around thoroughly and still can't manage to find a solid tutorial on how to do this in a real-world application.
I'm new to C# and programming in general, so naturally I looked around a lot before posting. I found that the DataContext and LINQ classes are very helpful for obtaining data. I also researched and found out that I could use attribute-based programming and create properties that are mapped to certain tables or columns in the database.
This would also be very helpful when trying to populate my UITableView with things because I would already have a class full of properties and data that is already hooked up to my database.
However, I just don't know how to apply this. All the remote data tutorials I see for MonoTouch and iOS in general are heavily reliant on JSON, REST, and SOAP, which I am not that familiar with.
In conclusion, all I want to do is connect to my VPS, query some data, and populate a UITableView. I know how to do the last one, but the first two are still really vague to me. Any assistance would be of great help!
.NET offers a lot of ways to communicate data and MonoTouch supports most of them. It's worth noting that WCF (Windows Communication Framework) is not fully supported (only the Silverlight subset is available in MonoTouch).
It's hard to recommend a specific technology without more details. If you're new to C# (and .NET) then you should look for something that offers you (lot of) samples - both for learning and that look similar to the application(s) you want to develop. You should also look (e.g. here on stackoverflow) if people are using it and supporting it (i.e. answering questions about it).
I've been reading (not using yet) about ServiceStack which supports a lot of options, has great performance, many samples and supports MonoTouch. Another popular one is Protobuf.net.
You might also want to ask for other people experiences on the MonoTouch mailing-list (stackoverflow is not the best place to ask for different opinions - at least without a very specific question).

NHibernate (or other worth recommendation ORM), real life example?

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.

Getting familiar with .NET - What's the best way?

I have a friend of mine who owns his own software consulting business. Most of the stuff his employees work on is .NET related development. He's been out of actual development for many years, and has been focused on building his business. He asked me the best way to get familiar with the whole .NET platform and development under .NET. Is anyone aware of a video training series, or something similar, that's designed to get someone up to speed on all aspects of .NET?
This is the obligatory "port another project into .NET" answer.
My guess is that he doesn't have to cover all of .NET, but a great way to get up to speed with both C# and a significant part of the .NET framework is the C# 4.0 in a Nutshell book. It assumes some programming experience and covers a lot of stuff.
In my opinion the first step is to read a book which covers different parts of .Net Framework. Pro C# 2010 and the .NET 4 Platform is one possible book as it covers different technologies such as WPF, WCF, Linq, Ef, Asp.Net. They are not discussed in depth but is a good resource for getting familiar with current technology stack.
Also, I would recommend actually developing in .Net as is many knowledge comes from the actually doing it.
In addition to the other answers, maybe your friend could sit in on any code reviews, design sessions or even perform pair programming with the other developers once he gets a basic understanding of things on his own. I suppose this could be difficult in a consulting business vs. regular development shop though.
In addition to all of the books and blogs which will be mentioned, I always recommend people start learning with something practical.
When I'm teaching I make up simple exercises broken down into chunks like build a basic database, try simple things like displaying the data, filter the data using drop down, add auto postbacks and update panels, updating the data in the DB. It doesn't take long to get an overview of the basic concepts, techniques and tools when presented with examples. And then it's down to experimentation, imagination, and research!
Buy Visual Studio and an MSDN membership - in case its a startup there maybe various options to reduce this cost (Bizspark/Websitespark)
Go through common walkthroughs - areas to go through are Winforms, Asp.net, Asp.net Ajax in that order. Can go through WCF, Silverlight and other framework options later.
Search the web for 'Azure trial' - supposedly, there is a one month free pass available. Dont know if this is real, but if it is, take it and deploy simple applications on the cloud - learn what Windows Azure and Sql azure are all about. After a while can learn about Appfabric messaging platform as well.
After this, start deep-diving into any areas of the technology depending on project needs.
Channel 9
ASP.NET getting started
Dimecast
DNR TV
more than reading any book what made get started with .NET was doing projects. Start building a web site if you want to learn ASP.NET, you get to learn C# and VB as well like this. Just by reading a book each chapter would take lot of time. Initially you will do lot of mistakes and you will frequently get the dreaded yellow error page. The more mistakes you make, the merrier. My experience with .NET is limited to ASP.NET, C#,LINQ, web services, SQL Server 2008. But it took less than 20 days for me to get to know about all these stuff. Now I am trying to do WPF, WCF, Silverlight projects. IF I read any .NET book now, it wouldn't take much time for me to complete it.
I realy suggest to use a step by step teach your self book. They are good for beginners and have some practices maybe something from SAMS publishing like
Teach yourself c# in 21 days

Are there any worthy CSLA alternatives available?

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.

What ORM to Run: telerik Open Access VS Subsonic VS linq to sql VS Active Record

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.

Categories

Resources