background preloader

C#_

Facebook Twitter

Inversion of Control and Dependency Injection: Working with Windsor Container. Oren Eini November 2006 Applies to: Visual Studio 2005 Team Edition for Software Architects .NET Framework Inversion of Control (IoC) Dependency Injection (DI) Windsor Container Summary: This article discusses how to use Inversion of Control and Dependency Injection, generic specialization, the decorator pattern, chains of responsibilities, and extensible software to build robust, extensible, working software. (16 printed pages) Contents How to Build Better SoftwareThe First Feature: Dispatching an OrderLooking at a More Complex Example How to Build Better Software Close your eyes, take a deep breath, click your heels three times, and say, "There's no better thing than Inversion of Control and Dependency Injection, generic specialization, the decorator pattern, chains of responsibilities, and extensible software. " This article will discuss using all of the aforementioned to build robust, extensible, working software.

If you want to learn all about Windsor itself, you can go to its Web site. <! Covariance and Contravariance FAQ - C# Frequently Asked Questions. In this post I’ll try to answer the most common questions I find on forums and in documentation feedback about C# covariance and contravariance. It’s a big topic for a single blog post, so expect to see a lot of “more information” links. Special thanks to Eric Lippert and Chris Burrows for reviewing and providing helpful comments. What are covariance and contravariance?

In C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments.Covariance preserves assignment compatibility and contravariance reverses it. The following code demonstrates the difference between assignment compatibility, covariance, and contravariance. // Assignment compatibility. string str = "test";// An object of a more derived type is assigned to an object of a less derived type. object obj = str;// Covariance. In C#, variance is supported in the following scenarios: What is array covariance? Arrays are covariant since C# 1.0. DBMapper - A new ORM tool. Download DBMapper.zip - 71.2 KB Introduction Hi, this article introduces DBMapper, a new ORM tool I have designed and developed.

It's written on C# 3.5, and tested on Oracle 10g / 11g, Ms Sql Server 2005 / 2008 / 2012. Main featues of DBMapper: No vendor lock-in. So what is an ORM? Mapping connections So lets start mapping. Connections .Add(MyConnection.OracleTest, DBVendor.Oracle, ":") .Add(MyConnection.SqlServerTest, DBVendor.Microsoft, ":"); Note that for all string definitions (like connection, schema, table/column, sp names etc.), DBMapper api expects you to pass an Enum type for intellisense names without typing errors, and less typing. Public enum MyConnection { OracleTest, SqlServerTest } Also in the App.config file we supply our connection strings (connection strings can also be encrypted using standard .Net methods you can find on the web) So where do we map our connections and entities?

Mapping a basic class for querying Next we define our entity classes. An example select statement: Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository & Unit of Work Pattern and MVC3 Razor View. Download source - 7.2 MB Introduction In my previous article I discussed developing a simple, multi-layered architecture for a .NET application. However, there were few points that I skipped considering that the article was for beginners. When we talk about an application architecture there are certain points that need to be put into consideration before initiating. For example: Is the architecture loosely coupled? Is it to be service based? To answer these type of questions, .NET 4 has come up with a generic solution, making use of Entity Framework.

My effort in this article was to put some light on building a generic multilayered architecture using Entity Framework 4.1 and MVC3 razor view engine. Use of Seperation of Concerns, Use of Code First approach, POCO objects, Repository Pattern, Dependency Injection and Inversion of Control. Architecture Overview I have created a very simple application to add student details in a database. The architecture is service based. (fig 1.) 1. 2. All about abstract classes. Introduction Abstract classes are one of the essential behaviors provided by .NET. Commonly, you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types. You can make use of abstract classes to implement such functionality in C# using the modifier 'abstract'. An abstract class means that, no object of this class can be instantiated, but can make derivations of this.

An example of an abstract class declaration is: abstract class absClass { } An abstract class can contain either abstract methods or non abstract methods. An example of an abstract method: abstract class absClass { public abstract void abstractMethod(); } Also, note that an abstract class does not mean that it should contain abstract members.

Abstract class absClass { public void NonAbstractMethod() { Console.WriteLine("NonAbstract Method"); } } A sample program that explains abstract classes: Example Abstract properties Important rules applied to abstract classes. All about abstract classes. Design pattern – Inversion of control and Dependency injection. Updates added MVC and MVP design pattern tutorial date 19 december 2008 Table of Contents Updated Added link for how to do DI using Unity application bocks. Introduction I have been writing and recording on design patterns for past some days. DI using unity application blocks Part 1 – Design patterns Factory, Abstract factory, builder, prototype, shallow and deep copy, and singleton and command patterns Part 2 – Design patterns Interpreter, Iterator, Mediator, Memento and observer patterns Part 3 – Design patterns State, Strategy, Visitor, Adapter and fly weight pattern Part 4 - Design patterns Bridge, Composite, Decorator, Facade, COR, Proxy and template pattern Part 5 Model View Controller MVC Part 6 Model View Presenter You can download by architecture interview question book from here.

