Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

Tags: c#

All the LessThanDot Journals

The new features in C# 4.0

by SQLDenis


Permalink 28 Oct 2008 13:29 , Categories: Microsoft Technologies, C# Tags: c#

Microsoft has made available a document showing the new features in C# 4.0.

The new features in C# 4.0 fall into four groups:

Dynamic lookup
Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the C# static type checking and instead gets resolved at runtime.

Named and optional parameters
Parameters in C# can now be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted. Furthermore, any argument can be passed by parameter name instead of position.

COM specific interop features
Dynamic lookup as well as named and optional parameters both help making programming against COM less painful than today. On top of that, however, we are adding a number of other small features that further improve the interop experience.

Variance
It used to be that an IEnumerable<string> wasn’t an IEnumerable<object>. Now it is – C# embraces type safe “co-and contravariance” and common BCL types are updated to take advantage of that.

You can download a Word document written by Mads Torgersen with a little more info here: C# 4.0

4 comments »Send a trackback » 578 views

Dependency Injection Tool StructureMap 2.5 Has Been Released

by SQLDenis


Permalink 27 Oct 2008 07:08 , Categories: Microsoft Technologies Tags: .net, c#, dependency injection, structuremap

StructureMap is a Dependency Injection tool written in C# for .NET development. StructureMap is also a generic “Plugin” mechanism for flexible and extensible .NET applications.

The new functionality in StructureMap 2.5:

  • Completely revamped Assembly scanning options
  • Cleaner, more predictable way to initialize a Container.  StructureMapConfiguration is now deprecated, please use ObjectFactory.Initialize().
  • Optional setter injection
  • All new abilities to query the configuration of a Container
  • The ability to use StructureMap with ZERO Xml or attributes by default
  • The ability to add services at runtime. You can now programmatically add an entire Assembly at runtime for modular applications that might not want all services to be loaded at startup.
  • An auto mocking container based on Rhino Mocks 3.5. I was a doubter on the validity of AMC, but I'm sold now that I've used it
  • Contextual object construction
  • More sophisticated auto wiring rules
  • Supporting NameValueCollection and IDictionary types
  • Far more extensibility
  • Interception and post processing hooks for you AOP enthusiasts. StructureMap will NOT include its own AOP engine, but will allow you to use the runtime AOP technique of your choice.
  • More configuration options in both Xml and the Fluent Interface. Completely revamped the Registry DSL.
  • More options for modular configuration (mix and match Xml configuration or Registry's at will) – which basically had to trigger:
  • Completely revamped diagnostics, including the Environment Testing support
  • Transparent creation of concrete types that are not explicitly registered
  • Create objects with explicit arguments passed to the container
  • Use the underlying Container independently of ObjectFactory
  • Pluggable auto registration with your own custom Type scanning policies
  • StructureMap is now strong named (thanks to Steve Harman)
  • Pull configuration from the App.config (thanks to Josh Flanagan)
  • Generics fixes (thanks to Derrick Rapp)

Download it here: http://sourceforge.net/projects/structuremap

2 comments »Send a trackback » 192 views

Npgsql2 the PostgreSQL Net Data provider written 100% in C# has been released

by SQLDenis


Permalink 06 Oct 2008 06:58 , Categories: Data Modelling & Design Tags: c#, entityframework, npgsql2, postgresql

Npgsql2 has been RTM :-)

Npgsql is a .Net Data Provider for Postgresql. It allows any program developed for .Net framework to access the PostgreSQL database server. It is implemented in 100% C# code. Works with Postgresql 7.x and above.

Npgsql2 Supports .Net 2.0 and 3.5 and there is even added support for the EntityFramework

You can download all the goodies here: http://pgfoundry.org/frs/?group_id=1000140

Leave a comment »Send a trackback » 340 views

Nice .NET Design Patterns Articles

by SQLDenis


