Reactive Extensions API: Domain Events Example

About two weeks ago, I started exploring Reactive Extensions API. Should say, it looks very promising. I found dozens of examples. All this examples gives understanding what Rx is and how to use it. This is really cool. But this does not answer the question - what problems Rx is intended to solve. This post is one of the possible answers.

Example Bank Account Model

First that comes in mind is Event Driven Business Logic. In DDD word this approach sometimes referred as Domain Events. Good explanation could be found in “How to create fully encapsulated Domain Models” and implementation in “Domain Events - Take 2”.

To illustrate this stuff, I will use Bank Account model with SendManeyTo workflow. Look at the code bellow. It should look familiar.

public class  Account{
  
	public string Number { get; internal set;  }   
	public decimal CurrentBalance { get; internal set; }			
	
	public void SendTransferTo(string targetAccountNumber, decimal amount) {                
		//  Withdrawal from this account balance
		CurrentBalance -= amount;
		
		// And deposit target account
		DepositeTargetAccount();
	}
}

// Usage
var account = new Account("Test");
account.SendTransferTo(targetAccountNumber, 25.0m);

Most interesting stuff here is DepositeTargetAccount method. It should make target Account aware about money transfer. How we will do this in real life? Just send a message. So let implement this.

Making Account Observable

First of all I need TransferMoney Observable, later target account will be observe it. Moreover, I need message TransferMoney to hold required information. And at the end DepositeTargetAccount should send TransferMoney message. Easy.

// Message to transfer money
public class TransferMoney
{
	public decimal Amount { get; set; }
	public string TargetAccountNumber { get; set; }
	public string SourceAccountNumber { get; set; }
}

public class  Account{
	
	// Observables
	
	// Back-end field, it will store instance 
	// of the observale implementation.
	internal static readonly Subject<TransferMoney> TransferMoneySubj
        = new Subject<TransferMoney>();
		
	// Public visible Observable, so anybody can subscribe
    public static IObservable<TransferMoney> TransferMoney
    {
        get { return TransferMoneySubj.Hide(); }
    }
  
	public string Number { get; internal set;  }   
	public decimal CurrentBalance { get; internal set; }
	
	public void SendTransferTo(string targetAccountNumber, decimal amount) {                
		//  Withdrawal from this account balance
		CurrentBalance -= amount;
		
		// Send money transfer message
		TransferMoneySubj.OnNext(new TransferMoney
		{
			Amount = amount,
			SourceAccountNumber = _number,
			TargetAccountNumber = targetAccountNumber
		});
	}
}

You may notice that TransferMoney is static. This reflects fact that I need to handle transfers from/to all Accounts.

Observing TransferMoney

Next peace of code we need is to handle money trnasfer. Let me write MoneyTransferObserver.

public class TransferMoneyObserver : IObserver<TransferMoney>
{
    private readonly AccountRepository _repository;

    public TransferMoneyObserver(AccountRepository repository)
    {
        _repository = repository;
    }
	
	// This is primmary place for the logic
    public void OnNext(TransferMoney value)
    {
		// Let's get target account
        var target = _repository.GetAccount(value.TargetAccountNumber);
		
		// and make it aware about transfer from source account
        target.ReceiveTransferFrom(value.SourceAccountNumber, value.Amount);
    }

    public void OnError(Exception error)
    {            
    }

    public void OnCompleted()
    {
    }
}

And register it:

Account.TransferMoney.Subscribe(new TransferMoneyObserver(repository));

That’s it. What is more interesting is that this stuff opens endless abilities. For example, we can log every transfer to the console.

Account.TransferMoney
	 .Subscribe(t => Console.WriteLine("Log transfer of {0:c} bucks.", t.Amount));

Or notify manager about all noticeable transfers.

Account.TransferMoney
	.Where(_ => _.Amount > 100.0m)
	.Subscribe(_ => SendMessageToManager());

It is easy implement compensation logic. For example, target account can prohibit transfer. What it need is just send appropriate message. Source account will handle it, and undo withdrawal. We can filter events; aggregate them; everything we can do with data, now possible with events.

At the end

I hate stuff that I cannot try. So most of my posts are illustrated with compliable examples. This post is not an exception. GitHub home page - here.

Enjoy.


Does Event Sourcing work with DDD?

This post is inspired by CQRS a la Greg Young. I do not think that this is answer to Mark’s post. Rather this is kind of thoughts on the topic. To be kind, I have no technical prove for anything I write below. So your comments are welcome.

Problem

The reason I love DDD is because it natural. Yes, you make class Person and add behavior to it. And this works. You know this. During one of the E-VAN, Mark did a great presentation of the CQRS. What was even more interesting is integration of the Event Sourcing pattern to the whole picture. BTW I am not even sure that CQRS was the central pattern of this talk. The whole design of his domain model is driven by Event Sourcing pattern. This is actually what I want to discuss.

What brought my attention are too many lines of plumbing code. In contrast to classical DDD, Mark’s Root Aggregate overloaded with infrastructure concerns. No one can read this code easily. Now you cannot read behavior of the entity as plain text. Aggregate Root is not modeling concept any longer. It just a container with state required for logic and some logic… And really I do not like this.

One of the legal opinions is:

… I think it only illustrates a boundaries of selected platform/language … says legigor.

Igor thinks that in future we will have kind of technical solution that will reduce verbosity. I cannot agree more, but even with this in mind I am not sure that Event Sourcing is compatible with Rich Models.

What is the key feature of the DDD? I think it all about mixing state and behavior in one object. What about Event Sourcing? Event Sourcing in contrast is about splitting state changes, state and as result behavior. So why put all of them to the single class? Because DDD says so? I am fail see another point.

Solution?

Let’s think about responsibilities of the Mark’s Root Aggregate. First of all it should keep state of the aggregate. Second is, of course, business logic. And third is handling Events (leaked infrastructure concern, btw). For me this is violation of the SRP. What about separate all this stuff?

First of all let me extract Withdrawl method from ActiveAccount to another class WithdrawalCashBehaviour.

public class WithdrawalCashBehaviour
{
    public Amount AmountToWithdrawal { get; set; }
    public ActiveAccountSnapshot Account { get; set; }        

    public IEnumerable<IEvent> Execute()
    {                
        // Guard
        if (Account.IsClosed) throw new ApplicationException("Account is closed...");
        if (Account.Balance.WithdrawlWillResultInNegativeBalance(AmountToWithdrawal))
			throw new ApplicationException("Not enuogh money...");
		
        var newBalance = Account.Balance.Withdrawl(AmountToWithdrawal);

        yield return new CashWithdrawnEvent(newBalance, AmountToWithdrawal);
    }
}

Of course, logic requires state. So we move it all together. Interesting outcome. We do not need mutable state any longer. So it was replaced with ActiveAccountSnapshot. Another change is that Execute() returns Events, instead of raising them…. I do not like this non-business concern in code, but even more I dislike raising global events…

Now ActiveAccount (Root Aggregate) does not have state. So we will drop all related code. The rest is bunch of methods that will create Behaviours and execute them. So the responsibility of the ActiveAccount is to be proxy between command handlers and behaviors… Looks quite stupid. I am ready to drop ActiveAccount.

So now my domain model looks like… a bunch of the behaviors and read-only representation of this model. Represenatation model, for example, could be generated from View Database.

Well, I am not quite sure that this is best solution, but it looks less cluttered compared to Mark’s, and I hope that did not lost functionality from original solution.

Enjoy!


NET 4.0: New and useful LINQ extensions to the IEnumerable

Few months ago I made post with comparison of Ruby, Java and C# code to read all lines from text file. That time I have to code ForEach extension method for IEnumerable<T>. It allows executing arbitrary code against each element of the sequence. I believe most people already have this method.

Other languages already have such construct. For example iter in functional languages like F# or ForEach for List<T> in C# or each in ruby. IMHO this construct is really usable.

Anyways, today is good day. Doing fast review of the Reactive Extensions API (abbreviated form is Rx) I found library System.Interactive.dll. The only public class is EnumerableEx. And yes, it holds bunch of extension methods for the IEnumerable.

First extensions that got my attention is Do and Run. Both invoke Action<T> for every single element. Do is lazy, while Run is not. Code form previous post could be rewritten:

File.ReadLines(inp).Run(Console.WriteLine);