In this section we will discuss about how IOC and DI can help us build loosely coupled software architecture. The Problem – Tight Coupling Solution. Zombie Explorer: An n-tier application from top to bottom. I just want the code man, give me the code, well OK chillax, it's right here: Download source code - 11.8 MB Table of Contents Introduction This article is something I have been meaning to do for some time, not really for anyone's benefit other than my own really.

I have been working with both WCF/WPF a while now (I know some people consider these old technologies now, but I can assure you they are both alive and well, at least in my part of the world), and I have seen a variety of different approaches to developing an n-tier application with these technologies. I have seen good and bad things, some of the bad things being: I thought it would be nice to try and see if I could make a demo app which kind of did things the way I had it mapped out in my mind. I freely admit I have used certain paradigms I have seen along the way, such as issuing a Request and getting a Response. For the attached demo code I wanted to make sure I covered the following aspects: So what does the demo app do? Requests. Generic Repository Pattern - Entity Framework, ASP.NET MVC and Unit Testing Triangle. IMPORTANT NOTE: I have a new blog post about Generic Repository implementation for Entity Framework. Please check it out instead of this one: Clean, Better, and Sexier Generic Repository Implementation for Entity Framework NOTE:Entity Framework DbContext Generic Repository Implementation Is Now On Nuget and GitHub: DRY: Don’t repeat yourself which is a principle of software development aimed at reducing repetition of information of all kinds, especially useful in multi-tier architectures.

That’s what Wikipedia says. Step back. That’s what I have done for repository classes on my DAL projects. First approach (worst approach) At first, long time ago, I have been creating all the single methods for each interface. I am giving the examples here with EF 4.2 but I was following this approach with EF 4 which does not contain DbContext class. Best approach. Dependency Injection with Unity: passing runtime parameters | Simon on Software. Today, I had a problem of how to construct an object passing a specific date into the constructor.

Normally, this is not an issue encountered frequently as only “services” are passed via constructor injection. This time however, I needed to make sure a date was passed when the object was created, and this date needs to be evaluated each time. I’m not a fan of the “initialise” pattern, so that was out. Here is how I solved the problem using Unity DI Container. First, I defined a dependency and an implementation: Then, configure the container, and attempt to make some calls: Ok, so this is not doing what I wanted. If we resolve the item from the container again, we still get the same value: This was annoying, it seems we need some sort of factory or delegate that is resolved each time the object is constructed: Now we can register our new type: and each time we make a call, we get the behaviour we wanted: There must be other ways of achieving this, is there a better way? Async/Await - Best Practices in Asynchronous Programming. These days there’s a wealth of information about the new async and await support in the Microsoft .NET Framework 4.5.

This article is intended as a “second step” in learning asynchronous programming; I assume that you’ve read at least one introductory article about it. This article presents nothing new, as the same advice can be found online in sources such as Stack Overflow, MSDN forums and the async/await FAQ. This article just highlights a few best practices that can get lost in the avalanche of available documentation. The best practices in this article are more what you’d call “guidelines” than actual rules. There are exceptions to each of these guidelines. Figure 1 Summary of Asynchronous Programming Guidelines Avoid Async Void There are three possible return types for async methods: Task, Task<T> and void, but the natural return types for async methods are just Task and Task<T>.

Void MyMethod(){ // Do synchronous work. Async void methods have different error-handling semantics. Async/Await - Best Practices in Asynchronous Programming. A Generic Factory Method-Create Object From Interface Parameter using Reflection. Download source - 23.1 KB Introduction In this write-up, I will demonstrate a generic factory method which will create object from interface type parameter from an assembly (either current or provided) with the help of reflection. Background Many times, inside our application development code-base, we expose interface and hide its implementation with access modifier. Suppose we create a repository/data-access layer and expose various repository interfaces publicly with public access modifier, but its implemented classes we do not expose publicly instead we hide them with internal access modifier.

In such cases, we should create a factory method which will create that object based on interface type argument. If we did not use any IOC container in our application, then this type of factory method is very helpful. Using the Code I create a generic method, in this generic method Interface is its typed parameter. Conclusion. C# - Returning IEnumerable<T> vs IQueryable<T>