Permalink 17 Sep 2008 09:40 , Categories: Web Design, Graphics & Styling Tags: .net, c#, design patterns, oop, singleton

The DotNetSlackers site has a nice series of articles about design patterns. The articles were written by Granville Barnett and are a very good read. Here is what is in the first four articles.

Design Patterns – Part 1
Learn how to design more robust and maintainable code by incorporating design patterns into your software projects.
1 Introduction
2 The strategy pattern
3 Implementing our design
4 Summary

Design Patterns – Part 2
In this part of the design patterns series we will take a look at the observer design pattern.
1 Introduction
2 What is the observer pattern?
3 The Design
4 Implementing our design
5 Aren’t events already in .NET?
6 Summary

Design Patterns – Part 3
In this part of the design patterns series we will take a look at the factory pattern.
1 Introduction
2 The simple factory
3 Factory pattern
4 Summary

Design Patterns – Part 4
In this part of the design patterns series we will look at the singleton pattern.
1 Introduction
2 How is a singleton typically implemented?
3 The static modifier
4 Issues with our first design
5 beforefieldinit
6 Summary
7 Acknowledgements
8 References

So what are you waiting for? Start reading the articles!

Leave a comment »Send a trackback » 549 views

My Path to the Dark Side part 4 - the Repositories

by AlexCuse


Permalink 28 Jul 2008 07:07 , Categories: Microsoft Technologies, C# Tags: c#, nhibernate, unit testing

Previous posts can be found here:

Setting up the repositories for our objects is where this really starts to get fun for me. This is what allows us to work with the persisted objects so easily from our application code, without all the SQL getting in the way. The first thing we want to think about here is what we need the repository to do. Add/Delete/Update all come to mind of course. As well as retrieval of single objects and collections. These will be pretty much standard behaviors across most of our objects. So lets’ look at the interface first:

  1. using System;
  2. using System.Collections.Generic;
  3. using RecipeTracker.Model;
  4.  
  5. namespace RecipeTracker.Interfaces
  6. {
  7.     public interface IRecipeRepository
  8.     {
  9.         void Add(Recipe recipe);
  10.         void Update(Recipe recipe);
  11.         void Remove(Recipe recipe);
  12.         Recipe GetByID(int id);
  13.         ICollection<Recipe> GetByFamily(string family);
  14.         ICollection<Recipe> GetAll();
  15.         void Dispose();
  16.     }
  17. }

Looking at all those methods, there is really only one (GetByFamily) that we won’t need for any repository that we create. So we can put all the other methods into a BaseRepository class. We will need use generics so we can return all the different types however. But first, we need to get a session, so we can add a class for that.

  1. using System;
  2. using NHibernate;
  3. using NHibernate.Cfg;
  4.  
  5. namespace RecipeTracker.Repositories
  6. {
  7.     public class SessionProvider<T> where T:new()
  8.     {
  9.         private static ISessionFactory _sessionFactory;
  10.  
  11.         private static ISessionFactory SessionFactory
  12.         {
  13.             get
  14.             {
  15.                 if (_sessionFactory == null)
  16.                 {
  17.                     var configuration = new Configuration();
  18.                     configuration.Configure();
  19.                     configuration.AddAssembly(typeof(T).Assembly);
  20.                     _sessionFactory = configuration.BuildSessionFactory();
  21.                 }
  22.  
  23.                 return _sessionFactory;
  24.             }
  25.         }
  26.  
  27.         public static ISession OpenSession()
  28.         {
  29.             return SessionFactory.OpenSession();
  30.         }
  31.     }
  32. }

The SessionFactory part should look familar from part 3, the only difference here is that we are initializing the configuration using TypeOf(T) to determine which assembly to find the configuration in rather than TypeOf(MyType). We could probably get away with the latter for this purpose, because there probably won’t be more than one assembly in the application, but why be lazy right? After all, we do need to use generics to deal with the return types anyways.

