Contents tagged with C#

  • A Simple CQRS Pattern Using C# in .NET

    For years in my apps I've used a Data Mapper pattern or even the Repository pattern to mediate between the business domain and the database. One thing I've learned is that something like this interface is no help at all: Continue reading...

  • PubSub Using RabbitMQ with ASP.NET Web API Subscribers

    A long time ago a colleague of mine, Bhaskar Apparajuvenkata, and I used WCF to implement a publish-subscribe system on our local network. (That was blogged here.) Ok it was less than 5 years ago but given the pace of change since then it feels very antiquated. At the time we were having a hard time convincing our infrastructure team to stand up MSMQ for us. This was before the software-as-a-service movement took hold. Back in those days something like a messaging or service bus framework was seen as an enterprise project. You had to have a project sponsor, huge timelines, and of course a big budget. We were a small development team and our needs were modest so at the time we rolled our own. Continue reading...

  • IComparer and Sorting Widgets

    Back in the turn of the last century, the golden days of .NET 1.1, if you wanted to implement a type-safe custom collection your best bet was to derive from CollectionBase in the System.Collections namespace. CollectionBase was more than just an abstract base class. It was also a wrapper around an internal ArrayList. If you wanted to sort strongly-typed objects in the collection you would implement an IComparer class: Continue reading...

  • Fetching Connection Strings

    Let's assume you wish to store your database connection string in the <connectionStrings> configuration section of Web.config. This is the best practice in my opinion because you can (and should) encrypt the section in production. Now you have two basic options: Continue reading...

  • Custom SiteMapProvider

    Usually custom SiteMap providers are written because we're pulling our nodes from a source other than xml. Jeff Prosise's provider written for SQL Server is a classic example. But I had a case recently in which the out of the box XmlSiteMapProvider was just fine; my problem was that I needed to do some peculiar security trimming on the nodes. No problem, just add roles to the nodes and enable security trimming in Web.config for the provider right? Not exactly. The requirements were that the system had to ship with the ability to turn a major piece of functionality on or off in production. Let's call the functionality "ViewWidgets" and let's say there's an app setting in Web.config called "ViewWidgets" with value="false". That means, the web site should suppress the Widgets unless and until it is flipped to true. Then Widgets should be available to the end user. One way of implementing this would have been to set a generic role or override Web.config in a subfolder. But I wanted the webmaster to be able to flip the flag to true or false in Web.config where he was more comfortable working. The solution turns out to be pretty easy. Just derive a custom provider from XmlSiteMapProvider and override the Initialize method to trim the node based on the app setting: Continue reading...

  • Generics and Nullable Types

    It is often useful in OR mapping to analyze a value type that might be null prior to setting it in a business entity. For instance, SqlDateTime type (in System.Data.SqlTypes namespace) is a value type that is nullable. You can call IsNull to check and Value to retrieve the underlying DateTime type: Continue reading...