Another one is MemoizeAll. This is funny because today, Oren Eini (Ayende Rahien) found performance problem with UberProf. His solution to the problem is ToList(). ToList() converts lazy IEnumerable<T> to List<T> effectively break laziness. MemoizeAll is another and probably better solution for the problem. It caches results of previous execution but works in lazy way. So his code will look like this:

var statements = session.Statements.Where(x => filter(x)).MemoizeAll();
return new SessionStatistics
{
    NumberOfStatments = statements.Count(....);
    NumberOfCashedStatments = statements.Count(....);
}

This will execute Where statment exactly once. In contrast to original code it will do this in lazy way (in this case in constructor of the SessionStatistics).

Moving forward. If you need to defer creation of the enumerable, use Defer. If you need execute some stuff at the end of the sequence, use Finally. If you need repeat your sequence, use the obvious Repeat. What about prepending elements to the sequence? Use StartsWith. And this is not the complete list. It has about two dozens of extensions. You can visit unofficial wiki with list of this methods.

Go ahead, download Reactive Extensions API and explore this stuff yourself, it really usable! It available for both .NET 3.5 SP1 and .NET 4.0.

BTW, I do not know why this stuff is part of the Rx, it should be Core.

Have a good day!


FsSpec: Introducing yet another Unit Testing/BDD framework for F#

Despite of a fact that we already have number of frameworks for unit testing for F#, let me introduce another one.

Source code of the FsSpec is hosted on GitHub, you can donwload zip

Syntax

Syntax is inspired by RSpec, so specification looks like:

do describe "Calculator" [        

    it "should add two integers" (fun unit -> 

        let res = Calc.Add 2 2
        res.should_be_equal_to 4
    );


    it "should devide two integers" (fun unit -> 

        let res = Calc.Div 2 2
        res.should_be_equal_to 1
    );

]

Looks pretty well, isn’t it? Unfortunately, I have number of problems that I have to solve to make this stuff usable. First of all is shared initialization of the specifications. Now you can do this just before specification (describe statement) but this looks bit ugly. Second are assertions. Now they are implemented as extension methods and look less readable than RSpec’s assertions.

Implementation

Well, it about 46 lines of code… Not too much to discuss. Better to visit repository on GitHub.


Can you guess error in this Java code snippet?

Our application has been upgraded from GXT 2.0.1 to GXT 2.1.0. And this snippet causes all our data forms ignore data…

So, can you guess where error is?

public static TextField<String> textField(final String name, final String label) {
    return new TextField<String>(){{
        setName(name);
        setFieldLabel(label);
    }};
}

Usage:

TextField<String>  f = textField("Test", "Test");
assert f.getName() != null // Fail

Let me know if you need more information :)


Early review: Master Data Services November CTP

So, what Master Data Services (MDS) is? This is Master Data Management solution from Microsoft. So what Master Data Management is? Tool that help to store master data in the enterprise. What Master Data is? This is key data, like your customers, products, etc. Interesting?

SQL 2008

Abstract Intention

What you can expect from MDM solution? First of all central data storage. Some sort of the schema designer. Some sort of support for schema versions. API to access data. API to put data. Simple business rules. Validation. Built-in user interface to data.

And yes MDS has all this stuff. The problem that MDS implements most of them in totally unusable manner. But let go closer. To get proofs you have to install this guy. If you are OK to trust me, just skip installation section.

Installation of the Master Data Services November 2009 CTP

Master Data Services is part of the SQL Server 2008 R2 64-bit, so you have to download one. You may notice 64-bit. This is important. MDS is bounded only with 64-bit version.

After download complete, you will have self-extracting archive. Unzip.

I am not sure why, but MDS works only with SQL Server 2008 R2 databases. So you have to install SQL 2008 R2 database engine, even if you have for example SQL 2008. This installation is not any different from regular installation. BTW, MDS installer is not part of the SQL 2008 R2 installer. Do not worry about this.

And now final step. Search for masterdataservices.msi within setup folder. Execute it. Click few Nexts and you are done!

Usage, Administrator perspective

You have to start from configuration. Almost everything is intuitive, so I will not step here.

After configuration, you can start design you Master Data. I said you can?

To design entity you have to give it name, say if it hierarchical. No magic here. Next step is define attributes. Each field will take at list 1 minute to add. UI is very slow. Now imagine entity Customer with 30 fields. It is evil.

Probably next step is business rules. Unfortunately, this part of the designer does not work even in IE. What is interesting here, is may be “start workflow” action. But cannot test this.

Fehhh. Deploy. No magic here. This is unexpected, but it works.

Now, you can start explore/add/update your data. Seems this also works. But even SharePoint has more usable UI.

Data API, Developer perspective

I do not even know how to start. It is unbelievable. Retrieve data API is direct access to the SQL Views in a database! But this not the end! Can you guess what is the name of the view for the entity Customer in Invoicing model? Yes! It is “viw_SYSTEM_2_6_CHILDATTRIBUTES”, where 2 is Invoicing and 6 is Customer. The Matrix. BTW, the same magic with import operations. And this API is targeted as primary integration API!

Management API, Developer perspective

Management API is implemented with WCF. Looks pretty developer unfriendly. For example for some reasons localization stuff are arguments of every call, why not headers? Many out parameters.

Probably future of this stuff

This is kind of unproven information.

  1. MS will implement integration with other products. SharePoint, Dynamics and BizTalk among them;
  2. MDS will provide secondary Data API. MDS will implement OData, so we will have normal REST based API;

And my conclusion

I think that we need such tool. But MS should not reject what they already have.

For example, SQL Modeling Services has a way better ability to manage schema, edit data and so on. Why not just to employ SQL Modeling Services to do this job? Another example is SharePoint Foundation. It already covers most of the scenarios MDS has. I am just wander if folks from MDS seen SharePoint Foundation 2010. They are not even comparable from UI perspective.

Well, at least for now, this product is unusable and could be safely skipped. Looking forward for total rewrite in v2.


What to watch at PDC09?

There is no answer to the subject. Of course, it depends. Below are my opinions about some of the videos. They are very subjective. So be careful.

PDC09

Build a .NET Business Application in 60 Minutes with xRM and SharePoint

Want to see how team of MS stuff failed to achieve requirements in time? You are welcome. This video is for you. With this example you will find out how not to write spaghetti code.

Anyways, many interesting high-level stuff about SharePoint, xRM (Microsoft Dynamics CRM), integration between them if you never seen them before.

Future Directions for C# and Visual Basic

First half is bla-bla-bla about stuff we will get in C# 4.0. This could be safely skipped.

What is interesting is second part. It starts at 33:20. It is about features that will be after C# 4.0 and Visual Studio 2010.

Fist one is about compiler. The C# compiler will be rewritten in C#. The same with VB.Net. Compiler for VB.Net will be in VB.Net. Additionally, Microsoft will open compiler. So you will have access to expression trees, you will be able modify compilation and so on.

Second feature is new keyword yield. Not actually new, rather with new meaning. So the code:

var response = yield request.AsyncGetResponse ();

This line of code means that you start getting response, release thread, when done, system will up your thread and put result to the variable response.

As for me this is awesome. I cannot even imagine all stuff I am written with callbacks will get such nice syntax.

You can find full example of this magic at 50:13 of the video.

And we will not see extension properties in near future.

How Microsoft Visual Studio 2010 Was Built with Windows Presentation Foundation 4

Nothing notable in this session. Just normal process of the adaptation.

Great to know that Microsoft have doing at least something with WPF. Actually this presentation clearly show this. During this port they “discovered” that really text is bit blurred… Looks funny.

Lap Around the Windows Azure Platform

Marketing bla-bla-blas about Azure. Could be safely skipped.

Data Programming and Modeling for the Microsoft .NET Developer

First part is about… OK, I do not know about what it. It starts from SqlConnection (“SqlConnection is the assembly” - 4:05), then continue with well know Entity Framework awesomeness. And ends with some “Qudarant”. Could be safely skipped.

Second part is about OData. It starts from 33:18.

This is another story. OData is Microsoft take to standardize data exchange. Presentation looks great. Don Box show how to connect data from various sources like some NASA, some SharePoint data, some ad-hoc data. And then work with it in C#, in Excel. This protocol is built on top of AtomPub. And technically implemented as ADO.Net Data Services. It worth watching. as this could influence your architectures. By the way, funny quote from 50:45 - “OData is our new ODBC” :).

I will fail to describe this session if will not mention what Don Box disclosed at the end. At 54:00 he says that XML based EDM files of the Entity Framework will be in future replaced by M language. Full example of language that will describe Entity Framework model could be found at 55:27. Pretty good job!