So now this little bit of code doesn’t need to be handled by our repository, and it can focus on what it does best. So here’s the BaseRepository, it’s nice and simple since it doesn’t need to get its’ own sessions anymore:

  1. using System;
  2. using System.Collections.Generic;
  3. using NHibernate;
  4. using NHibernate.Cfg;
  5.  
  6. namespace RecipeTracker.Repositories
  7. {
  8.     public abstract class BaseRepository<T> where T: new()
  9.     {
  10.         protected ISession _session = SessionProvider<T>.OpenSession();
  11.  
  12.         public T GetByID(int id)
  13.         {
  14.             return _session.Get<T>(id);
  15.         }
  16.  
  17.         public ICollection<T> GetAll()
  18.         {
  19.             var products = _session
  20.                 .CreateCriteria(typeof(T))
  21.                 .List<T>();
  22.             return products;
  23.         }
  24.  
  25.         public void Update(T toUpdate)
  26.         {
  27.             using (ITransaction transaction = _session.BeginTransaction())
  28.             {
  29.                 _session.Update(toUpdate);
  30.                 transaction.Commit();
  31.             }
  32.         }
  33.  
  34.         public void Add(T toAdd)
  35.         {
  36.             using (ITransaction transaction = _session.BeginTransaction())
  37.             {
  38.                 _session.Save(toAdd);
  39.                 transaction.Commit();
  40.             }
  41.         }
  42.  
  43.         public void Remove(T toRemove)
  44.         {
  45.             using (ITransaction transaction = _session.BeginTransaction())
  46.             {
  47.                 _session.Delete(toRemove);
  48.                 transaction.Commit();
  49.             }
  50.         }
  51.  
  52.         public void Dispose()
  53.         {
  54.             _session.Close();
  55.             _session.Dispose();
  56.         }
  57.     }
  58. }

Now, look how simple that is to do what we need with our object? No building SQL queries, no creating parameter arrays, or anything. A nice simple bit of code that does just what we need it to do, without all the hassles. We just need a session and the simple commands that it offers, and we can do anything we need. Beautiful, right?

But what if we wanted to do something like get all recipes from a certain family? This will be specific to the object type we need, so we can do that in our implementation of the baseclass (hey, gotta have something in there right!). And this is in fact all that our RecipeRepository class has, one method:

  1. using System;
  2. using System.Collections.Generic;
  3. using RecipeTracker.Model;
  4. using NHibernate;
  5.  
  6. namespace RecipeTracker.Repositories
  7. {
  8.     public class RecipeRepository : BaseRepository<Recipe>, Interfaces.IRecipeRepository, IDisposable
  9.     {
  10.         public ICollection<Recipe> GetByFamily(string family)
  11.         {
  12.                 var products = _session
  13.                     .CreateCriteria(typeof(Recipe))
  14.                     .Add(NHibernate.Criterion.Expression.Eq("Family", family))
  15.                     .List<Recipe>();
  16.                 return products;
  17.         }
  18.     }
  19. }

This is some wacky looking code at first. It kind of reminds me of Linq, but what its’ called is HQL (Hibernate Query Language). I haven’t really gotten into it all that much, so I don’t feel qualified to speak about it in detail, but I do find it kinda cool. It may not be as easy as Linq, where you could do a nice Linq query such as

recipeList.Where(n => n.Family = family)

But remember this needs to work on older framework versions as well. I think the HQL is reasonably succinct, and even somewhat elegant. After all reading that you can just about instantly tell what it does. It just creates a criteria on the session for Recipes, and then defines the expression to be evaluated (table.Family = family). Pretty cool. We could probably figure out how to do this with generics pretty easily, but I figure this kind of method is going to be tied to your specific type, and you want to have a descriptive name, and all that.

So after all this I think we are ready to set up some tests. This will be on the next page (damn I’m getting long-winded!)

Pages: 1 · 2

2 comments »Send a trackback » 412 views

:: Next >>