Archive for June, 2010
Validation Framework – Enterprise Library Validation Block
In previous
posts I wrote about validation AOP-style, using Policy Injection or Depency Injection (Unity). I am a true believer that this form of validation is the way to go.
That said, what kind of validation do you really need?
User Input Validation is probably the first thing that pops up into your mind, and that is indeed an important one. But, in fact, validation can be applied to almost every layer. That’s why they call it a cross cutting concern and AOP makes sense.
That’s not what I mean. I am talking about using validators here. You probably know the available ASP.NET validators, like shown in above picture.
But you know what? Although User Input Validation is important, sooner or later the data will be persisted to a database. And if your database accepts varchar(10) only, then “Thunderbird” is not such a good idea to type.
In short, you are in need of a StringLengthValidator.
Of course you can create you own simple validation rules engine, but fortunately it is also an out-of-the-box validator of Microsoft’s validation block.
Difficult? Not at all, when following these simple steps:
- Download and install Microsoft Enterprise Library 5.0
- Create a project
- Add a reference to the assemblies “Enterprise Library Validation Application Block” and – not to forget – “System.ComponentModel.DataAnnotations”
- In your class, add the namespace: Microsoft.Practices.EnterpriseLibrary.Validation, et voilá
using System;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
public class Customer
{
[StringLengthValidator(0, 10)]
public string CustomerName;
public Customer(string name)
{
this.CustomerName = name;
}
}
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer("Thunderbird");
Validator<Customer> validator = ValidationFactory.CreateValidator<Customer>();
ValidationResults results = validator.Validate(customer);
if (!results.IsValid)
{
foreach (ValidationResult result in results)
{
Console.WriteLine(result.Message);
}
}
}
}
Perspectives in Software Architecture Quality
Recently, I followed a discussion about quality of software in zombie mode. Please do understand that not the software – but I – was in zombie mode.
The question under discussion was: who is to blame for poor software quality?
Is it the business, with their vague requirements? Or maybe the (project)managers, who only care for budget and scope? In any case the coders are to blame, are they?
Maybe all of the aforementioned?
Personally, I am a (sort of) believer in Goldratt his theory of constraints, so the answer would be obvious. Loss of quality adds well to the pile of crap.
However, what first struck me was the omission of software architects (and testers). The omission suggests that there is no role for software architects in quality? Strange.
Therefore I would say: no wonder it is of poor quality, there is no software architect involved as a technical conscience. On the other hand, technique plays – of course – only a small part in the whole software development process. For creating quality software, all perspectives must thus be taken into account.
True. But what is the definition of quality code?
From a technical viewpoint you can have high-quality software, whilst from a business perspective the software doesn’t solve the problem that the business is looking for. The other way around can also be true.
Therefore, the motto of today:
Although the software is crappy, the business still can be happy!
That’s the power of a good UI/UX. Apple does understand this very well.
Generating Database Objects from an UML Model – Part 1
In this mini-series of one post, I’ll show you how to generate SQL-script from an UML model.
First, you need a model. This can be any model, but here I use an UML model. The class diagram on the left shows a simple model consisting of a parent class and a related child class. Both are stereotyped <<table>> so you can see we are talking about a data model.
Both have some attributes. Note that their types have been declared explicitly.
The interesting part is their association. As you can see it is a traditional master-detail relationship. For clarity I have named the association and both association ends, but the latter is not strictly necessary.
This UML diagram can be represented in the following XML form:
<?xml version="1.0" encoding="utf-8"?>
<EntityModel>
<Entity Name="TestParent">
<Property Name="TestParentName1" Type="String" />
<Property Name="TestParentNumber" Type="Integer" />
</Entity>
<Entity Name="TestChild">
<Property Name="TestChildName" Type="String" />
</Entity>
<Association Name="FK_TestParent_TestChild">
<End Name="TestParent_End" Role="TestParent" Multiplicity="0..1" />
<End Name="TestChild_End" Role="TestChild" Multiplicity="*" />
</Association>
</EntityModel>
Why that is interesting? That is because XML easily can be transformed into something useful.
For performing the actual transformation you can use, for example:
- XSLT stylesheet-driven transformation
- template-driven code generator
- code using XmlSerializer or XmlReader
Thus, it is transformed into a sql-script.
/****** Table [dbo].[TestParent] ******/ CREATE TABLE [dbo].[TestParent]( [TestParentID] [int] IDENTITY(1,1) NOT NULL, [TestParentName1] [varchar](MAX) NULL, [TestParentNumber] [int] NULL, CONSTRAINT [PK_ParentTable] PRIMARY KEY CLUSTERED ([TestParentID] ASC) ) GO /****** Table [dbo].[TestChild] ******/ CREATE TABLE [dbo].[TestChild]( [TestChildID] [int] IDENTITY(1,1) NOT NULL, [TestParentID] [int] NULL, [TestChildName] [varchar](50) NULL, CONSTRAINT [PK_ChildTable] PRIMARY KEY CLUSTERED ([TestChildID] ASC) ) GO /****** ForeignKey [FK_TestParent_TestChild] ******/ ALTER TABLE [dbo].[TestChild] ADD CONSTRAINT [FK_TestParent_TestChild] FOREIGN KEY([TestParentID]) REFERENCES [dbo].[TestParent] ([TestParentID]) GO
The keen eye will notice that in the final sql-script primary and foreign key columns are introduced, which are not present in the original model (diagram nor xml file). The other way around, specification of associations in the original model is much richer than the resulting foreign key constraint in the sql-script. These “mismatches” must be handled and are typically present when performing O/RM mapping.
UML Use Case – Uses or Extends?
If you are a more functional focussed person, you are probably thinking: who cares. And if you are, please tell me why do you functional people talk so much with such a low information density? Is it thinking out loud?
Okay. Back to topic.
If you are a technical übermensch – like yours truly – you probably sh*t on people who do not see the delicate difference between a uses and an extends relationship.
In short, a tiny refresher for your brain.
- Uses (formerly known as include) indicates a “has a” relationship, and
- Extends (formerly known as extend) indicates an “is a” relationship
As you can see I still use pre-historic diagramming. Not only because I am wise and therefore somewhat older(fashioned), but merely because I use a freely available and (almost) open source diagramming tool StarUML. This tool unfortunately stuck at UML v1.3 Notwithstanding, an excellent tool.
Translated to a real world situation:
If you are a car driver you want to drive a car. Something you must include at least once is to start the engine. An extension of driving a car is driving a formula 1 car, which indeed is something very special.















