Lets say we have some very simple objects such as these two classes: [Serializable] public class ClassA { public string StrA { get; set; } public ClassB B { get; set; } public ClassA Clone() { return (ClassA)MemberwiseClone(); } } and [Serializable] public class ClassB { public string StrB { get; set; } } There are three types of copying objects. These are [...]
Archive for April, 2010
Copying objects – Cloning
Posted in Back to basics - C#, tagged c#, cloning, deep clone, IClonable, serialization, shallow clone on April 28, 2010 | Leave a Comment »
Testing for expected exceptions
Posted in TDD, tagged expected exceptions, test driven design, testing exceptions on April 17, 2010 | 1 Comment »
My last post detailed how to test methods that are not public. The example code I used was a method that divides one decimal by another. I thought this a good chance to demonstrate how to test for expected Exceptions. To refresh our memories, the method we are testing is this: public decimal PublicDivide(decimal numerator, [...]
Testing private, protected and internal methods
Posted in TDD, tagged internal, private, protected, test driven design on April 17, 2010 | 1 Comment »
I have seen a lot of debate about testing methods that are private, internal or protected. There are different arguments for both sides of the argument of whether these methods should be tested or not. I like to have a fine grained test coverage as possible and as I like to keep methods as short [...]
Dispose Pattern
Posted in Architecture, Design Patterns, tagged com interop in .net, Design Patterns, Dispose Pattern, resource management on April 16, 2010 | 1 Comment »
I had an interview the other day and thought I would write abut one of the questions I had. It involved releasing references to COM objects in .NET. The pattern to do this is unsurprisingly called the Dispose pattern. The problems with using COM components in managed code are as follows: 1. Developers can accidentally [...]
Decorator Pattern
Posted in Architecture, Design Patterns, tagged code reuse, decorator pattern, Design Patterns, web architecture on April 15, 2010 | Leave a Comment »
This is the second in the series of design patterns. The first post might have been a little verbose so I thought I’d liven this one up a bit with a good old fashioned class diagram and some actual code! I used the decorator pattern recently on the advert booking project. The scenario is this: [...]
Strategy Pattern
Posted in Architecture, Design Patterns, tagged code reuse, Design Patterns, strategy pattern on April 14, 2010 | Leave a Comment »
Ive been looking at the amazing book Head First Design Patterns by Erik and Elisabeth Freeman. I have been using various different design patterns for awhile and I thought I would write about some cool uses I have found for different types of patterns. This is the first post like this and it is about [...]
Finding the repository with dependency injection
Posted in Castle Windsor, IOC, tagged Castle Windsor, IOC, loosely coupled, separation of concerns on April 12, 2010 | Leave a Comment »
One thing I noticed, when using dependency injection was that the calling class, i.e the one that instantiates the container to do the injection wont be able to find the repository unless part of your code calls into the assembly it resides in. If you have a) Assembly for domain b) Assembly for DAL c) [...]
Debugging NHibernate with log4net
Posted in NHibernate, tagged debugging, log4net, NHibernate, ORM on April 11, 2010 | Leave a Comment »
Why are the errors to do with mapping from NHibernate so damn cryptic! As NHibernate uses Log4Net I added the following XML to an app.config file to my test project, included the log4net.dll reference and added this line of code to the TestFixture method to initialize Log4Net. log4net.Config.XmlConfigurator.Configure(); The config xml is: <?xml version=”1.0″ encoding=”utf-8″ ?> <configuration> [...]
NHibernate sessions and units of work
Posted in Architecture, NHibernate, ORM, TDD, tagged loosely coupled, NHibernate, ORM, separation of concerns, sessions, unit of work, web architecture on April 5, 2010 | Leave a Comment »
My understanding of the NHibernate Session is that in the case of a web application, the ISession should be created and destroyed with each HttpRequest. This is probably the most efficient use of the session and enables easy use of lazy loading within a single request. Sessions do not require much overhead to create and [...]
Many to Many insert in NHibernate
Posted in NHibernate, ORM, TDD, tagged many to many, Mapping files, NHibernate, ORM on April 5, 2010 | Leave a Comment »
This has probably been covered in a thousand blogs and technical articles but I couldn’t find one that covered this exact scenario, so here goes.. When adding a row to the link table between two entities in a many to many relationship, I wanted to make sure a single row is saved in the database but both [...]