C# – Object Composition – Removing Boilerplate Code

I have a single generic repository interface, which is implemented only once for a particular data storage. Here it is: public interface IRepository<T> where T : class { IQueryable<T> GetAll(); T Get(object id); void Save(T item); void Delete(T item); } I have implementations of it for EntityFramework, NHibernate, RavenDB storages. Also I have an in-memory … Read more

inheritance vs. composition for testability

I believe that the more you start to develop using design patterns, you’ll find more and more often where composition is going to be favored over inheritance. I actually believe in the Head First: Design Patterns book that “Favor Composition Over Inheritance” is one of the primary design principles. Your example of being able to … Read more

Difference between dependency and composition?

The difference can be seen in the two constructors: Dependency: The Address object comes from outside, it’s allocated somewhere else. This means that the Address and Employee objects exists separately, and only depend on each other. Composition: Here you see that a new Engine is created inside Car. The Engine object is part of the … Read more

Does C# support function composition?

public static class Extensions { public static Func<T, TReturn2> Compose<T, TReturn1, TReturn2>(this Func<TReturn1, TReturn2> func1, Func<T, TReturn1> func2) { return x => func1(func2(x)); } } Usage: Func<int, int> makeDouble = x => x * 2; Func<int, int> makeTriple = x => x * 3; Func<int, string> toString = x => x.ToString(); Func<int, string> makeTimesSixString = … Read more

Object Oriented Best Practices – Inheritance v Composition v Interfaces [closed]

Mark, This is an interesting question. You will find as many opinions on this. I don’t believe there is a ‘right’ answer. This is a great example of where a rigid heirarchial object design can really cause problems after a system is built. For example, lets say you went with the “Customer” and “Staff” classes. … Read more

Design patterns: Composite vs. Composition

Composition This is a design concept (not really a pattern). This term is used when you want to describe one object containing another one. It occurs very often in Composition over inheritance discussion. Moreover, composition implies strong ownership. One objects owns (i.e. manages the lifecycle) of another object. When parent is destroyed, all children are … Read more