Sadek Drobi’s Blog

June 26, 2009

A monad in C# for simplifying WPF multi-threading for a more responsive GUI

Code included here is over simplified for clarity, I hosted a better implementation code on CodePlex. These modifications change strictly nothing for the client code and are only an implementation detail. I use a continuation rather than a delay, and I chose to design a custom continuation class rather than using a delegate because of a type system limitations.

DSC_2468Most GUI frameworks, including Silverlight and WPF, are shipped with a fundamental problem: long use of the main thread causes the Window to blackout, and using different threads requires you to get your hands dirty with the Dispatcher stuff and freezable objects. Worse, you wont learn the necessity to do so until you get a surprise of “The calling thread cannot access this object because a different thread owns it.” exception when all what you were doing is to use available methods on an object that seemed you have access to, at lease it seemed until runtime! This post illustrates a solution based on Monads abstraction and LinQ syntax.
(more…)

March 29, 2009

Abstraction for People: Configurations, Patterns, DSLs and Monads

DSC_2692a3

LinQ is often understood in terms of introducing a Domain Specific Language to work with data to C# and .Net in general. The fact is:it is not, and there is a considerable difference between LinQ syntax nature and a DSL. The problem is that DSL definition is blur enough to take anything interesting or cool under it!

(more…)

March 5, 2009

Erik Mejier: LinQ, Beyond List Comprehensions in C# and .Net

Filed under: DOTNET, DSL, Functional Programming, Haskell, InfoQ, LinQ — Sadache @ 9:55 pm

Erik Meijer talks about less known LINQ features, like meta programming, about the differences between functional languages and OO ones, asynchronous computation, and others.

(more…)

February 3, 2009

Quiz:: SharpLight |> What does this do?

combination

January 19, 2009

Quizz:: SharpLight |> What does this do?

Filed under: DOTNET, DSL, F#, Functional Programming, Haskell, Productivity, sharplight, web 2.0 — Sadache @ 10:40 am

div <<
          a "sadekdrobi.com"  
                   << img "http://is.gd/gqVX"
          ++ "my lil’ friend"

January 4, 2009

Who told Java Checked Exceptions were a Bad Idea?

Filed under: C#, DOTNET, Decipline, J2EE, Java, ORM, Spring, Spring Framework — Sadache @ 12:50 am

DSC_0285

One of the few things that lack seriously, IMO, in C# is the power of Java checked exceptions. In Java world? things happen weirdly. Maybe it is a result of crowd intelligence and a bad side effect of open source. Often, people in J2EE world tend to believe the opposite. Anyway you see the effect in most preferred frameworks (such as today’s popular ORMs and Containers) of wrapping checked Java exceptions and rethrowing them as runtime ones.

(more…)

November 10, 2008

RefX:: ORMs, Relational Data, Mismatch, LinQ and DSLs

Raw00078 Raw00170 DSC_0191

Having worked with several Object-Relational mapping frameworks in the last few years, I got to a point where I couldn’t justify their complexity in my project. We often talk about the mismatch between the database and the object worlds, and that is where ORMs are often stated and referenced for “bridging the gap”!

Well I prefer to call it lifting the gap, or highering the gap, to have it now between DAOs and the rest of the code than having it between database and code.But I wouldn’t call this in any way reducing the gap.

(more…)

October 28, 2008

Ted Neward on Present and Past Languages

This is an interview I did at QCon with Ted Neward. Talking to Ted was very interesting even though arguing with him turned to be not easy at all :)

June 1, 2008

And you get all the VM libraries for free! Is it actually what I want when I switch languages?

DSC_0459 So it is getting more and more interesting. A vast number of languages available. And they are even available on your favorite platform. So you may not need anymore to beg for a process of changing a deployment environment to change your programming tools. It is all changing, and you may now start thinking about what language or paradigm suits best the domain or even the sub domain you are trying to model.

Everyday we hear about languages implementations on the JVM or the CLR. Every language has its own taste and semantics. All of these implementations try the best to satisfy an important second promise which is leveraging the preexisting platform libraries and giving an easy access to them from within that language. So on the CLR you get a promise of an easy access to all the .NET framework, and from Java, well, to the J2EE enormous libraries.

Well designed .Net libraries might be bad designed F# libraries

Some days ago I was trying to solve a small problem using F# (a new member to the .Net family). The reason I chose F# for this problem is the functional paradigm, my specific problem was easier to model using functional paradigm than any other paradigm. What I noticed while using F# is how easy it was to access the large .Net libraries, but I also noticed how can that potentially affect my way of programming. As I already mentioned the reason I chose F# was its semantics, I realized that .Net don’t talk F# and to access it I had to program with its semantics: imperatively, with a command like style almost dropping the expression oriented style that was one of the reasons I chose the language for!

