Monday, February 28, 2011

Using Distributed Transactions in multitier environment

In one of my previous posts I wrote that we use Distributed Transactions in our code. This worked just fine till we tested all installed on one machine. But once we installed SQL server on machine1 and our services on machine2 and services that use our services on machine3 we got following error messages:

Inner exception message: Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool.

This means that your transaction is distributed but not as much as you wish it to be. So it is not enabled for Network access. You would need to go to Administrative Tools –> Component Services to enable it.

image

Here is everything explained up and down how you Enable Network DTC Access:

http://msdn.microsoft.com/en-us/library/cc753510%28v=ws.10%29.aspx

Also, please keep in mind that you would need to enable it on both machines between which you are distributing your transaction.

Saturday, February 26, 2011

Mediator

Some time have passed since I wrote blog post on GoF Design Pattern. Not sure what are actual reasons for this. I guess passing MS exams, but maybe I was lazy.
I had been thinking about good example for Mediator Design Pattern and could not find anything better than standard example with UI and cooperation of different components, but this was before I thought about neurons and their cooperation and in few moments I realized amazing thing. Our brain is mediator to different body parts. It just amazingly fits description.
Just try to imagine what would happen if each of our body part knows about each other. This means that if you see something great and you want to touch it, your eye has to know about your hands and manipulate them to do this. What if you would need to defense by hands because you got hurt in chest. In this case your chest would need to know about hands and manipulate them. This can be applicable to any part of your body that collects information from world and then some reaction should be done by one or many other body parts. Cooperation many-to-many doesn’t work for our body. But it often happens that developers write code that works just as was described above. Sometimes it works, but sometimes it just creates crappy spaghetti mess. Our body has one central system that does all the analyzing for us and manages reactions. Very often this is good practice for code we write.
MEDIATOR
Mediator is design pattern that centralizes cooperation between components thus decoupling interaction between them.
Mediator gracefully simplifies understanding of interaction between components. This gives better maintainability for future, but because logic is centralized it can become very complex and huge, as human brain is.
Here below is demonstration of the demo application I wrote:
image
So as you can see brain know how to handle different things and how to react on them and with which body parts. How does this happen?
Brain (Mediator) knows about each body part (Colleague), so it actually has properties or fields, like Ear, Eye, Hand, Leg, etc. Each of the body parts knows its brain and can signal that something has happened.

Colleague base class (knows its brain)

        // Colleague
        class BodyPart
        {
            private readonly Brain _brain;

            public BodyPart(Brain brain)
            {
                _brain = brain;
            }

            public void Changed()
            {
                _brain.SomethingHappenedToBodyPart(this);
            }
        }

Concrete implementation of Colleague can look as following:

    class Ear : BodyPart
    {
        private string _sounds = string.Empty;

        public Ear(Brain brain)
            : base(brain)
        {
        }

        public void HearSomething()
        {
            Console.WriteLine("Enter what you hear:");
            _sounds = Console.ReadLine();

            Changed();
        }

        public string GetSounds()
        {
            return _sounds;
        }
    }
So it basically can HearSomething and can provide its status by GetSounds method. Some other body parts can have some reactions, as simple Face implementation below:
    class Face : BodyPart
    {
        public Face(Brain brain)
            : base(brain)
        {
        }

        public void Smile()
        {
            Console.WriteLine("FACE: Smiling...");
        }
    }

Mediator (aka Brain)

As it should be expected Mediator is bit huge class and it has to handle all the complexity of interaction between body parts. You might want to change something in this class, I do not expect you to see this as ideal brain, but I hope you get an idea how it works.
    // Mediator
    class Brain
    {
        public Brain()
        {
            CreateBodyParts();
        }

        private void CreateBodyParts()
        {
            Ear = new Ear(this);
            Eye = new Eye(this);
            Face = new Face(this);
            Hand = new Hand(this);
            Leg = new Leg(this);
        }

        public Ear Ear { get; private set; }
        public Eye Eye { get; private set; }
        public Face Face { get; private set; }
        public Hand Hand { get; private set; }
        public Leg Leg { get; private set; }

        public void SomethingHappenedToBodyPart(BodyPart bodyPart)
        {
            if (bodyPart is Ear)
            {
                string heardSounds = ((Ear)bodyPart).GetSounds();

                if (heardSounds.Contains("stupid"))
                {
                    // attacking offender
                    Leg.StepForward();
                    Hand.HitPersonNearYou();
                    Leg.Kick();
                }
                else if (heardSounds.Contains("cool"))
                {
                    Face.Smile();
                }
            }
            else if (bodyPart is Eye)
            {
                // brain can analyze what you see and
                // can react appropriately using different body parts
            }
            else if (bodyPart is Hand)
            {
                var hand = (Hand)bodyPart;

                bool hurtingFeeling = hand.DoesItHurt();
                if (hurtingFeeling)
                {
                    Leg.StepBack();
                }

                bool itIsNice = hand.IsItNice();
                if (itIsNice)
                {
                    Leg.StepForward();
                    Hand.Embrace();
                }
            }
            else if (bodyPart is Leg)
            {
                // leg can also feel something if you would like it to
            }
        }
    }
Personally I liked example that I’ve prepared. Hope there is no such example over internet, but who knows.

UML

In addition here below is some UML stuff. You can click on image to see it bigger.

image
My design patterns table

Friday, February 18, 2011

I’m MCPD: Enterprise Application Developer 3.5

So far so good, I passed everything I planned to pass for the next 1.5 years. Take a look at the picture in career plan for software engineer blog post. I guess that I probably was afraid about learning ASP.NET. And as I passed everything sooner I can plan for more cool stuff, for instance starting my own small project or spend more time on personal life.