Microsoft Project Code Name M The Data and Modeling Language

If you are interesting in “The M” you can watch this video. You will know how Don Box is chewing :). Anyway this is good introduction.

Funny fact, when Don presented “M” integration to the ASP.NET project by writing some ASPX code. He said: “I feel like ScotGu”. Ha-ha :).


I will post new reviews later. Stay tuned.


Microsoft Office Outlook 2010: How to connect e-mail account hosted on Windows Live Custom Domains?

You may know about Microsoft Office 2010 Beta. One of the revolutionary (sic!) improvements is ribbon interface in Outlook. Despite of sarcasm, it looks polished. So if you have time it worth a look :). But return back to the earth and let’s try to connect live.com account.

Microsoft Office Outlook 2010 UI

You may remember Outlook Connector for Office 2007. But check out this post Connecting to Hotmail accounts in Outlook 2010 in Office Outlook Team Blog. They made dramatic improvement.

Let Outlook 2010 determine if you need OLC 14 and install it for you

We are working hard to make sure that the experience of moving to Outlook 2010 is as smooth as possible when it comes to accessing your Hotmail accounts. Outlook 2010 is able to determine when OLC 14 is needed and points you to the correct download location for the OLC 14 package.

Can you guess how they made this?

Adding a @hotmail.com, @live.com or @msn.com accounts through the Add New Account dialog

Should you choose to add a new account that uses the @hotmail.com, @live.com or the @msn.com domain and OLC 14 is not installed, Outlook 2010 will prompt you to download and install it!

Stop. “@hotmail.com, @live.com or the @msn.com” - are you kidding? What about mail hosted on Windows Live Custom Domains? Well. Nothing here. Outlook 2010 just fail to detect that this is Hotmail account (to be honest Outlook 2007 also fail). The solution is obvious, just download Office Live Connector 14 and you are done. But this is not so easy. Actually you cannot. At least I was not able to find it.

My first take was to create fake Hotmail account, connect it, and then to reconnect with my account. But the result solution is much more genius!

In the Add New Account dialog, when prompted for e-mail address, just type something fake with hotmail.com domain. Hit Next. And agree to install Office Live Connector 14 :).

Office Live Connector - Install Now

Have a fun day!


Google Wave - GWT is probably a wrong technology choice

Few months ago I have watched IO presentation about Google Wave, I was really impressed. Also application I am working on uses GWT for client side (server-side is .NET). Eventually, I got invitation to the Google Wave.

What I can say. Looks cool. Very cool. And idea behind is great. This is modern replace for email. This is something that have had to appear some day. So yes, it is cool.

Google Wave

Despite of all this WOWs, I am trying to think about technology. You probably know that the Google Wave is all about HTML5 and other buzz stuff. The less known is fact that it GWT based. In short this is special Java compiler that produce regular JavaScript. Google have spent abnormal efforts to make results of the GWT compiler as browser capable as they can. And this really works. Most of the time…

GWT

Now lets imagine Google uses Silverlight or Flash or Java:

  1. Browser compatibility. With everything Google do for GWT, it still does not 100% compatible with all browsers. For me Google Wave crashes in IE (in Google Chrome it also crashes, but in IE it does not even start). With our own application, we still have latches on some browsers. Compare this to guaranties that gives us Silverlight or Flash or Java.
  2. Features like access to user’s hard drive, bitmap edit, network and so on. HTML does not have such futures at all or they are restricted, and probably so will be in future. For example we use Flash object to give user ability to select multiple files.
  3. Layout. HTML and DOM is not designed for serious Desktop like User Interfaces. This is why Google Wave is all about absolute positioned, calculated blocks. Every resize, every move makes more or less all blocks to be recalculated. Well, this is about client. But what about development? Right now I can compare GWT, Ext GWT and Silverlight layout managers. And I’m begging you ask about what is best, answer is Silverlight. This just because it was DESIGNED for such stuff. Everything that could GWT or Ext GWT offer is actually OVERCOMES of the leaked HTML layouts.
  4. Development experience. Compilation of our application take about two minutes. Debugging is hard. Hosted mode has differences with compiled mode. Many of the Java stuff does not work. I think Google has the same experience. Looks pretty bad? Can you guess why? Because GWT try to OVERCOME leaked execution engine. Yes I am about JavaScript. BTW, IntelliJ IDEA is way better than Visual Studio, but even this does not help.

Lets think what can GWT give:

  1. Broader usage, as we do not have to have any plugin installed. This is true. But only for simple stuff. RIA always have to have advanced features so you still have to use plugins (such as, GitHub uses Flash to put stuff in your clipboard). And do not forget about layout performances. Just wonder how our application (way heavier as Google Wave) behave on netbooks.
  2. Browser as a platform. For example, one can expect that she have ability to select HTML table and paste it to the Excel as she do often. Unfortunately, this is not a case neither for Google Wave nor for Ext GWT Grid. Just because they cannot leverage on the restricted HTML table. Another example is back button. This is good that Google Wave has support for the back button. But to do this, they use hidden IFRAME. And for me this looks like another OVERCOME of the leaked platform. And yes, Silverlight is also support back button.

So what?

This stuff, makes me think if GET is suitable for RIA at all. I cannot understand why Google does not invest in Silverlight :) or something like this. Why they are building yet another abstraction on already leaked abstraction? I cannot even guess.

I am wondering what my employer will say if he read this :).


Not so generic Generic Repository

I know two kinds of people, who use generic repository and who do not. Personally I do not like generic repositories. I feel that “one size feet all” is not a case here.

At the same time I am lazy and do not like to write each repository. Fortunately, I found nice solution that keeps repository custom, but do not need too much code.

I have abstract base repository, with most of the stuff I can predict.

public abstract class AbstractRepository<T>{
	public T GetById(Guid id);
	public Add(T entity);
	....
}

Then I have very custom repository contract. For example in product repository I have custom method GetByNumber.

public interface IProductRepository{
	Product GetByNumber(string number);
	void Add(Product product);
}

And I have implementation of the IProductRepository. Because of base class AbstractRepository, I have to implement only custom methods.

class ProductRepository : 
	AbstractRepository<Product>,
	IProductRepository
{
	
} 

Now, let’s look to the client code. Because client code knows only about IProductRepository, everything generic like GetById are not visible.

public class Client
{
	private final IProductRepository _productRepository;
	public Client(IProductRepository productRepository)
	{
		_productRepository = productRepository;
	}
	
	public void DoSomethingSpecial(Guid id)
	{
		_productRepository.GetById(id); // Will not compile
	}
}

Enjoy!


Simple WCF Client by contract and without svcutil

Sometimes your WCF based Sever and Client, both have access to the contract assembly (common approach in .NET Remoting, btw). There is good chance to reduce your code. Well, at least client code. Look at the following code:

public MyCoolService Create()
{
	var factory = new ChannelFactory<MyCoolService>(new WSHttpBinding());
	var address = new EndpointAddress("http://localhost/ MyCoolService.svc");
	return factory.CreateChannel(address);
}

As per name ChannelFactory is a factory. And it will create fully functional client. And yes, it works :).

In our application, we use WCF web-services for communication between components. This stuff made WSDL and auto-generated proxies redundant. And we dropped many code. I think this is good thing.

Enjoy!


Semantic meaning of the read methods

Personally for me, this is hard to understand what is the difference between GetOrder(id), ReadOrder(id), FindOrder(id) or RetrieveOrder(id). I am not native speaker and for me they look exactly the same.

All this methods just retrieve Order information by identifier of the Order. What information is missing is behavior in situations when Order was not found. For example method can raise exception or return something magical like null or “empty” Order.

So we have an API. It has methods like GetOrder(id). What will you do to make your code maintainable? You will check each result to ensure it does not null? You will insert Debug.Assert() after every call? You will leverage on NullReferenceException? You will write anti-corruption layer? You will use design-by-contract? Doesn’t this look a bit overcomplicated?

Well, I know simple and what is more important probably the most reliable solution. Never return null from your methods. That’s it.

The story is good, but, unfortunately, sometimes you have to. In this case, I would recommend simple naming conventions. For example:

The GetOrder is something strong. So it should throw exception if Order was not found.

The FindOrder is less strong, you can find but also can fail to find. So this kind of methods can return null.

You can argue that naming conventions actually does not resolve the problem. Yes and no. Theoretically - yes, it does not resolve the problem because everybody can break this. In practice - why one will even try to break this? Doesn’t he want to write maintainable code? Just make everybody aware about these conventions. It actually works.

