May 20, 2012 C#, OData, WCF, Web 3 comments
May 20, 2012 C#, OData, WCF, Web 3 comments
Let’s talk about OData.
I would propose to start our OData journey with the best thing about it. Which is openness of data, easy of accessing and working with it. Client applications no longer need to depend on some specific service methods or formats if there is OData feed available. Consuming OData is simple and enjoyable.
For example, I want to know how many users with name ‘Andriy’ are there on StackOverflow with reputation higher than 500. No one at StackOverflow would develop special method in API which would allow me to request exactly this data.
But as StackOverflow exposes OData feed we can connect to it with LinqPad (get it here) and simply write normal C# linq query, like this one:
You can see same data if you use URL below. This URL was built by LinqPad to request data:
(View page source if you don’t like how your browser rendered that feed).
So, no magic. You just build special URL and get your data of interest in preferred format. You can use wide set of libraries both for client and server to implement and use OData.
Whenever you see this icon it is good indication that there is OData feed available. There are many applications/web sites that already utilize this protocol. Do you use Nuget? It works through OData. Know ebay? They expose its catalog via OData. Need more examples? Go to ecosystem page of OData.
The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores. The protocol emerged from experiences implementing AtomPub clients and servers in a variety of products over the past several years. OData is being used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.
…from http://www.odata.org/
Continue your reading about OData on its documentation page here.
If you have time, I would recommend to watch this “OData: There’s Feed for That” MIX10 video.
As OData was initially introduced by Microsoft no wonder it is extremely easy to put it in place when you are on MS stack of technologies. If you are using EF there is almost nothing you have to do to make it happen.
Add “WCF Data Service” to your project.
You will get following code:
public class WcfDataService1 : DataService< /* TODO: put your data source class name here */ > { // This method is called only once to initialize service-wide policies. public static void InitializeService(DataServiceConfiguration config) { // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc. // Examples: // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead); // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } }
Assuming that you have your data model generated on Northwind db. All you would need is something like this:
public class WcfDataService1 : DataService<NorthwindContext> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2; } protected override NorthwindContext CreateDataSource() { return new NorthwindContext(ConfigurationManager.ConnectionStrings["NorthwindContext.EF.MsSql"].ConnectionString); } }
And now clients can do whatever they like with your data. Of course, you can restrict them as you wish. OData is not about putting your database into web, you can control what you expose and to which extent. Also you can play with your service by adding caching, intercepting queries, changing behaviors and much-much more.
In next post I will show how we can build OData service for custom data you keep in memory.
In meantime you can checkout another video from NDC2011 by Vagif Abilov. Video is called “Practical OData with and without Entity Framework”. Follow this link to direct mp4 video file.
May 7, 2012 Book Reviews 2 comments
For good reasons many people recommend book “The Mythical Man-Month: Essays on Software Engineering” when it comes to management of software projects. Reason is that it is one of the classical books on this matter. Recently I was also recommended this book. After reading it I realized that I grasped no new ideas or things that I did not know before. It is not because book is not good enough, but rather because it is very old and many other publications, I’ve read, have provided me with lots of up-to-date information. Not to mention, that many of these publications have been influenced by exactly this book.
Same was true for other person flying in same plane with Brooks, author of book:
The plane droned through the night toward LaGuardia. Clouds and darkness veiled all interesting sights. The document I was studying was pedestrian. I was not, however, bored. The stranger sitting next to me was reading The Mythical Man-Month, and I was waiting to see if by word or sign he would react. Finally as we taxied toward the gate, I could wait no longer:
“How is that book? Do you recommend it?”
“Hmph! Nothing in it I didn’t know already.” I decided not to introduce myself.
On the other hand, I got some insights into how software development looked like back in 70th.
Yes, I spelled it right. Book survived many years, mainly “because building things, including software, has always been as much about people as it has been about materials or technology – and people don’t change much in only 25 [35] years.”
I think that the strongest generalization from the book is this one:
“Assigning more programmers to a project running behind schedule, may make it even more late.”
To the topic, but not that much related. In book there is mentioning of Conway’s Law, which could sound like this:
“Organizations which design systems are constrained to produce systems which are copies of the communication structures of these organizations.”
The other day I came across nice article called “Seeing the team in the code”. As per me lack of opened and frequent technical communication between developers, like code reviews, could make source code not really what team wants it to be. On the other hand, it could help make code less coupled, if everything is made by contract. Conway’s law sounds like fun, but each joke is based on truth behind. There is even Microsoft research paper on this: http://research.microsoft.com/pubs/70535/tr-2008-11.pdf. (Not easy to read or to follow.)
I would recommend this book in two cases: you have little or no clue of how development and management of big software projects is done OR if you want to just make sure you are familiar with this work to have more coherent view on the topic. In case you are average developer, who frequently reads recent books or posts on management/leadership/etc in software it is very likely that you will not enjoy this book. I also did not enjoy it very much, I probably fall into case number 2.
Thanks for reading…
April 23, 2012 Languages, Opinion 6 comments
Title of the post is complete bizarre(*). It consists of words out of 4 languages I have to deal with now.
Before I moved to Austria I mostly used Ukrainian. Of course, many meetings at work were in English and all mailing was held in English. Not to mention, there was some interaction with Russian, but not much. At least there were no real need to speak it. Now everything has shifted. I knew that I will have to deal with English everyday, I also knew that I will need some basic German. What I did not know is that there will be many guys from Ukraine and Russia at work and I will use Ukrainian and also Russian for small talks in kitchen or at lunch or for one-to-one discussions.
I continue to speak Ukrainian at home with my wife. We try to use English/German phrases. My wife is not good in English, instead her German is at intermediate level, so we try to exchange some knowledge in languages. But you know what? Unless someone or something kicks you in the ass, you won’t take learning of language seriously.
So, I paid 290 euro to have someone kicking me each day for 2 hours during whole month. Normally it is called language course. After one week I can introduce myself and provide brief information about myself, I can count and ask basic questions, I already know some colors, week days, months, restaurant words, etc.
It worth to mention, that you really need some pressure to start learning German in Austria, because all people around speak English very good and if you are lazy you can simply avoid conversations in German. Plus everything here could be done via internet or though automatic devices, so not much human interaction during the day.
I’m afraid for foreigners coming to Ukraine for EURO 2012. On average people don’t speak English in Ukraine. It is pity and shame for me.
Now back to German language courses. As I mentioned, I’m attending intensive evening courses for total beginners. I allocate myself 1 hour before class to do my home work. So in total it is 3 hours of German per day. My group is rather small – only 4 people, me and one girl from Ukraine, lady from Kazakhstan and another girl from Iran. If there are some explanations needed they are provided in German, if not understood in Russian or English (only girl from Iran doesn’t understand Russian). Another very interesting thing is that, as school is concentrated in Russian/German, teacher is not extremely good in English. Thus I often help to explain things to girl from Iran who is proficient in English. For me it is great – I hear explanations twice: in English and Russian.
To your surprise there are many words which sound similar to English and some are similar to Russian and Ukrainian words (or probably otherwise). Germany/Austria geographically are located between Great Britain and Russia/Ukraine so it could be understood without reading dozen wiki pages on language families, branches and their roots. Again, good for me.
Nevertheless, I have this question: “Is German important language anyway?” Accordingly to wiki there are about 100 millions German speakers in the world, so 12th place by number of speakers, but apparently it is number one in Europe where I live now. It is highly developed language, it is also language of technology (after English of course). All these sound great and everyone would answer that German is important language for Europe, especially if you already know English. So would I. In short term it makes sense to learn German. But in centuries world will dramatically shift to English, if not Chinese.
This all makes me think about importance of languages, their meaning for me and their value for world. Imagine there are no other languages, but just one, no matter which, how much would world be easier? The most importantly, how much further would we develop? Would we already start to colonize Mars? Or would it have opposite effect? Accordingly to Darwin there should be some deviation, otherwise no evolution could be progressing. All these are very philosophical questions and suitable for beer evening, or… for Friday snaps evening.
To conclude, I’m very proud to realize I will understand almost 1 billion people in the world after I learn German (precisely, 902 millions as per wiki).
I have some questions for you:
Thank you!
P.S. Hope this was good reading. If not, please let me know. I’m willing to improve my blogging skills to write posts of higher quality. All for you.
(*) In English it would be “There is question of Language. Or not?”.
April 18, 2012 Opinion, UnitTesting No comments
I’m not going to write a long post discussing advantages and disadvantages of high code coverage. There are hundreds if not thousands of such posts out there and in the end almost all of them conclude that high code coverage in general is nice but not always justifiable, one of main reasons being redundant abstractions in favor of higher coverage. Here are my recent thoughts.
It may sound crazy and not doable at all(*) or may have side effects if misused. I suggest very simple techniques to achieve high code coverage the right way:
Don’t be lazy. Recently I worked on a project and I already had 95% coverage. If I haven’t decided to increase coverage further I wouldn’t have found one missing mapping for a property. I maybe spent couple of hours to write more tests, instead of days of devs/testers/managers time to work around the bug. And in case of finding this in production it would cost real money for the company.
Work around external dependencies you really cannot test. Isolate them as much as you can and simply exclude from coverage report. I don’t think this is cheating. It is the best you can do, plus you do it explicitly. And, of course, you should have integration tests to test external dependencies.
Remember the Single Responsibility. Well… and few more things. You will be amazed how much code is simpler and easier to read if you just keep following SOLID. I think that developer should be able to clearly describe responsibility of a single class within one sentence.
Start with testing in mind, not with coverage number. It is vital to keep in mind that tests are intended to ensure you code works as designed and without defects, tests are NOT intended for high coverage numbers, which can be shown to boss. Thus always have tests to cover more important and sensitive code at first and only then move towards covering less important or easy to test code.
Refactor! Never write code you don’t like. It is fine to hate it the next day, but not at the moment when you are writing it. Usually crap code starts to appear when you try to add functionality which was not planned before. You must refactor constantly (same is applicable for your unit tests). Keep everything in synch.
Be a 100% good programmer. Don’t spoil yourself with 80% coverage or with just 60%. If someone says you are 75% good programmer, would you like it? Well, it is high number, isn’t it? I was worse developer few months ago than I’m today. Year ago I would disagree with today’s myself. High coverage, if used right, means that you know that your code works and that it is readable/refactorable/decoupled/structured/… and most of all – it is highly maintainable.
I hope my opinion sounds sensible!
Till next time…
Further reading:
There are research papers on this matter. In “Experiments of the effectiveness of dataflow and control flow-based test adequacy criteria” authors “evaluate all-edges and alluses coverage criteria using an experiment with 130 fault seeded versions of seven programs and observed that test sets achieving coverage levels over 90% usually showed significantly better fault detection than randomly chosen test sets of the same size. In addition, significant improvements in the effectiveness of coverage-based tests usually occurred as coverage increased from 90% to 100%.” – from MS research paper.
(*) I could agree with many exceptional situations you are thinking about. I would agree that with old systems it is difficult to do what I ask you to, I would also agree that if there are deadlines it is hard to stand against. There are bad programmers around, bad decisions taken and many other conditions. In the end it is your job to do the job right. And if you cannot, change the company or change the company.
April 16, 2012 WCF 2 comments
I would like to quickly mention how threads are used while running instance of WCF service. Don’t bother if you are WCF guru!
When using PerSession, once client did first call, instance of service implementation will be kept on server. Every client has it’s own session executed only on one thread at one point of time (thread is taken from ThreadPool). So, one client cannot run concurrent calls within one session. But you can change ConcurrencyMode to make this happen. In case of PerCall service instance will be disposed immediately after call is done.
ThreadPool uses available thread to run your service code. But if you start 1000 concurrent clients ThreadPool will allocate many threads, which involves resources, such as memory. So keep this in mind when designing scalable applications. PerCall is best choice for highly scalable services.
I created simple Calculator service to show how treads are used by WCF service.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class CalculatorService : ICalculatorService { private int _threadIdOnCreating = Thread.CurrentThread.ManagedThreadId; public int AccumulatedValue { get; private set; } public int Accumulate(int valueToAdd) { AccumulatedValue += valueToAdd; Console.WriteLine(string.Format( "Accumulated: {0}. ThreadIdOnServiceCreating:{1} CurrentThreadId:{2}", AccumulatedValue, _threadIdOnCreating, Thread.CurrentThread.ManagedThreadId)); return AccumulatedValue; } }
Calculator is very simple it allows to accumulate some value within session and also it logs some interesting threading information – id of service instance creator thread and id of current thread within method.
I ran Accumulate method with parameter 2 for five times and then created new client proxy and did the same. Below is output, which proves that server keeps instance of service implementation (proof is threadId on creation), but methods are ran on different threads, taken from ThreadPool (proof is threadId on run).
I'm calculator
Accumulated: 2. ThreadIdOnServiceCreating:6 CurrentThreadId:6
Accumulated: 4. ThreadIdOnServiceCreating:6 CurrentThreadId:7
Accumulated: 6. ThreadIdOnServiceCreating:6 CurrentThreadId:6
Accumulated: 8. ThreadIdOnServiceCreating:6 CurrentThreadId:7
Accumulated: 10. ThreadIdOnServiceCreating:6 CurrentThreadId:6
Accumulated: 2. ThreadIdOnServiceCreating:9 CurrentThreadId:9
Accumulated: 4. ThreadIdOnServiceCreating:9 CurrentThreadId:6
Accumulated: 6. ThreadIdOnServiceCreating:9 CurrentThreadId:9
Accumulated: 8. ThreadIdOnServiceCreating:9 CurrentThreadId:6
Accumulated: 10. ThreadIdOnServiceCreating:9 CurrentThreadId:8
Hope this was interesting. Also continue your reading on instance/session management of WCF at msdn pages here if you would like to. Or read another short blog post my me.
BTW, this is just reposting of one of my answers at stackoverflow, but I thought it worth to have it here.
April 15, 2012 Book Reviews, Success No comments
I like encouraging books. I like to be inspired by ideas that make us think we can do something to improve our lives. Despite, we all know that to change something we have to work extremely hard, usually after few attempts we give up, but never want to admit this. This is how we are built – we want to be better for less efforts.
Book “First Things First” is very good because it, unlike many other time-management books, brings to the table more dramatic and global questions of our path and purpose. Start with this: “How many people on their deathbed wish they’d spent more time at the office?”. Of course people work to make their living, but apparently working hard at work cannot be life goal itself. The goal could be to contribute to society and to your profession. I’m software developer and my goal could be to contribute to community of other guys, similar to me. Instead I catch myself on being hard-worker. I stay longer than others at work. In the end this only transforms me into Dark Matter Developer. I may get noticed at work and get better income, so I buy myself bmw z4 in few years. But, what’s the point? No happiness at home, no recognition in world of software developers and small but fast car for no kids. It is what you do, not what you own.
I never was lucky to truly embrace lot of time-management techniques. Maybe they are too difficult and take much time to get them rolling. Or maybe they are just wrong? They teach us to be effective and efficient, but usually we solve only short-term goals. Are you sure you are heading “the true north”?
From this book I will take & try one simple and great technique and would recommend you to try. Think about one thing for your personal life and one for your professional life, which would definitely improve quality of your life if you do them constantly and coherently. Ok? Just do them! Don’t concentrate on any other things… work on these two.
I have two for myself: Personal: exercise every morning. Professional: frequently blog quality posts. Though, none of them transparently represent how some aspects of life can be improved, believe me or not, I see great breakthrough, if I only do them.
Almost forgot, this should have been book review:
And it is! Thoughts provoked by book are great indicator of its quality.
Book worth to be read!
You may find my thoughts on other books I read by Book Reviews tag, many of them are also time/self-management and success oriented.
March 22, 2012 .NET, Autofac, C#, IoC 3 comments
builder .Register(b => new F()) .As<IF>(); builder .Register(b => new E()) .As<IE>(); builder .Register(b => new D1()) .Named<ID>(SomeD1Name); builder .Register(b => new D2()) .Named<ID>(SomeD2Name); builder .Register((b,prms) => new C(b.Resolve<IE>(), prms.TypedAs<IF>())) .Named<IC>(CName); builder .Register((b,prms) => new B(b.Resolve<IF>(), b.ResolveNamed<IC>(CName, prms))) .As<IB>(); builder .Register(b => new A(b.Resolve<IE>(), b.ResolveNamed<ID>(SomeD1Name), b.ResolveNamed<ID>(SomeD2Name))) .As<IA>(AName); builder .Register(b => new Service(b.Resolve<IComponentContext>(), b.Resolve<IA>(), b.Resolve<IB>())) .As<IService>();
And that’s it.
March 21, 2012 .NET, C#, QuickTip No comments
Recently I had some FTP work to do. Nothing special, but in case you need quick guide on setting up FTP and writing access code in .NET you might find this interesting. Also you know where to find it in case you need it later.
I will define simple task and we will solve it!
Task:
Imagine we have external FTP server, where some vendor puts many files. Of course they provided us with credentials. We want to connect to server and then parse some files from the whole list of files. Also for testing purposes we are going to mock external service with our own local.
Setup FTP:
1) Enable FPT in Windows features.
2) Go to IIS –> Sites –> “Add FPT Site…”. You would need to specify some folder.
3) As for our task we want to mock some system. Following setup might be good:
You should see something like this:
You will be able to access FTP locally without any issues and need to provide credentials.
4) Go to User Accounts –> Advanced –> Advanced –> New User… Create user you would like use when connecting to FTP.
5) Go to IIS -> your FTP site –> Basic Settings –> Connect as… –> Specific User. And enter same user again.
We added this user because we need to imitate situation in which our code and FTP have different credentials.
Access code:
To get list of files on server (using WebRequest):
public List<string> FetchFilesList() { var request = WebRequest.Create(FtpServerUri); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential(UserName, UserPassword); using (var response = (FtpWebResponse)request.GetResponse()) { var responseStream = response.GetResponseStream(); using (var reader = new StreamReader(responseStream)) { var fileNamesString = reader.ReadToEnd(); var fileNames = fileNamesString.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); return fileNames.ToList(); } } }
To fetch some file contents as XDocument (using WebClient):
public XDocument FetchFile(string fileName) { var client = new WebClient(); client.Credentials = new NetworkCredential(UserName, UserPassword); var fileUri = new Uri(FtpServerUri, fileName); var downloadedXml = client.DownloadString(fileUri); return XDocument.Parse(downloadedXml); }
I don’t think those two chucks of code need lot of explanations. As you can see with WebClient there is less code, but this way you cannot specify request ftp method.
Hope this overview is quick and not too much noisy.
NOTE: I’m not professional administrator, so my FTP setup may be somewhatwrong, but it satisfied needs of task described in the beginning of blog post.
In any case here are some links:
March 20, 2012 .NET, C#, QuickTip 2 comments
This blog post might look like any other standard blog posts, answering question, which can be googled and found on stackoverflow. But it isn’t. You see… it composes couple of interesting things you might need for you custom configuration. Also it is not congested with explanations. I’m adding this as quick reference for myself, so I don’t spend my time googling a lot to find answers. Also if you just starting with custom configuration and don’t want to read MSDN pages, please refer to my earlier blog post on basics here.
Let’s get back to topic:
We want section in our app/web.config with collection which will be able to contain elements without ugly “add” tag and also have CDATA inside. See configuration:
<Feeds defaultPollingInterval="00:10:00"> <Feed> <![CDATA[http://www.andriybuday.com/getXmlFeed.aspx?someParam=A&somethingElse=B]]> </Feed> <Feed pollingInterval="00:05:00"> <![CDATA[http://www.andriybuday.com/getXmlFeed.aspx?someParam=C&somethingElse=D]]> </Feed> </Feeds>
So as you can see in collection of elements there is custom name “Feed”, which is awesome. Also notice that URL contains weird characters (not for us, but for XML), so we surround URL into CDATA. Those feeds are fake of course.
To make all this happen we need few things:
Source code below (interesting pieces are in bold):
[ConfigurationCollection(typeof(FeedConfigElement))] public class FeedsConfigElementCollection : ConfigurationElementCollection { [ConfigurationProperty("defaultPollingInterval", DefaultValue = "00:10:00")] public string DefaultPollingInterval { get { return (string)base["defaultPollingInterval"]; } } protected override ConfigurationElement CreateNewElement() { return new FeedConfigElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((FeedConfigElement)(element)).Url; } // In order to avoid standard keyword "add" // we override ElementName and set CollectionType to BasicMap protected override string ElementName { get { return "Feed"; } } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.BasicMap; } }
public FeedConfigElement this[int index] { get { return (FeedConfigElement)BaseGet(index); } } } public class FeedConfigElement : ConfigurationElement { public string Url { get; private set; } public string PollingInterval { get; private set; } // To get value from the CDATA we need to overrride this method protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey) { PollingInterval = reader.GetAttribute("pollingInterval") ?? "00:00:00"; // Also for some unknown reason for CDATA ReadElementContentAsString returns // a lot of spaces before and after the actual string, so we Trim it Url = reader.ReadElementContentAsString().Trim(); } }
Hope this gives quick answers to some of you. It took me good portion of time to find all this things, because for some odd reason it wasn’t so much easy to find.
Some links:
March 19, 2012 QuickTip, WP7 No comments
I can assume this is of no use for most of you, my constant readers.
But in case you googled for this or just happened to have Windows Phone 7 and cannot update it because of the error 80070026, which you see on the image below,
please know: the only solution that helps is complete reset.
Don’t waste your time on searching for solutions – I did it for you. Also reset is not that painful as you might imagine. Just ensure you have all your media and application data backed-up to some clouds or at least local drive. With good internet connection, which no doubt you have at home, reinstallation of apps is super quick, plus you will get rid of junk apps.
To reset either go to “Settings->About->Reset you phone” or use more geeky way with using phone buttons: turn off phone, then press both volume up and down buttons and hold and then press the power button (briefly) to switch the phone on. When you see reset screen release volume buttons and follow instructions.
You might be interested to know if there are some specifics to my situation, so here they are:
Phone: HTC Mozart T8698
Update: 7.10.7740.16 => 7.10.8107.79
When: On performing actual update
OS: Windows 7
Zune: latest version possible
Some links in case you have other update troubles:
If you found another solution to this problem please let me know.