Worse, this drew my attention to how this promise of 100% integrity with the preexisting libraries can affect the language design and implementation. Looking at implementations of popular languages like Ruby and Python on the JVM/CLR one can notice concepts that have almost forced in the languages (like attributes and signatures) . I guess these additions add noise to the language and to the code written with them, but that leads to an important question: How languages should communicate in a multilingual project? And what and where should be the seams when communicating between them?

Some libraries and frameworks are not even necessary for other languages, some just don’t fit, and some need to be wrapped and adapted for the new language

Before trying to think how to communicate between languages, I need to think about what is the kind of libraries I need for the new language. I mean of course it might be technically possible to use Spring and Hibernate when programming JRuby. But in case it is your first choice then, I guess you might be missing the point. There are a lot of frameworks and libraries that have been constructed because of limitations of the language. That is why you need to think twice before just accessing a foreign library because it might be supported natively by your language of choice or its supporting libraries.

Nonetheless , there are some necessary libraries that just don’t exist in your language of choice, and it would be kind of nice to have the painless access to them. But on the other hand, and as stated previously, these libraries don’t talk your language. It is somehow senseless, for instance, to specify type parameters while accessing strongly typed APIs from a dynamic language like Ruby, or say implementing strategies or writing 10 lines of imperative code for configuring a mutable object from a functional programming language like F#.

Some of these necessary APIs can be adapted for the target language. So in this case our issue is solvable by wrapping these well thought mature APIs to make them fit better in the target language paradigm. This can be done using abstractions while leaving other unnecessary details behind like state or type casting. This makes them less intrusive on your paradigm or language. Some libraries’s paradigm, however, is impossible to hide, and in this case you either need an equivalent library for your language, or you need to think carefully about your modules and boundaries to be able to use different libraries with different paradigms without one module being intrusive on another.

Understanding properties and semantics of each used language is the key to a better integration and communication

Each programming language is built and designed based on some theories, principles and concepts. Understanding these does not only help you in your decision of language choice, but this also helps you identifying where and how languages should be communicating. So for example modules written with functional programming languages can be imagined as stateless services that take an input and yield an output, modules written using OOP as holding the state, and modules written with configuration languages place some global, most probably immutable, variables into the environment. This thinking also helps defining barriers between modules to identify when the scope of one language ends for the starting edge of another.

Finally

In this post I tried to draw attention to a hairy, in my view, promise we get from language specification implementers of a seamless integration with existing libraries and frameworks. I guess in the no-fit case, the seam will be in your code and it will obscure it and add noise to your language rather than nicely being abstracted behind a foreign API wrapper. Proper design is instrumental for a non intrusive integration. Properties of each of the used language and frameworks should be considered.

May 27, 2007

Transactions are not automatic! Logs neither…

Filed under: Agile Programming, Architecture, DOTNET, Domain Driven Design — Sadache @ 8:12 pm

As Aspect Oriented Programming is getting more and more known, a lot of wrong usages appear. Surprisingly, I’ve seen common agreement among J2ee/.net architects for applying it in their application architectures.

First of all, Aspects are not only about the Spring’s  code injection by interception. Even if it looks like interesting, this style of AOP use can be misapplied. One of these misapplications is in transactions. I ve heard it a lot, “All my services are transactions” or “All methods that end with service are transactions” and worse “all of this package (assembly) is transactional!”

Transactions are a part of business logic, they should be explicit in the code, and they should belong to whatever layer they need to belong to. Transactions are not automatic, and cant be applied as an aspect of a whole layer or as an architecture decision.

Yet applying them as Aspects (Aspect) Attributes or Tags is explicit, but they should not be exclusive to somewhere in the architecture, THEY ARE NOT A PART OF THE ARCHITECTURE!

I like the .Net’s implementation of TransactionScope in System.Transactions. It fits rather good to the functional meaning of transactions. I still prefer to have a more explicit code that expresses and treats them. After all, EBay is Transactionless (talking about database technical transactions) and Amazon doesn’t do any Distributed Transactions! That confirms that transactions should be treated as business  notions rather than technical.

I guess it wont be very intresting for readers if I introduce an aspect for loggin all my events and ideas to this weblog! Even if it would be much eaiser for me :) Beleive me, that would be floody!

Older Posts »

Powered by WordPress