BTW, for long read operations I user RetrieveOrder, for ad-hoc - QueryOrders, for paged results PageOrders, for UI lists - ListOrders.

Hope this stuff, does not conflict with real English :). Happy coding folks!


Thoughts on non-blocking sequential number generation

Right now we are facing a requirement to generate readable sequential numbers. Yeh… some Order Numbers, some Customer Numbers, that kind of stuff. Nothing new. The problem is that we cannot leverage on the standard patterns that use central authority. Having central authority always means huge distributed locks and this goes even worst in load balanced farms.

Fortunately, Igor has a brilliant idea. Simply setup constraint for the number column. Looks crazy? You should know some details before judge. First of all, in our system concurrent order creation is rare. We are processing system. So often we have batch of orders to create. Second our orders is numbered per context (tenant if you wish), and nodes in our farm is balanced per context. Less crazy?

The probability of the duplicate number is very low. But for sure “low”, does not guarantee that we want in our application. So at this stage we have physical constraint in the database. Whole unit-of-work will be discarded if something goes wrong.

In worst case process looks like cyclic retry.

1. Assign "probably good number";
2. Push it to database;
3. If fail, try to assign another "probably good number";
4. If success, commit;

At this point matrix is ended. And reality is starting. I failed to implement this in reasonable time frame… May be next time.

Sorry, nothing to enjoy. So just have a good day!


Code-generation DSL with T4 (Text Templates)

Sometimes you have to write many repeatable code. Good examples are data/message contracts for the web-services or data access layer. This often means writing dozens properties, classes … boring ….

One of the solutions to this problem is code generation. In short words you are taking some data, make transformation and as output you are getting code file in your favorite language. In the following post I will describe code generation with T4 (Text Templates) and DSL (Domain Specific Language) that will be the input data.

Demo Problem

For demonstration purposes I will use the real problem form my ongoing work. We are working on much componentized system. Each component has its own deployment requirements. To make deployments as flexible as possible we use .NET Configuration. The only problem with .NET Configuration is fact that 80% of the custom ConfigurationSection code is useless. Looks like a lie? Well, to prove please view here. This is regular section. Most of the stuff is duplicated. Anyway, let us return to problem.

So we need generate configuration section. The only data required as input is:

NameType
Serverstring
Databasestring
Usernamestring
Timeoutint

As you may see, most of the attributes have type string so generator should assume string as default type.

DSL

First of all we need empty T4 template (for details - T4 (Text Templates)”).

We will start with simple structure that holds support information like name of the .NET configuration section as well as list of the configuration attributes. This will be our DSL. Code worth a thousand words:

<#+
// DSL
public class Section{
	public string Namespace {get;set;}
	public string Name {get;set;}
	public string Path {get;set;}
	public IList<Property> Properties = new List<Property>();
}
public class Property{
	public Property(){
		Type = "string";		
	}
	public string Name {get;set;}
	public string Type {get;set;}
}

#>

We need to embed this DSL to the template, to do this we will use <#+ ... #> section. This is very similar to the ASPX code <script runat="server"> ... </script>.

Data defined with DSL

Next is actual data.

<# var section = 
	new Section
	{
		Namespace = "Community.Example",
		Name = "DataConnectionConfiguration",
		Path = "community/data",
		Properties = 
		{
			new Property{Name = "Server"},
			new Property{Name = "Database"},
			new Property{Name = "Username"},
			new Property{Name = "Timeout", Type = "int"}			
		}
	};
#>

As you can see, this is just variable and plain C# code. You also may noticed that there is no +. This is because this code is part of the actual template.

Template - Code Generation

So we have DSL, and we have data. Time to do actual code generation.

namespace <#= section.Namespace #>
{
	using System.Configuration;
	using System.Diagnostics.Contracts;

	public class <#= section.Name #> : ConfigurationSection
	{			
<# foreach(var property in section.Properties) { #>	
		[ConfigurationProperty(
			"<#= CamelCase(property.Name) #>",
			IsRequired = true)]
		public <#= property.Type #> <#= property.Name #>
		{
			...
		}
<# } #>			
	}
}

This is just markup code and includes of the control code. We have access to the variable section that holds our data, so we can do virtually anything.

Right now we have complete template. It generates .NET Configuration Section. You even can download the whole file from here. But wait a minute. We can make this stuff more useful.

Making stuff usable

Right now we have single file. That means when we will need another section, we have to copy this template (Copy Code Refactoring). Well, not really true. The T4 support includes. We can use it to separate template with DSL and actual data.

So at the end we will have 1 + n files. Where n is a number of sections you need:

File #1, ConfigurationSection.ttinclude

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly Name="System.Core.dll" #>
<#@ import namespace = "System.Collections.Generic" #>
<#@ output extension=".cs" #>
namespace <#= section.Namespace #>
{	
	// Bla-bla-bla, template goes here....
}
<#+
// DSL
public class Section{
	// Bla-bla-bla, DSL goes here...
}
#>
<#+
// Helper methods
private static string CamelCase(string input)
{
	// Bla-bla-bla, Helper methods goes here...
}
#>

And file #N, DataConnection.tt

<# var section = 
	new Section
	{
		// Bla-bla-bla, Data goes here...
	};
#>
<#@ include file="..\ConfigLanguage\ConfigurationSection.ttinclude" #>

F-e-e-e-w. That is all for now.

As always (second time) you can find result template and usage example on GitHub.

Let me know if this stuff was usefull.


ASP.NET MVC Web Services (Part #1)

What do you think about ASP.NET MVC as platform for web service based applications? What about JSON Web Services? Look at the following code:

public class OrderPorcessingController
{
	public OrderPromotionResult PromoteOrder(string orderNumber)
	{
		var orders = new OrderRepository();
		var order = orders.GetOrderByNumber(orderNumber);

		order.PromoteOrder();

		return new OrderPromotionResult
		{
			NewStatus = order.Status
		};
	}
}

public class OrderPromotionResult
{
	public OrderStatus NewStatus { get; set; }
}

This code is actually ASP.NET MVC Controller. The only notable difference is return type. I return custom object. Not inherited from ActionResult. For me looks good.

For example, I can test it without any ASP.NET MVC bindings:

public void Should_change_status()
{
	var processing = new OrderPorcessingController();
	var result = processing.PromoteOrder("TestOrder");
	Assert.Equals(OrderStatus.Promoted, result.NewStatus);
}

And at the end I have endpoint /OrderPorcessing/PromoteOrder that accept argument orderNumber and return JSON.

Cool?

Implementation

Under the hood ASP.NET MVC, when action return type not inherited from ActionResult wraps it’s ToString() to ContentResult. Not so useful on the client side.

Of course, ASP.NET MVC has extensibility point that exactly fits. Well, not exactly true. It has at least two such points. Custom ControllerActionInvoker and no less custom IActionFilter. In our production code we use custom IActionFilter, however this post is about Custom ControllerActionInvoker.

Custom ControllerActionInvoker

public class ServiceActionInvoker : ControllerActionInvoker
{
	protected override ActionResult CreateActionResult(
		ControllerContext controllerContext,
		ActionDescriptor actionDescriptor,
		object actionReturnValue)
	{
		ActionResult actionResult = (actionReturnValue as ActionResult) ??
			new JsonResult { Data = actionReturnValue };

		return actionResult ?? new EmptyResult();
	}       
}

Yes, it overrides CreateActionResult and wraps result to the JsonResult instead of ContentResult. That it.

To use this stuff you need to override CreateActionInvoker.

protected override IActionInvoker CreateActionInvoker()
{
	return new ServiceActionInvoker();
}

Enjoy!

P.S. Code with example could be found at GitHub.


The right way to do INNER JOIN in LINQ to Entity

Model Despite of my last belief, that code will not fly into the production. Why? Because I found the right way to do INNER JOIN in Entity Framework!

The problem I have to solve is the following:

I have regular Order model. A Customer has many Orders, an Order has many OrderItems, many OrderItems have the Product. I have to get all Products that given Customer have bought.

So The Right Solution

is the most easy code. And is actually the most correct one:

var r = from p in ds.OrderProducts
		from oi in p.OrderItems
		where oi.Order.Customer.CustomerID == customerId
		select p;

Why I did not thought about the simplest possible code? I do not know. Probably because of SQL background… And of course, resulting generated SQL:

SELECT 
...
FROM   [Invoicing].[OrderProduct] AS [Extent1]
INNER JOIN [Invoicing].[OrderItem] AS [Extent2] 
	ON [Extent1].[OrderProductID] = [Extent2].[OrderProductID]
INNER JOIN [Invoicing].[Order] AS [Extent3] 
	ON [Extent2].[OrderID] = [Extent3].[OrderID]
WHERE cast('f925c22b-8379-4dd9-a1f6-9c8f0113f235' as uniqueidentifier) 
	= [Extent3].[CustomerID]

Pretty? Yes! This code is equal to one I wrote myself!

Hope this eventually helps :)


