background preloader

C#

Facebook Twitter

Standards

.NET Naming Conventions. Generic Service Locator - The Code Project - C#... Download source files - 24.88 KB Introduction In general, there are two approaches to implement the Service Locator pattern. The first is like the following: public class ServiceLocator { ... public void AddService(String ServiceName, IService Service){...} public IService GetService(String ServiceName){...} } You can imagine what the client code looks like. IOrderSvc OrderSvc = (IOrderSvc) ServiceLocator.GetInstance().GetService(ORDER_SVC); The other approach is like this: public class ServiceLocator { public void addService(String ServiceName, IService Service){...} public OrderSvc getOrderSvc(){...} public AccountSvc getAccountSvc(){...} ... } Both approaches have their pros and cons. Generic Methods The code for the second approach shows a pattern: public T GetT(){...}

The pattern is exactly what generics are designed for to eliminate the duplication. Conclusion No changes to the new ServiceFactory will be needed when adding a new service. References History 30th September, 2006: Initial post. C# 2.0 Iterators. Total votes: 3 Views: 18,202 Comments: 0 Category: Iterators Print: Print Article Please login to rate or to leave a comment. Iterators provide a simpler way to create classes that can be used with the foreach statement without implementing the IEnumerable and IEnumerator interfaces. Introduction Iterators are a new feature in C# 2.0. An iterator is a method, get accessor or operator that enables you to support foreach iteration in a class or struct without having to implement the entire IEnumerable interface.

Iterators Overview An iterator is a section of code that returns an ordered sequence of values of the same type. The yield keyword is used to specify the value, or values, returned. Usage Scenario Suppose we have a class named ProductCategory, and another class named Product. Product Class Product class is very simple as shown bellow: 01.class Product 03. private string _name; 04. public string ProductName 06. get { return _name; } 07. set { _name = value; } 10. public Product(string name)

C# Coding Guidelines. SteveX Compiled » String Formatting in C# I couldn’t find a quick reference to .NET string formatting using the String.Format() function, so I created this one (which has also spawned this String Formatting FAQ). When I started working with the .NET framework, one thing puzzled me. I couldn’t find sprintf(). sprintf() is the C function that takes an output buffer, a format string, and any number of arguments, and builds a string for you. For example: char szError[256];sprintf(szError, “Error %d occurred.

\n”, nError); This would write “Error 12 occurred.” into the szError buffer (assuming nError was 12). Str << “Error ” << nError << ” occurred.” << endl; Or something close to that. The .NET framework handles strings very nicely – but it takes some getting used to. String errorString = String.Format(“Error {0} occurred.”, nError); Teeming with metadata, the .NET environment doesn’t need the format string to say what type of data you’re formatting, just where you want it. Strings Numbers Basic number formatting specifiers: Dates. Fun with C# functions - Wes&#039; Puzzling Blog. Sign in | Join Wes' Puzzling Blog ... trying to solve the puzzles of .NET This Blog Syndication Search Tags Navigation Archives Projects Fun with C# functions Dustin Campbell has an interesting series on C# functions on his Did it with .NET blog.

Some interesting stuff that ties together a number of C# 2.0 and 3.0 feature sets. Published Sunday, November 11, 2007 4:26 PM by puzzlehacker Filed under: .NET, Code Comments No Comments Terms of Use. C# by Contract - Using Expression Trees « The W... Last time I created a simple, but powerful, little design by contract library in C# 3.0. It took hardly any lines of code, and covered a broad range of possible usage scenarios. See here, for more on DBC. One thing that bothered me was the fact that if something failed a check, it wouldn’t tell what went wrong. I had a little free time today, so I thought I’d fix that.

I wanted the exceptions it threw to have the text of the check we were performing. In my previous efforts at .NET DBC, I used strings to pass around the predicate body that I wished to evaluate. [Requires("arg1 > 10")] [Requires("arg2 < 100")] [Ensures("$after(Prop1) == $before(Prop1) + 1")]public void TestMethod(int arg1, int arg2, string arg3) { Prop1 = Prop1 + 1; System.Diagnostics.Debug.WriteLine("MyTestClass.TestMethod.Prop1 == " + prop1.ToString()); } This let me to test private fields and properties, but on the other hand it stopped me from testing sealed classes. Int BinarySearch(object[ ]! Till then, enjoy! Extension Method - Database. Rhino Commons, Repository&lt;T&gt; and Unit Of Work. Rhino Commons is a great collection of stuff that I gathered along the way, but never documented.

There is a sample application ( but not much more. I want to spend a few minutes talking about the way the data access part of it works. This is post about how it works, not how to make it work (in other words, very little code here). Before I start, I want to mentions that Rhino Commons is (highly) opinionated software, unlike Castle or NHibernate. It is a separate place where I take what Castle & NHibernate gives me, add a mix of my own best practices and let it run. The data access part in Rhino Commons revolves around the Unit Of Work, Unit Of Work Factory and the Unit Of Work Application. It started as a set of wrapper methods and sort of grew from there.

As you can see, the IRepository<T> is serving as a way to query NHibernate very easily. Invoke a Static Generic Method using Reflection... Which type should I use in C# to represent numbers? - Fabrice&#039;s weblog. LINQ Extensions | Projects | TomasP.Net. AutoMapper - Home.