sfeldman.NET

.NET, code, personal thoughts

October 2009 - Posts

Posted by Sean Feldman | with no comments
Filed under:

One picture is worth a thousand words :)

PA269561

Posted by Sean Feldman | with no comments
Filed under:

I really liked this presentation. Highly recommended.

Posted by Sean Feldman | with no comments
Filed under:

Today I was trying to solve a simple technical problem. Given a specific XML, needed to clean it up by removing any elements of a particular type.

<Attachment>
  <Name>file1.pdf</Name>
  <Id>1</Id>
</Attachment>
<Attachment>
  <Name>file2.pdf</Name>
  <Id>2</Id>
</Attachment>

Result had to be without Id elements

<Attachment>
  <Name>file1.pdf</Name>
</Attachment>
<Attachment>
  <Name>file2.pdf</Name>
</Attachment>

A few choices for implementation:

  1. Regex
  2. XmlDocument
  3. LINQ to XML
  4. XSLT (as suggested in comments)

Regex option is probably the most efficient, but not the most maintainable. Myself, looking sometimes at the solutions with Regex I ask “what the heck did I try to do here”. So much for “code doesn’t lie”.

XmlDocument is more expressive than Regex option, but way too chatty.

LINQ to XML same as XmlDocument, expressive. As well as very clear and fluent. I picked this option not for performance, but for maintainability sake. I know it will take less developer type to understand and/or modify code when it’s time to change it. And it documents itself very well, with no need to write any comments.

var xdoc = XDocument.Load(new StringReader(received_content));
xdoc.Descendants().Where(element => element.Name == "Id").Remove();
return xdoc.ToString();

 

 

 

Note: this is a very specific case, which does not indicate it’s a solution to all kinds of problems. Regex / XmlDocument are valid tools for all sorts of other problems.

Posted by Sean Feldman | 4 comment(s)
Filed under:

More than a year ago, I have posted a blog entry related to what I was trying to implement in one of the projects. Unfortunately, not my team could understand what I was trying to do, neither I was able to make myself clear. Either way, I ended up closing the blog with a question “can code be too loosely (coupled), or your code is so coupled, that anything else is difficult to digest?”. Now I can answer my own question question.

Today, my current team had a team learning session, when the latest project demonstration showed what are we doing with a IoC container we started to leverage (StructureMap). The team is not the same one I had back a year+ ago. This team has also had same difficulty in the beginning – code that was too coupled. We have started from “Poor mans’ injection”,  moved to home-grown Dependency Resolver with a typical Startup Task, and now have reached the point, where a third party open-source IoC container is used. I think that could be achieved because:

  • We worked on decoupling code, learning the pros and cons
  • We got to the point were SRP is followed
  • We have got to the point were Design by Contract is almost entirely how we develop
  • We have a complete TDD development in place

So why I could not root the idea in my previous team? Because it was way too early for my team mates and I was not ready to wait.

Oh, and how the code looks now? Even better than before ;)

 

public class ComponentB : IComponentB   
{
  private IComponentA dependency;   
 
  public ComponentB(IComponentA dependency)    
  {   
    this.dependency = dependency;  
  } 
 
  public void SomeFunctionality(string param)
  {
    var result = dependency.OperationDefinedByDependencyContract(param);  
    return DoSomethingWith(result);   
  }
}

Benefits:

  • TDD/BDD development style is enforced better when there’s no default constructor with Dependency Resolver or Poor’s man injection
  • No messy Startup Task that is a must when doing Dependency Resolver
  • Can leverage smarts of IoC container to perform tasks on a massive scale (interceptors, events, etc)
  • Easy way to verify if all dependencies are wired or not (contract based dependencies)

And as one commenter of the original blog post has asked correctly: “The question might be more appropriately be, is the code self-documenting? Can someone who did not write the original code follow the logic and modify and maintain it?”. The answer is: it depends.

You have to get to a certain level to answer Yes. Until you get to that level, the answer will always be No. In order for code to be self-documenting, it needs to be of high quality and accompanied with tests/specifications. In order to be modified, maintained, and logic followed by a person who did not write it, it has to have the listed above AND have the person to be at a certain level of skills.

Posted by Sean Feldman | 1 comment(s)
Filed under: , ,

ASMX web services were a breakthrough when appeared on .NET platform. A lot of services were created to take advantage of web services technology.

Now, that WCF is replacing legacy web services, we still have a lot of legacy web services running and being used. Connecting to legacy web services from WCF can be achieved either by leveraging Visual Studio .NET auto-generated proxies, or by creating dynamic channel I talked about before. Creating dynamic channel looks like a cleaner solution, but there are a few caveats:

1. Legacy web service does not always expose a known/published interface (quiet often just implementation and no contracts);

2. Configuration might be a little tweaky;

In order to overcome the 1st issue, we can “re-invent” the contract that a legacy web service “would” implement.

[XmlSerializerFormat]
public interface ILegacyService
{
  [OperationContract]
  Version GetVersion();
}

Next we can generate a dynamic channel

var channel = new ChannelFactory<ILegacyService>().CreateChannel();
using (channel as IDisposable)
  return channel.GetVersion();

Once we have channel – time to configure

<system.serviceModel>
  <client>
    <endpoint name="SomeServiceEndPoint"
              address="http://some.where.com/LegacyService/LegacyService.asmx"
              binding="customBinding"
              contract="ILegacyService"
              bindingConfiguration="custom.binding.for.SomeService">
    </endpoint>
  </client>
  <bindings>
    <customBinding>
      <binding name="custom.binding.for.SomeService">
        <textMessageEncoding messageVersion="Soap12" writeEncoding="utf-8" />
        <httpTransport/>
      </binding>
    </customBinding>
  </bindings>
 
</system.serviceModel>

One comment about ServiceContract attribute. When consuming a legacy web service, and it has a pre-defined namespace. Modification to “re-invented” service will include the required information (such as Namespace)

[XmlSerializerFormat]
  [ServiceContract(Namespace = http://some.where.com/LegacyService)]
  public interface ILegacyService 
  {
    [OperationContract]
    Version GetVersion();
  }
Posted by Sean Feldman | 1 comment(s)
Filed under:
More Posts