Application Services, to be or not to be

This entry is dedicated for people who already use Application Services Pattern (most commonly referenced as Service Layer Pattern).

RIP of the Application Layer

You are probably aware about role Application Services layer plays in modern architectures. And I am aware. And this why Application Services Pattern was my default pattern for ages. I used to use it with domain driven applications (DDD), data driven (with business logic in so called managers) and even in distributed, where all logic was on the server.

But, time is going and I started realizing that Application Services layer lacks its responsibility. Strong sign of this is when you do not know how to name tests for particular unit. In my case I was failing to name tests that just delegate calls from UI to Domain Entities. What tests should test? Call delegation? Level of abstraction? This all is not functional!

Anyway, let return back to Application Services. In last project we are using MVC pattern for UI part and DDD for domain logic. For wiring them up together, we used what..? Yes. Application Services. Diagrams look cool. Good separation of layers. Looks great! But. Price of this is thousands of useless code. Useless tests. Useless mocks. Half of the code to implement use case was useless for this use case! Even worse, refactoring existing use cases are also hard. Imagine how much stuff you need just to add argument to domain service?

So after hard thinking, we dropped Application Services. We decided to go the pragmatic way. When we will need it, we will implement it. But not earlier.

Hope this makes sense!


Data Access approaches in our application

We are building regular enterprise application. The Domain is boring. The only interesting thing is technologies. This night is a good time to share how we do Data Access.

NHibernate

We are approaching DDD. So we need POCO and NHibernate is good choice. The only problem with NHibernate is mappings. XML mappings makes me crazy. To overcome this we use FluentNHibernate, in particular it’s Auto-Mapping features. All is convention based, so adding new entity does not require any further configuration.

LINQ to SQL (Linq2Sql)

One of the problems with NHibernate is support for SQL UDFs(User Defined Functions). This is hard to get them work. Even harder to get them configured in AutoMapped way. So in situations when we need call UDF(User Defined Function) we just make hand written LINQ to SQL context.

private class ProductFunctions : DataContext
{
	public ProductFunctions(IDbConnection connection)
		: base(connection) { }

	[Function(IsComposable = true)]
	public IQueryable<ProductListItem> QueryProductList(Guid contextId, 
		string productNumberFilter, string titleFilter,
		string SKUFilter, Guid? productTagIDFilter)
	{
		return CreateMethodCallQuery<ProductListItem>(this,
			((MethodInfo)(MethodBase.GetCurrentMethod())),
			contextId, productNumberFilter, titleFilter,
			SKUFilter, productTagIDFilter);
	}
}

Name of the UDF(User Defined Function) will be the same as name of the method (e.g. QueryProductList). If you need custom name, you can do this with FunctionAttribute. Pretty easy. And seems to me this is the less overheated solution for calling functions.

BTW, the ProductListItem is real POCO. Without attributes, without virtual properties. Just class as how you expect to see it.

Entity Framework

While building User Interface, I started feeling that we are doing too many coding. Customer need list for this, customer need list for that, and so on and so forth. From coder perspective every list needs DTO, parsing arguments, doing some filtering. Of course, we need way to automate this. One of the solutions is to use ADO.NET Data Services. So now we have Entity Framework Model, and then publish it with ADO.NET Data Services. Works as a charm.

That is it!

As you can see, we try to solve problems with tool that is most applicable in particular situations. I think this is good example of the positive experience not to be strictly MS guy or Alt.NET guy. Let us see how this will work in real live.

Happy coding!


Always call IDisposable.Dispose! Sure? No!

Sometime ago there was discussion on StackOverflow about BinaryWriter implementation. As you may know BinaryWriter and all other .NET Wrtiers implement IDisposable. You also might be aware about guidance to always call Dispose. Boring…

using(var s = File.OpenWrite("1.txt")){
	using(var w = new StreamWriter(s)){
		...
	}
}

The question is… Why? So you have Stream and it has resources to dispose. Then you have Writer… And… It does not have resources to dispose. Why to dispose then? I do not know.

So why they implement IDisposable? I think about this as a marketing for newbie developers. Most examples in MSDN looks like:

using (var sw = new StreamWriter("1.txt")) {
	...
}

Without IDisposable this code will fail to close handlers to file. For me this is just leaked design of the StreamWriter.

At the end of the day, I can formulate my IDisposable usage.

By default, always call Dispose. But if you sure what are you doing - do not call!

How to ensure that call to Dispose is not required? Following is my checklist:

  1. Think about usage semantics. In most cases, this gives 90% of sure;
  2. Use .NET Reflector. It can unhide some stuff you may not aware about;
  3. Read documentation. This also can help to decide;

Anyway, add comment to the code. This will be helpful for maintainers and will reduce questions during reviews.

And remember:

  1. Dispose it contract. If one marking code with this contract, for sure he want to say something;
  2. Things changing. Behaviour of the Dispose could also change. You will have nothing to say, when your code will not work on .NET 5.0;

BTW

  1. DataSet implements IDisposable. This is because of usage in .NET Remoting and Design Time support. In addition DataTable, DataView and even DataColumn are the same. And no they do not clean data;
  2. SqlCommand and everything derived fromDbCommand. It does exactly nothing. Why it have? What resources it suppose to own?
  3. Something not expected at all - ‘SqlCommandBuilder’. What is expected Dispose do nothing notable here;
  4. Each Component implements IDisposable. Even if does not own resources. Think about WinForms BindingSource, ‘ErrorProvider’;
  5. Ha!, IEnumerator<T> is also here! This is dark side!
  6. And at the end WCF Clients has “broken” IDisposable implementation;
  7. Some stuff could use, IDisposable as syntax sugar.

Enjoy! Happy coding!


History of the one LINQ to Entities query

From last week, our approach to reporting has been changed. Now we use ADO.NET Data Services on top of the Entity Framework Model. Today was the first day, when I was not able to express query in terms of the ADO.NET Data Services query options.

Often this is not a problem because Data Services supports awesome feature - Service Operations. Even more exiting that methods return a IQueryable<T> automatically get support for all query options like $filter and $orderby. So what I need is some code that will retrieve required data and then return it as a IQueryable<T>.

The problem

Model

I have regular Order model. Customer has many Orders, Order has many OrderItems, many OrderItems have Product. Pretty common.

I need to retrieve all Products that Customer bought. With SQL, it is pretty easy with few joins. What about LINQ?

Short #1. Direct translation of the SQL to LINQ

Well, it might look as the following:

var r = from c in ds.Customers
	join o in ds.Orders on c equals o.Customer
	join oi in ds.OrderItems on o equals oi.Order
	join p in ds.OrderProducts on oi.OrderProduct equals p
	where c.CustomerID == customerId
	select p;

Here ds is Entity Framework context. Very familiar. Imagine my disappointment when I saw stuff generated by Entity Framework.

exec sp_executesql N'SELECT 
	...
FROM    [Invoicing].[Customer] AS [Extent1]
INNER JOIN [Invoicing].[Order] AS [Extent2] ON  EXISTS (SELECT 
	cast(1 as bit) AS [C1]
	FROM    ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
	LEFT OUTER JOIN  (SELECT 
		[Extent3].[CustomerID] AS [CustomerID]
		FROM [Invoicing].[Customer] AS [Extent3]
		WHERE [Extent2].[CustomerID] = [Extent3].[CustomerID] ) 
			AS [Project1] ON 1 = 1
	...
	WHERE ([Extent1].[CustomerID] = [Project1].[CustomerID]) 
		OR (([Extent1].[CustomerID] IS NULL) 
		AND ([Project2].[CustomerID] IS NULL))
)
INNER JOIN [Invoicing].[OrderItem] AS [Extent5] ON  EXISTS (SELECT 
	...
)
INNER JOIN [Invoicing].[OrderProduct] AS [Extent8] ON  EXISTS (SELECT 
	...
)
WHERE [Extent1].[CustomerID] = @p__linq__1',N'@p__linq__1 uniqueidentifier',
	@p__linq__1='F5DD85CF-CE1E-E2D1-3171-650938ABD2B7'

