Right aligning text when printing from a .net program

In a program I am writing, I need to print directly to a couple of label printers.

My God these things are a pain in the ass to work with, but I will leave that rant to another post.

One thing I ran into was how to right align text when using the DrawString method of the System.Drawing.Graphics class off of the PrintPageEventArgs.

The solutions I was coming across seemed about as bad as I was expecting.

They involved measuring the width of the line of string using some System.Drawing.Graphics.MeasureString to figure out the length, and then dynamically position the text to make it appear right aligned.  So what that means is that for short text, X would be greater and for long text X would be less.

Fortunately, I came across the most simple solution:

Dim s As New StringFormat()
s.Alignment = StringAlignment.Far

e.Graphics.DrawString("por que?", New Font("Tahoma", 8), Brushes.Black, 50, 50, s)

Very simple.  But you have to wonder, why did MS pick “Far” as the alignment name instead of “Right”, as you would expect.  Maybe “Far” is more of a graphics term for alignment?  Who knows.

 

Subsonic MVC Templates. Not what I was expecting.

When I saw a new item in my RSS feed from Rob Conery about MV* I was immediately interested to read it, because I have been working on trying to create my web app pages using MVP, but am unable to find any examples beyond the most basic.

I would love to see how other people manage the interactions between the Controller and the View, to see how it compares to how I am doing it. 

My view interfaces tend to be kinda large.  For example, if I have a button that I hide and show depending on business rules, I will create a MyButtonVisibility property on the interface can set the properties from the controller.

I would be interested to see how others deal with things like the hiding / showing of items.  I could see wrapping more of that kind of functionality in the view, and giving the view some more logic but I think you would then start to lose some of the testability.

Anyway, the articl on Rob’s blog was really to talk about creating an MVC style architecture for subsonic itself, not the pages that use it.  However, Rob seemed to suggest that the new changes would aid you in using MV* in your pages by forcing you into good habits.

But I really don’t understand how that would work.  If you have code that does:

MyGridView.DataSource=Product.FetchAll();
MyGridView.DataBind();

And you change it so that you use a Controller (or Manager as I have called it when loading Business Objects or DTOs) to look like this:

Product product = ProductController.Get(newID);
product.ReorderLevel = 100;
ProductController.Save(product,"unit test");

I don’t see how this helps you create an MV* architecture in your pages.

Maybe I am just not understanding.

 

Getting Enterprise Services Working

I have been trying to get Enterprise Services working in one of my clients environment. 

The latest hurdle was cleared thank to this article, which pointed out the need to modify the settings for windows firewall to allow the msdtc executable to talk OUT.

To enable network transactions through the firewall, you will need to add the msdtc.exe to the exception list of the firewall on all the machines involved in the transactions. You can do this using the UI in Control PanelWindows Firewall or you can use this command: “netsh firewall set allowedprogram %windir%system32msdtc.exe MSDTC enable”.

Managing the number of assemblies

Scott Hanselman has an article on trying to pick the right number of assemblies when creating an application.

Much of my work is on a large intranet application, which brings lots of considerations into the fold.  We have started segmenting different web applications into their own projects, meaning they are seperate applications in IIS.

Of course this means that common functionality now needs to be broken out into assemblies.

Also, we had the practice of creating 1 data access assembly for each database we touched.  This was pretty simple at first as we only had a handful of databases, but now have 16 of these assemblies, am I am starting to think we should just merge them all into 1.

I downloaded a trial of NDepend which produced a pretty neat diagram connecting assemblies together.  I am going to try to see if I can refactor out a few (maybe only 2 are options at this point).

Adding Static Code Analysis To Team System/TFS

So I have this problem.

If someone declares:

Public iProductId as Integer

then I want to be notified.  But if someone declares:

Protected WithEvents txtUserName as Textbox

I don’t want to be bothered.

Every control declaration we have throws an error in the static code analysis tools.

So after asking around, I guess it is not possible to cusomize the existing rules to exclude common prefixes such as lbl, txt, cmd etc.

So apparently the only option left is to disable those rules and create my own. 

So here are a few links that deal with creating custom links.

MSDN

Kevin Castle

FxCop Team

 

Microsoft P&P Software Factories

I didn’t notice that the Microsoft P&P team has released something they call “Software Factories” which are supposed to guide the developer in building different apps using best practices (at least that is what I think they do from the descriptions).

Specifically I am interested in the Web Client Software Factory:

… provides an integrated set of guidance that assists architects and developers in creating composite Web applications and page flow client applications.

These applications have one or more of the following characteristics:

  • They have complex page flows and workflows.
  • They are developed by multiple collaborating development teams.
  • They are composite applications that present information from multiple sources through an integrated user interface.
  • They support XCopy deployment of independently developed modules.
  • They support online business transaction processing Web sites.

 

and the Web Service Software Factory, which as they put it is:

… an integrated collection of tools, patterns, source code and prescriptive guidance. It is designed to help you quickly and consistently construct Web services that adhere to well known architecture and design patterns.

The package covers:

  • Designing ASMX and WCF messages and service interfaces.
  • Applying exception shielding and exception handling.
  • Designing business entities in the domain model.
  • Translating messages to and from business entities.
  • Designing, building, and invoking the data access layer.
  • Validating the conformance of service implementation, configuration, and security using code analysis.
  • Planning for the migration to WCF.
  • Applying security to WCF services.
  • Applying message validation.