But here is just a little bit more on exam itself. I was ready for this exam not because of some books, training kits or anything else. It is all my experience. I extremely enjoyed passing this exam, you just apply your experience and common sense if you have it in software development of enterprise applications. Exam measures my understanding of designing application, its components, their testing, stabilizing, deployment, also choosing appropriate technologies. That is what I encounter often at work.

 

MCPD(rgb)_1260

This is good plus to my CV and promotion strategy.

Exam 70-565: PRO: Designing and Developing Enterprise Applications Using the Microsoft .NET Framework 3.5

I passed 70-565 exam with score 971 out of 1000. And this time exam questions just flattered to me. I liked them much. I believe that this is because they are not kind of questions where you have to know exact method signature. These are questions where you have to think and choose appropriate decision, and even more, they overlap a lot with questions I encountered in my experience.

You can see my transcript using this information:
https://mcp.microsoft.com/authenticate/validatemcp.aspx
Transcript ID: 904316
Access Code: andriybuday

Nearest certification plans

There is still one thing left. It is 4.0 .NET framework outside, not 3.5 so I’m looking for upgrade of the current certification. The best matching exam would be transition of my skills to MCPD 4.0 Windows Developer (70-521). At the moment I don’t have exact date for this exam, but it might be in 2-4 weeks.

Friday, February 11, 2011

That’s enough for post suggestion poll on my blog–RESULTS

 

Here below are results of the poll that have been on my blog:

image

Obviously people want me to write more on software design and patterns and no one wants to see “Hello World” applications. I’ve been thinking about this poll that it might be that I misses something related to frameworks and new technologies/languages. I’m afraid that those are valuable options as per me.

Anyway, that isn’t so much bad, 24 people have voted and I appreciate all of the votes. I will try to be stick to it, but cannot promise that I will have everything in proportion. Actually I’m planning to have 24 book reviews this year. This means 48 posts on design patterns and software design. Hm.. not that many. Smile

Thank you very much.

Bind two values into one text box

Today I’ve spent some time on playing with some piece of code that had to ensure that on screen in some NumericTextBox depending on situation we show one of the two values – either one automatically calculated or either another user entered.

If value was automatically calculated we show it in that text box, but if user changes it we still keep calculated value untouched (double? CalculatedMileage) but work with another value (double? EnteredMileage). As you see we cannot bind any of these directly to the view.

So, how do we bind two sources (fields) to one target (textbox) on the UI?

I came to some solution with which I’m not completely satisfied, but at least I like it much more than it was before my changes.
Here is binding (I did not change it):

<UserControls:NumericTextBox AllowDecimals="True"
                             Minimum="0" UseMinimum="True"
                             Maximum="999.999" UseMaximum="True"
                             MaxLength="6"
                             Value="{Binding Mileage, Mode=TwoWay}"

Now, Mileage property that I’m binding to the view has backing field _mileage, so I always get value for displaying by using simple getter. But on set I’m actually populating two fields _mileage and EnteredMileage like below:
        private double? _mileage;
        public double? Mileage
        {
            get
            {
                return _mileage;
            }
            set
            {
                _mileage = value;
                EnteredMileage = value;
                RaisePropertyChanged("Mileage");
            }
        }
This means that once I used setter I touched EnteredMileage.
What I display is _mileage and I when I display is when RaisePropertyChanged gets called. So keeping this in mind I do following stuff on screen setup:
 _mileage = EnteredMileage.HasValue ? EnteredMileage : CalculatedMileage;
 RaisePropertyChanged("Mileage");

If user initially entered some value we start operating with it and don’t touch calculated one, but if he did not, then we show calculated at first.
I may be bothering you, but I’m wondering if you know/see some more elegant solution for this stuff. I guess there must be some. And if there is no such, then use mine…

Thursday, February 10, 2011

Book Review: “3.5–ADO.NET Training Kit” and passing 70-561 exam

Training Kit

imageSo I finally finished reading training kit for ADO.NET exam. It is easy to read book with many topics that you probably know or at least heard about if you are using .NET. But besides of standard topics on DataSets, Data Querying, LINQ there are few that represent much more interest at least for me. For me these are chapters about Synchronization Framework, Entity Framework and Data Services (REST). I read them with great pleasure. But unfortunately in book they are covered briefly. Also Data Services are not even included into exam questions. I would also say that unless you are going to pass 561 exam, don’t read this training kit, better find some good tutorials on each of the themes, use MSDN and try everything out.




Exam 70-561: TS: Microsoft .NET Framework 3.5, ADO.NET Application Development

I passed 70-561 exam with score 700 out of 1000 with needed 700 for passing. Yeah, that is damn near to failure. But what is also interest that the more I fill overconfident about the exam the worse score I get. Maybe that is why I failed WinForms for the first time, but passed ASP. Who knows.. This rule probably doesn’t apply to WCF exam. I was confident about it and passed with high score.
You can see my transcript using this information:
https://mcp.microsoft.com/authenticate/validatemcp.aspx
Transcript ID: 904316
Access Code: andriybuday

Nearest certification plans

My current certification situation is as following:
image
So I’m very close to my certification goal which is “MCPD: Enterprise Application Developer 3.5” and I already scheduled 565 exam for Friday Feb 18. As for preparation I really hope that couple of years of experience in Enterprise project are in help for me. Also I pick up “Microsoft Application Architecture Guide” for my reading. I read about first 30 pages and I’m impressed by this book. It is real glue. I like very much things that I read from very first pages. Will see how it goes and be ready for review next week. I believe it will be more comprehensive, since this is not kind of training kit book.