Download full script if you interested in. The Execution Plan was terrible. Just terrible. I can send this plan as example of plan to avoid….

Of course, this is not acceptable. So next:

Short #2. Reversed order of the joins

May be change of the order will help?

var r = from p in ds.OrderProducts
	join oi in ds.OrderItems on p equals oi.OrderProduct
	join o in ds.Orders on oi.Order equals o
	join c in ds.Customers on o.Customer equals c
	where c.CustomerID == customerId
	select p;

No ;(. So next:

Short #3. Lets reduce joins

When doing LINQ, it’s probably worth to remember that you have dot notation. Dot notation is pretty powerfull. So I just rewrote where clause to reduce 75% of my joins.

var r = from p in ds.OrderProducts
	join oi in ds.OrderItems on p equals oi.OrderProduct
	where oi.Order.Customer.CustomerID == customerId
	select p;

Looks better? Sure. Generated stuff also looks much better.

exec sp_executesql N'SELECT 
...
FROM   [Invoicing].[OrderProduct] AS [Extent1]
INNER JOIN [Invoicing].[OrderItem] AS [Extent2] ON  EXISTS (SELECT 
	cast(1 as bit) AS [C1]
	FROM    ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1]
	LEFT OUTER JOIN  (SELECT 
		[Extent3].[OrderProductID] AS [OrderProductID]
		FROM [Invoicing].[OrderProduct] AS [Extent3]
		WHERE [Extent2].[OrderProductID] = [Extent3].[OrderProductID] ) 
			AS [Project1] ON 1 = 1
	LEFT OUTER JOIN  (SELECT 
		[Extent4].[OrderProductID] AS [OrderProductID]
		FROM [Invoicing].[OrderProduct] AS [Extent4]
		WHERE [Extent2].[OrderProductID] = [Extent4].[OrderProductID] ) 
			AS [Project2] ON 1 = 1
	WHERE ([Extent1].[OrderProductID] = [Project1].[OrderProductID]) 
		OR (([Extent1].[OrderProductID] IS NULL) 
			AND ([Project2].[OrderProductID] IS NULL))
)
INNER JOIN [Invoicing].[Order] AS [Extent5] 
	ON [Extent2].[OrderID] = [Extent5].[OrderID]
WHERE [Extent5].[CustomerID] = @p__linq__1',N'@p__linq__1 uniqueidentifier',
	@p__linq__1='F5DD85CF-CE1E-E2D1-3171-650938ABD2B7'

Only one crazy INNER JOIN. BTW, possible that I am not aware of something. I do not understand why Entity Framework generates INNER JOIN with EXISTS in response to LINQ joins. My expectation was just INNER JOIN. Need to spend some time to understand this. Anyway, the Execution Plan could be compared to manual.

After such success, I decided to go further. So next…

Short #4. What about JOIN by ID?

var r = from p in ds.OrderProducts
	join oi in ds.OrderItems 
		on p.OrderProductID equals oi.OrderProduct.OrderProductID
	where oi.Order.Customer.CustomerID == customerId
	select p;

Damn, this works!

exec sp_executesql N'SELECT 
...
FROM   [Invoicing].[OrderProduct] AS [Extent1]
INNER JOIN [Invoicing].[OrderItem] AS [Extent2] 
	ON ([Extent1].[OrderProductID] = [Extent2].[OrderProductID]) 
		OR (([Extent1].[OrderProductID] IS NULL) 
		AND ([Extent2].[OrderProductID] IS NULL))
INNER JOIN [Invoicing].[Order] AS [Extent3] 
	ON [Extent2].[OrderID] = [Extent3].[OrderID]
WHERE [Extent3].[CustomerID] = @p__linq__1',N'@p__linq__1 uniqueidentifier',
	@p__linq__1='F5DD85CF-CE1E-E2D1-3171-650938ABD2B7'

Now LINQ query looks not so cool. Hm. Entity Framework knows about primary keys. Why it cannot optimize this? I could not live with these results. So next:

Short #5. Without joins at all. Projections

We can remove last join with simple projection.

var r = from oi in ds.OrderItems
	where oi.Order.Customer.CustomerID == customerId
	select oi.OrderProduct;

And yes, it works. Now Entity Framework generates this:

exec sp_executesql N'SELECT 
...
FROM   [Invoicing].[OrderItem] AS [Extent1]
INNER JOIN [Invoicing].[Order] AS [Extent2] 
	ON [Extent1].[OrderID] = [Extent2].[OrderID]
LEFT OUTER JOIN [Invoicing].[OrderProduct] AS [Extent3] 
	ON [Extent1].[OrderProductID] = [Extent3].[OrderProductID]
WHERE [Extent2].[CustomerID] = @p__linq__2',N'@p__linq__2 uniqueidentifier',
	@p__linq__2='F5DD85CF-CE1E-E2D1-3171-650938ABD2B7'

Of course, execution plans look OK.

So can we stop here? No. At least not quite. The problem that all this queries are wrong… They do not distinct products. So next:

Short #6. Intuitive where clause

Believing in the power of the Entity Framework, I decided to write everything into the where clause:

var r = from p in ds.OrderProducts
	where p.OrderItems.Any(oi => oi.Order.Customer.CustomerID == customerId)
	select p;

Result

exec sp_executesql N'SELECT 
...
FROM [Invoicing].[OrderProduct] AS [Extent1]
WHERE  EXISTS (SELECT 
	cast(1 as bit) AS [C1]
	FROM  [Invoicing].[OrderItem] AS [Extent2]
	INNER JOIN [Invoicing].[Order] AS [Extent3] 
		ON [Extent2].[OrderID] = [Extent3].[OrderID]
	WHERE ([Extent1].[OrderProductID] = [Extent2].[OrderProductID]) 
		AND ([Extent3].[CustomerID] = @p__linq__1)
)',N'@p__linq__1 uniqueidentifier',@p__linq__1='F5DD85CF-CE1E-E2D1-3171-650938ABD2B7'

I am fully with these results. So this code will fly in production.

Conclusions

  1. Do not think in SQL terms when doing LINQ
  2. Do check results
  3. Reduce join clauses. Where clause has enough power
  4. Use projections. Entity Framework support them

Enjoy!

UPDATE Unfortunately, this post does not provide right solution. Please see The right way to do INNER JOIN in LINQ to Entity.


Make your DbPro generated test data look better

Few days ago I was tasked to fill database with millions records of data. We have to stress test our application and to define initial SLA. We use DbPro (Visual Studio Database Edition) to maintain our database schemas. Of course, I started with auto-generated strings. Looks terrible….

Terrible generated data.

Next idea was to use my DbPro Generators. Well, it could work for some fields. But… it does not work in my environment (64bit or Visual Studio SPs, I do not know).

Next smart idea was to use data we already have in databases. With data bound generators it could work. But… it is too hard to use. You have to move data, you have to sanitize it. And at the end you need to write queries for each field. Manually!

So pls, welcome the smartest idea! I decided to give a try to Regular Expression Generator. After some cracking of the Regexes, I ended with pretty readable results.

Pretty generated data.

Following is Regex Expressions I used to generate my data. First and Last names are bit Slavonian, but you can contribute them in your language. I will add them to the list.

Goal Regular expression
Email Address (admin|support|test|info|user|[a-z]{3,12})@[a-z]{3,12}((\.com)|(\.net)|(\.co\.uk)|(\.ua))
Website Address ((http|https)://){0,1}www\.[a-z]{3,12}((\.com)|(\.net)|(\.co\.uk)|(\.ua))
Comapany Name ((Power)|(Super)|(Greate)|(Just)) [A-Z][a-z]{3,9} ((LTD)|(Inc)|(Corp))
Last Name [A-Z][a-z]{3,9}(chenko|ko|ov|ev|ns|er|th|il)
First Name (Sachko|Mikhailo|Bogdana|Viktor|Varvara|Neo|Alisa|Mira|Ludmila|[A-Z][a-z]{3,9})
County/Town (London|Kiev|Khrakiv|Paris)
Address [A-Z][a-z]{8,15} ([A-Z][a-z]{8,15}){0,4}( Av| Str){0,1}
Postal Code [0-9]{3,4}-[0-9]{4,5}
Country (UA|UK|AZ|FR)
Telephone Number \+[0-9]{1}([0-9]{3})-[0-9]{3}-[0-9]{4}
Order Number ORD[0-9]{8,8}
Lorem Ipusm (Lorem|A|Amet|Dius)( ipsum){0,1},((a|A|ac|accumsan|bibendum |blandit|conubia|dolor|eni|ipsum|laoreet |lorem|mauris|nunc|odio|senectus|urna|ut|velit|viverra|\.) )*

Enjoy!


Stuff to know when starting with ADO.NET Data Services

This post is dedicated for those who are starting with ADO.NET Data Services (formerly known as Project “Astoria”). So let us start.

You made everything how tutorial says. You checked every single line of code. But you still get:

The server encountered an error processing the request. See server logs for more details.

Probably you also have checked everywhere but failed to find these logs. If not, even do not try. This is waste of time. Anyway, solution is simple. First alter InitializeService method:

config.UseVerboseErrors = true;

And second is ServiceBehavior attribute with IncludeExceptionDetailInFaults:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]   
public class ServiceName : DataService<ExampleContext>{
	...
}

You need to do both of this. In fact I spent some of my weekend time to understand this.

Go further. Next super intuitive exception message is:

The property ‘Xxxx’ on type ‘Xxxx.Xxxxx’ is not a valid property. Properties whose types are collection of primitives or complex types are not supported.

Had I mentioned that for some reasons neither of these exceptions break Visual Studio in debug mode? It is true. Anyways, reason and solution by Pedram Rezaei. The reason is Data Services are not so clever (or simply stupid) to understand how to map complex types. So you have to help them. You have to add another root for this complex type. Suppose your Xxxx was Order. Then you have to alter your context with:

public IQueryable<Order> Orders { get { return /*Somewhere*/ } }

Should work! Well, not really! Go further:

The property ‘Xxxxx’ on type ‘Xxxx.Xxxx’ is not a valid property. Make sure that the type of the property is a public type and a supported primitive type or a entity type with a valid key or a complex type.

This is about enums in your class. Drop them out! YAGNI! You aren’t gonna need enums!

The property ‘EntityState’ on type ‘Xxxx.Xxxx’ is not a valid property. Make sure that the type of the property is a public type and a supported primitive type or a entity type with a valid key or a complex type.

Image stolen from Heavy Metal Nutcracker(http://www.mocha.uk.com/shop/index.cgi?command=moreinfo&search=TT09)

This occur when you try to use entities generated by Entity Framework in non Entity Framework context (context not derived from ObjectContext). As you may know, in Entity Framework your entities have to derive from EntityObject and this guy has enum….

Under the hood, ADO.NET DataServices is hardcoded (yes, believe or not, but it really hardcoded!) to look if context derives from ObjectContext, and if true just filter out infrastructure properties. The only solution so far is to use IgnoreProperties. Both the reason and a “solution” came from MSDN Forums. BTW, inheriting context from the ObjectContext does not work.

Are you still here? Well, my journey ended on last issue. I cannot live with it. This is enough for today. Tomorrow I will start with another approach. Hope my boss will not kill me for 6 wasted hours.


My old-old code/projects. The landing page

From time to time it happens, some code become outdated. And I have some of the such code. This post is all about such code/projects.

This post, actually, serves two purposes. Main purpose is to be landing page for the no longer existing projects. Of course, “no longer” is not quite true, they exist. Well, at least in form of the source code. So if something got your attention, do not hesitate to contact me, I will be happy to send you source code or binaries.

Second purpose is actually to list old and outdated projects. I will update this post from time to time, with new outdated code.

Project NameDescriptionAvailability
XML Updatable Data SourceAs per name this is implementation of the ASP.NET IDataSource similar to XmlDataSource, but with editing compatibles.Source code
SQL Hierarchical DataSourceCustom implementation of the ASP.NET IHierarchicalDataSource for any ADO.NET Provider (SQL, Oracle, OleDb, ODBC)Source code
Rebar ToolStrip RendererCustom renderer for standard .NET Toolbar(ToolStrip). Renders toolbar similar to IE6. Uses themes API.Source Code
VS2005 ToolStrip RendererCustom renderer for standard .NET Toolbar(ToolStrip). Renders toolbar similar to Visual Studio 2005.Source Code
Google SitemapThe HttpHandler to render standard sitemap, based on data from ASP.NET Sitemap.Source Code
XML Membership ProviderRead-only Roles/Membership provider for XML data.Source Code
DbPro GeneratorsProject provides Lorem Ipusm Generator for Visual Studio Database Edition.Codeplex
SAWSDL Implementation for WCFAim of the project is to provide SAWSDL implementation for WCF.Codeplex
Syndication ExtensionsImplementation of the Media RSS Module.Codeplex

Do not hesitate to contact me, I will be happy to send you source code or binaries.


Read all lines of the file with Java, Ruby and C#

UPDATE: As of Dec 2009 C# provides analog of the ForEach method out of the box, you can read more here.

Yehuda Katz, in his post ”My 10 Favorite Things About the Ruby Language”, provides example of the reading all lines of the file, with Java and Ruby.

Java is a bit wordy:

static void run(String in) 
	throws FileNotFoundException {
		File input = new File(in);
		String line; Scanner reader = null;
		try {
			reader = new Scanner(input);
			while(reader.hasNextLine()) {
				System.out.println(reader.nextLine());
			}
		} finally { reader.close(); }
}

Ruby is much better:

def run(input)
	File.open(input, "r") do |f|
		f.each_line {|line| puts line }
	end
end

But what about C#? Look.

static void Run(string inp){
	Array.ForEach(File.ReadAllLines(inp), Console.WriteLine);
}

The only thing I do not like with this code is a fact that I cannot read it as a plain English sentence. This is because, for some reasons, Array.ForEach was not defined as extension method. Fix is quite easy. Lets add small extension to IEnumerable<T>:

using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;

public static class EnumerableExtensions
{
	public static void ForEach<T>(this IEnumerable<T> input, 
						Action<T> action)
	{
		Contract.Requires(input != null);
		Contract.Requires(action != null);

		foreach (var item in input)
		{
			action(item);
		}
	}
}

And then rewrite results:

static void Run(string inp){
	File.ReadLines(inp).ForEach(Console.WriteLine);
}

BTW, in contrast to first C# example, second one will read lines one by one.

UPDATE: As of Dec 2009 C# provides analog of the ForEach method out of the box, you can read more here. So code could be rewritten in this way:

static void Run(string inp){
	File.ReadLines(inp).Run(Console.WriteLine);
}

Have a nice day!

P.S. I am not quite sure, that provided is the best solutions in Java and Ruby. The purpose of this post is to illustrate C# abilities, not to find disabilities of others.


T4 (Text Templates) - The Beginning

This is the first post in my ongoing series about T4 (Text Templates). I will describe basics to ensure that we are talking on the same language. If you know what T4 is, just skip this stuff.

What is that?

The Text Templates or simply T4 is general purpose text templates that comes out of the box with Visual Studio (starting from Visual Studio 2008). Text Templates look and behave very similar to the ASPX, ASP or even PHP files. Both are just templates that allows code included in. Unlike ASPX, T4 does not allow code behaind.

How to start?

Easy. Create text file with extension .tt and content:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly Name="System.Core.dll" #>
<#@ output extension=".cs" #>
class Test
{
	public void DoSomething()
	{
		System.Console.WriteLine("Generated at {0}", 
			"<#= DateTime.Now.ToString() #>");
	}
}

Press Ctrl+S and now you can see generated code.

Generated results

Thats it, you have done with your first Text Template.

BTW, the Visual Studio 2010 already comes with templates for Text Templates (funny, yeh?) out of the box. For the Visual Studio 2008 you can use custom T4 templates.

Where to read more?

Generating Artifacts By Using Text Templates - Text Templates landing page on MSDN

Oleg Sych, Site Archives T4 - awesome blog about T4, for example - Pros and Cons of T4 in Visual Studio 2008.

T4 on Patterns and Practices Guidance - Many examples

What next?

I already have drafts about building DSL with T4. Also I have some tips to share. So let hope to see something interesting.


This post in Ukrainian - T4 (Visual Studio Text Templates) - The Beginning


Virtual by default, is that good or not?

Inspired by background of the `Jimmy Bogard`'s - My favorite NHibernate exception.

Here, at Just Applications, we are using Google Web Toolkit (GWT) as our client side framework. Personally for me that means that last few months I spent in average 30% of my work time coding with Java. After years of C# coding this is freaking awesome experience!

BTW, I am sure that there are just two real ways to learn stuff. One is red pill and second is to use stuff in real work.

Of course, been C# developer, I found stuff I do not like in Java. For example, unlike in C# where class members are sealed/final by default, in Java they are virtual by default.

So let start with some myths.

Myth 1. It’s all about testability

Mythbusters

I know only two places where virtual members could help. One is stubbing/mocking and second is trying to change behavior of the SUT.

So, stubbing… In most cases, stubbing requires classes not to be sealed/final and so for members you would like to override. This is due the fact that stub is often inherited from your class. Therefore having everything virtual is direct solution! It could be true. Unless you are okay to have default constructor on your class. Unless you are agreeing that small change to our code can break half of your tests… From my experience, both of these problems kill an idea of stubbing real implementations. Solution to these problems is obvious. Just extract interface and do all stubbing around it.

Next is changing behavior of the SUT. Sometimes when class was not designed with testing in mind you have to check indirect results or you have to do something tricky to isolate code. This is often referred as testing legacy code. Of course, in this situation making everything virtual could help. Actually even more help you can get after making everything you need public. But hey! This is anti-pattern for regular testing! This is leakage of the implementation details into the test!

So at least for me testability is not an argument in the Great Virtual Battle!

Myth 2. It’s all about extensibility

This is my favorite! This is about extensibility though inheritance!

So, I have written some code. Why to restrict usage of the class by making it sealed? Why not to support freedom?

Well, there are some facts:

  1. Sealed as well as privates and internals helps you to provide useful API. They just hide stuff not required for API. When members of a class did not intend to be overridden, why not to restrict this at compilation time?

  2. When you design API, you probably should think how to make usage as obvious as you can. Inheritance is yet another usage. If your class will have everything virtual, how your user will know what is expected to be overridden?

  3. Your code should be tested. But please imagine how many invariants your will have if everything will be overridable? This is just hard to design inheritance API.

Main point of these items is that if you allow something, you should support this. I prefer not to spend time to support invariants that will not occur with 99% probability.

Solution is not obvious, but generally well known. It is Composition. Let your classes to be composable in a way your user would like.

BTW, did you ever extend DataGrid? I did. It was most terrible experience I ever had. You override something, and some stuff stop working, you override something else, and another unexpected stuff stops working...


Dependency Injection versus Dependency Inversion

From time to time, I hear questions what is the difference between Dependency Injection (DI), Dependency Inversion (also DI), Inversion of Control (IoC) and so on. Despite you can make decision yourself, I will gather all related stuff in this post. Also check my recommendations. Hope you like them.

The Dependency Inversion Principle

As per title, Dependency Inversion is The Principle. Means that this is just theoretical stuff and this is your responsibility to invent how to implement it. The original publication (at least I think so) was at Object Mentor - The Dependency Inversion Principle.

Publication defines this principle in two sentences:

High level modules should not depend upon low level modules. Both should depend upon abstractions.

Abstractions should not depend upon details. Details should depend upon abstractions.

As you can see, it is very general. Whole bunch of patterns helps to solve this problem.

It is recommended to use in high-level design battles. It could help to bring down your opponents.

The Dependency Injection Pattern

Yes-yes, it’s pattern. This is one of the concrete solutions to the application/code composition problem. And yes this solution is implementation of the Dependency Inversion Principle. Probably the most known place to read is Martin Fowler - Inversion of Control Containers and the Dependency Injection pattern.

You can use this term in battles with ruby fans. This is something that most of the ruby developers do not aware about. It is common in .NET word so you can look trite and predictable.

The Inversion of Control

There is no clear answer what the Inversion of Control is. Some people say this is the principle, some people - the pattern. Because name does not suggest implementation, I prefer to refer this as the principle. Anyways this is just another name of the Dependency Inversion Principle or the Dependency Injection Pattern, respectively.

Because this term is common in Java World, you can use it in battles with .NET guys. Also you can use when not sure what you need the principle or the pattern. Not to mention, it sounds way cool.

Hollywood Principle

The Hollywood Principle is another name of the Inversion of Control Principle.

Don’t call us, we’ll call you.

Simple!

Well, if you want look as solid, proven architect this term is for you. It sounds very antique. Do not forget to quote "Don't call us, we'll call you."

The Inversion of Control Container

It is hard to not mention the Inversion of Control Containers. This is common name of the libraries that implement the Dependency Injection Pattern. Why Inversion of Control? Probably because of the history.

The only usage is at work.

The Service Locator Pattern

The Service Locator is another cool stuff. For some reasons many people think that there is no relationship between Service Locator and DI. Not true. The Service Locator is yet another implementation of the Dependency Inversion Principle. And yes Service Locator is competitor for the Dependency Injection Pattern.

It is hard to believe but I cannot recommend mentioning this pattern at all. This pattern is only for gourmets.


Templates for Templates. T4 (Text Template) Template for the Visual Studio 2008

Ha-ha! Funny thing, there are no templates for the T4 (Text Templates) in the Visual Studio 2008! IMHO this the main reason why most of the developers not aware about them. Well, it’s easy to fix. Following is Visual Studio Installer with single ‘C# T4 template’.

Download T4 (Text Template) Template for Visual Studio 2008


Installer

BTW, I hope someday MSFT will implement normal editor features for the T4. Unfortunately, for now even Visual Studio 2010 is stupid notepad.


Today's information magic, stolen in 5 minutes

12:26 at Twitter

@bubbl: Tigris.org has released ArgoUML 0.28.1, an open source UML modeling tool written in Java. http://ff.im/-6VV4S

12:27 at http://www.cafeaulait.org/

Book Beautiful Code on the sidebar. Interesting.

12:27 at http://oreilly.com/

Book Beautiful Code - Description. Well, seems good reading…

12:28 at Twitter

@chaliy: 1 month without books… I need to start read again. “Beautiful Code” is good start - http://bit.ly/8C9mx

12:30 at Skype

xxxx: I have 0596510047_OReilly.Beautiful.Code.Jun.2007.pdf, do you need one?

12:30 at Skype

Download completed.

12:31 at Mesh

The file synchronized to all my computers.

12:31 just here

Back to work…


P.S. Sorry for using pirated copy of the book. Also note that xxxx is not real name :).


DDD: Not in love with the Domain Services! Hope I am not along!

Not so kind to be angry for the first post… 100 excuses!

When approaching the DDD some of the model code goes to the Domain Services. IMHO, this is some sort of “allowed” or “acceptable” solution when you cannot do something in pure model. Examples are access to the external services and operations on the multiple entities. You know.

The following article describes what is wrong with Domain Services.

No single place to start (or to learn)

Imagine you have the Order.Approve() and the ApprovalService.Approve().The ApprovalService do some stuff and then delegate actual approve to the Order. You never can be sure whatever you use right method! If you are not author of the both, you can easily miss the ApprovalService and just directly call the Order. Any solution that comes to the mind does not guarantee results or introduces much more problems than actually solves!

Dependency/Reference Hog

The main goal of the Domain Services is to “orchestrate” business operation. Often that means huge amount of external services, references out of the boundaries, access to the different partitions of your model. You can review the uses section of your services. Sometimes it looks terrible. Do you see a problem now?

Well, the solution is obvious. To make tools like NDepend happy, just use composition. This will likely make your uses section slightly smaller. However, problem is still here. The service is still complex and you cannot do something with it.

Testability

Yes, sure you use the DI so you can test all your code. But, please compare your Domain Service tests with Model tests. What I look here: Model tests are just instantiation of the entity, doing something and checking results. Everything like introduction tests in books on TDD. Tests for the Domain Services are slightly different. They require the mocks for the dependencies. They require assertions of the indirect output. For sure, I like model tests much more!

What to do then?

In most cases, this depends what you want to overcome (I you want of course).

For example to overcome problem with the API spread across the model, you can introduce Service Layer Pattern. Another way is to mark everything you are not expect be public as internal or implement them as explicit interfaces.

For dependencies and testability, I can suggest making Domain Services as stupid as you can. For example, move some code to the next layer (Service Layer Pattern).

Not to mention Domain Events (Udi Dahan’s version). This is very promising Domain Services killer. For now, it is my default approach. I will write more on this in near feature.


Welcome!

To be honest, main intent of this entry is not to welcome you… This would be plain boring… Isn’t it? Rather it for the testing purposes. Plus I am not sure that my blog can handle empty pages.