Filed under Clean code

Ensure.That – a simple guard clause project

Yes I know there’s a bunch of these projects out there already and that there’s built in support for using code-contracts in .Net, but even so we found a need for a custom API, hence Ensure.That was created. Also, I think Microsoft’s Code-contracts fails on the part of having to install an add-on to VS2010 to actually put the Code-contract requirements to use. Until it’s a first class citizen of VS, it’s not useful in my eyes. Enough about that.

Why

Easy, to provide a clean API for validating arguments and a solution that throws natural argument exceptions with informative exception messages.

NuGet

It’s distributed using NuGet. You could consume it as a normal lib or as a source distribution. The later gives you the possibility of incorporating it into any existing “common core lib” that you already use for these kind of utils.

Some code

The API is continuously evolving and it hasn’t reached v1.0 yet, hence there might be braking changes, but that’s not very likely. There will probably only be new stuff added.

Without parametername

Will throw ArgumentNullException or ArgumentException without ParameterName.

public void MyMethod(string myString, int myInt, Guid myGuid)
{
    Ensure.That(myString).IsNotNullOrEmpty();
    Ensure.That(myInt).IsInRange(10, 20);
    Ensure.That(myGuid).IsNotEmpty();
}

Using Lambdas

Note, that since the value also is extracted via the Lambda there’s a compile of it, hence the other overloads or more performant.

public void MyMethod(string myString, int myInt, Guid myGuid)
{
    Ensure.That(() => myString).IsNotNullOrEmpty();
    Ensure.That(() => myInt).IsInRange(10, 20);
    Ensure.That(() => myGuid).IsNotEmpty();
}

Providing parameter name explicitly

Will work as the first example with the difference of including the parameter name in the exception’s _ParameterName _property.

public void MyMethod(string myString, int myInt, Guid myGuid)
{
    Ensure.That(myString, "myString").IsNotNullOrEmpty();
    Ensure.That(myInt, "myInt").IsInRange(10, 20);
    Ensure.That(myGuid, "myGuid").IsNotEmpty();
}

More information and sourcecode is available on the GitHub site.

//Daniel

Tagged , ,

C#, Clean up your Linq-queries and lambda expressions for Non Linq to objects

This is an update to my post “C#, Clean up your Linq-queries and lambda expressions”, where I got a comment that it will not work other than against Linq to objects. That is somewhat true. It will work but the query will fetch all posts from the database and then apply the where clause to objects in memory which should trigger a sign DANGER!. In my case I only needed in memory but I thought, why not provide an example….

This example will be targetting Entity framework Code first, CTP 5.

The test

[Test]
public void AgainstEfCodeFirstCtp5()
{
    using (var dbContext = new StorageContext())
    {
        DbDatabase.SetInitializer(new DropCreateDatabaseAlways<StorageContext>());

        foreach (var customer in _customers)
            dbContext.Customers.Add(customer);

        dbContext.SaveChanges();
    }

    using (var dbContext = new StorageContext())
    {
        //Will not call the database
        var bonusCustomers = dbContext.Customers.Where(new IsBonusCustomer());

        //Will invoke call to database
        CollectionAssert.AreEqual(_expectedBonusCustomers.Select(c => c.Id), bonusCustomers.Select(c => c.Id));
    }
}

The sql-query will not be executed against the database until I access the deferred executable collection “bonusCustomers”. The query will look like this (interceptet with the Sql Profiler).

SELECT 
[Extent1].[Id] AS [Id]
FROM [dbo].[Customers] AS [Extent1]
WHERE
(
	(0 = [Extent1].[NumOfYearsAsMember])
	AND
	([Extent1].[CashSpent] >= 3000)
)
OR
(
	([Extent1].[NumOfYearsAsMember] > 0)
	AND 
	(
		([Extent1].[CashSpent] / [Extent1].[NumOfYearsAsMember]) >= 5000
	)
)

It matches our rule of the c-sharp code, but the generated Sql will of course brake, since it will give a divide by zero for the customers with less than one year of membership, but that is not the point of this blog post.

The changes from the last blog post

Instead of letting the implicit operator return a Func<T> it now returns Expression<Func<T>>. To let the Evaluate member work I also compiled the lambda to a Func.

public class ExpressionRule2<T>
    : IRule<T> where T : class
{
    public Expression<Func<T, bool>> Expression { get; private set; }

    public Func<T, bool> Compiled { get; private set; }

    public ExpressionRule2(Expression<Func<T, bool>> expression)
    {
        Expression = expression;
        Compiled = expression.Compile();
    }

    public static implicit operator Expression<Func<T, bool>>(ExpressionRule2<T> item)
    {
        return item.Expression;
    }

    public bool Evaluate(T item)
    {
        return Compiled(item);
    }
}

Yes, I know, not the best naming with ExpressionRule2 but it’s a quick hack to get the blog post compatible with non linq to object sources. There’s actually nothing more to it. Of course I have put up a custom DbContext for Entity framework Code-first CTP 5, as well as added an Id to the customer.

public class StorageContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }

        public StorageContext()
            : base(@"data source=.;initial catalog=NiceLambdas;integrated security=SSPI;")
        {
        }
    }

public class Customer
{
    public int Id { get; set; }

    public int NumOfYearsAsMember { get; set; }
    public int CashSpent { get; set; }
}

Why this solution at all?

What this gives me:

  • A name to my rule.
  • I can have interfaces represent it and have a factory or IoC returning variations of the rule.
  • I can isolate it for testing.

Happy coding!

//Daniel

Tagged , , , ,

C#, Clean up your Linq-queries and lambda expressions

Part 2How to get it to work against non Linq to object sources

Last week something caught my eyes. How scattered business logic can become if you let your binary expressions be used “here and there” when matching entities against certain rules. Logic for a single entity, e.g Customer can be used all over your codebase. This article will show you a way to get around this and to get a central repository of rules/predicates that can be applied to entities and makes your daily unit test writing easier. I will use simple operator overloading for accomplishing this.

The example code can be downloaded here.

I have put together a testfixture with test going from ugly code to “more” beautiful code. The example is fictive and therefore not the best example for what I’m showing. The problem below might be best solved with simple code as Customer.IsBonusCustomer() which then contains the logic for determening this. But I want to show you a way to handle this if you instead want the logic outside the customer and don’t want the lambdas/linq queries scattered all over your codebase.

In the code I have made use of Func but you can of course use Expression<Func> if you need to parse the expression tree.

The problem

Identify customers that are considered to be bonus customers.

Rules:

  • A customer that has been member shorter than one year and has spent 3000 or more in our store.
  • A customer that has been member for one year or more and where money spent divided by the years is equal to or greater than 5000.

The solution

Lets start with the entity example which of course intentionally have been made simple.

public class Customer
{
    public int NumOfYearsAsMember { get; set; }
    public int CashSpent { get; set; }
}

The ugly ways

First, lets be clear: “No I don’t mean the Linq or Lambda syntax in it self when I say ugly”. I mean ugly as you get “small” (in best case) pieces of logic lying around in your queries.

[Test]
public void TheUglyWayUsingLinq()
{
    var bonusCustomers = 
        from c in _customers
        where
        (c.NumOfYearsAsMember == 0 && c.CashSpent >= 3000) ||
        (c.NumOfYearsAsMember > 0 && (c.CashSpent / c.NumOfYearsAsMember) >= 5000)
        select c;

    CollectionAssert.AreEqual(_expectedBonusCustomers, bonusCustomers);
}

[Test]
public void TheUglyWayUsingLambdas()
{
    var bonusCustomers = _customers.Where(c =>
        (c.NumOfYearsAsMember == 0 && c.CashSpent >= 3000) ||
        (c.NumOfYearsAsMember > 0 && (c.CashSpent / c.NumOfYearsAsMember) >= 5000));

    CollectionAssert.AreEqual(_expectedBonusCustomers, bonusCustomers);
}

Both of these examples are rather simple (seen to what you meet in different enterprise applications) but yet rather hard to read. Well of course not if you concentrate on just the algorithm but as a whole it is. First lets make it somewhat nicer. Lets extract the selectors so that we get a name in the Linq and Lambda expressions.

[Test]
public void SomewhatNicerUsingLinq()
{
    Func<Customer, bool> isFirstYearBonusCustomer = c => 
        c.NumOfYearsAsMember == 0 && c.CashSpent >= 3000;

    Func<Customer, bool> isAfterFirstYearBonusCustomer = c => 
        c.NumOfYearsAsMember > 0 && ((c.CashSpent / c.NumOfYearsAsMember) >= 5000);

    var bonusCustomers = from c in _customers
                            where isFirstYearBonusCustomer(c) ||
                            isAfterFirstYearBonusCustomer(c)
                            select c;

    CollectionAssert.AreEqual(_expectedBonusCustomers, bonusCustomers);
}

[Test]
public void SomewhatNicerUsingLambdas()
{
    Func<Customer, bool> isFirstYearBonusCustomer = c =>
        c.NumOfYearsAsMember == 0 && c.CashSpent >= 3000;

    Func<Customer, bool> isAfterFirstYearBonusCustomer = c =>
        c.NumOfYearsAsMember > 0 && ((c.CashSpent / c.NumOfYearsAsMember) >= 5000);

    var bonusCustomers = _customers.Where(c =>
        isFirstYearBonusCustomer(c) ||
        isAfterFirstYearBonusCustomer(c));

    CollectionAssert.AreEqual(_expectedBonusCustomers, bonusCustomers);
}

Slightly better and if we put the selectors in a class they can be reused.

The more beautiful way

Now lets write a test in a way I would like to read it.

[Test]
public void MaybeMoreBeautifulUsingRules()
{
    var bonusCustomers = _customers.Where(new IsBonusCustomer());

    CollectionAssert.AreEqual(_expectedBonusCustomers, bonusCustomers);
}

How can I accomplish this?

Well it’s easy. Create a class that implements a implicit operator overloading for Func which then allows you to pass instances of this class into the IEnumerable.Where function.

public class ExpressionRule<T> 
    : IRule<T> where T : class
{
    public Func<T, bool> Expression { get; private set; }

    public ExpressionRule(Func<T, bool> expression)
    {
        Expression = expression;
    }

    public static implicit operator Func<T, bool>(ExpressionRule<T> item)
    {
        return item.Expression;
    }

    public bool Evaluate(T item)
    {
        return Expression(item);
    }
}

The next step is to create one representing the rule at hand IsBonusCustomer.

public class IsBonusCustomer 
    : ExpressionRule<Customer>, IIsBonusCustomer
{
    public IsBonusCustomer()
        : base(c =>
                (c.NumOfYearsAsMember == 0 && c.CashSpent >= 3000) ||
                (c.NumOfYearsAsMember > 0 && (c.CashSpent / c.NumOfYearsAsMember) >= 5000))
    {
    }
}

Again, if you need to parse the expression tree the ExpressionRule class would look something like this.

public class ExpressionRule<T>
    : IRule<T> where T : class
{
    public Func<T, bool> Expression { get; private set; }

    public ExpressionRule(Expression<Func<T, bool>> expression)
    {
        //Do something with the lambda
        //...
        //...
        Expression = expression.Compile();
    }

    public static implicit operator Func<T, bool>(ExpressionRule<T> item)
    {
        return item.Expression;
    }

    public bool Evaluate(T item)
    {
        return Expression(item);
    }
}

Easier to test

By having the solution above you open up your code to be easy to test. Lets say we have a PriceLocator that uses this rule to determine the price for a customer. We want to test the discount algorithm and be sure that the rule for determining if a customer is a bonus customer, always evaluates to true.

public class PriceCalculator
{
    public IIsBonusCustomer IsBonusCustomer;

    public PriceCalculator()
    {
        IsBonusCustomer = new IsBonusCustomer();
    }

    public decimal Calculate(decimal basePrice, Customer customer)
    {
        return IsBonusCustomer.Evaluate(customer)
                    ? basePrice * 0.8M : basePrice;
    }
}

Now I can put up a fake and inject it to the price locator, so that the code under test becomes isolated.

[Test]
public void CanBeEasilyBeFaked()
{
    var alwaysBonusCustomerRuleFake = new Mock<IIsBonusCustomer>();
    alwaysBonusCustomerRuleFake.Setup(f => f.Evaluate(It.IsAny<Customer>())).Returns(true);

    var priceCalculator = new PriceCalculator { IsBonusCustomer = alwaysBonusCustomerRuleFake.Object };
    var priceWithDiscount = priceCalculator.Calculate(100, new Customer());

    Assert.AreEqual(80, priceWithDiscount);
}

I can now of course also test the IsBonusCustomer rule.

[Test]
public void CanBeTestedInIsolation()
{
    var firstYearBonusCustomer = new Customer { CashSpent = 3000, NumOfYearsAsMember = 0 };

    var rule = new IsBonusCustomer();

    ExpressionAssert.EvalAsTrue(rule, firstYearBonusCustomer);
}

That’s it. Again the examples above might be overkill since you could have placed methods directly on the Customer, but….

Why this solution at all?

What this gives me:

  • A name to my rule.
  • I can have interfaces represent it and have a factory or IoC returning variations of the rule.
  • I can isolate it for testing.

//Daniel

Tagged , ,

C# and abusive dynamic APIs

I read a blogpost last night about dynamic code and how it could be used to handle arguments passed in to a method, and it got me thinking. I exchanged a couple of tweets with about this and I really didn’t get any wiser, hence I guess I have to go and try out some Ruby, but thats another day.

The thing I don’t get is how someone, in a public API designed for use by others, generally can think dynamic args or a collection of args can be received by a method which in turn just selects X of the passed in args without even bother looking at the rest of them? How are the consumer supposed to know what to name the keys/members to get their elements processed? If a method takes a collection of arguments, I as a consumer, kind of expect all the elements to be visited. I don’t wan’t to crank up Reflector and look at the source to see what I should name the keys and members to get them processed.

Lets start with an over exaggerated example using say a generic Dictionary.

public class AddressParser
{
    public string BuildAddress1(Dictionary<string, string> args)
    {
        return string.Format("Street: {0}\r\nZip:{1} City: {2}\r\nCountry: {3}.",
            args["Street"], args["Zip"], args["City"], args["Country"]);
    }

    public string BuildAddress2(dynamic args)
    {
        return string.Format("Street: {0}\r\nZip:{1} City: {2}\r\nCountry: {3}.",
            args.Street, args.ZipCode, args.City, args.Country);
    }
}

So, how would you as a consumer know that the args being processed are:”Street, ZipCode, City and Country”. Should you test all of the common names of address attributes? “StreetName, StreetNumber, Zip, Area….”

I asked:

And how would I now as a consumer that every element has been visited?

Because you’re an awesome tester and you don’t rely on your API method signatures to tell you what to do.

So pull up your sleeves and put on that monkey suite. It’s time to do some exploratory API programming.

To be fair, the dynamic example has one huge advantage. If I miss to pass something to the Dictionary method I get a not so informative KeyNotFoundException. If I instead use the dynamic example I get a more detailed exception:

'f__AnonymousType0' does not contain a definition for 'Country'

Ok, of course you could be a good citizen and document your method but we all know where that ends up.

I don’t know about you, but in this scenario, I don’t want to be forced to be a Curious George programmer.

//Daniel

Tagged

Use extension methods to let your enums hold the logic.

I often stumble upon code checking values on enumerations to determine the state of some object or rule. At best, this code is extracted and put in a helper or utils class of some sort. Don’t! Use extension methods instead. It let’s you provide a name for the condition (rule) that you are checking, and it put’s it together with the enumeration.

Example
Lets say I have an enumeration with storage providers. Each provider might be able to store data virtually, hence I need the possibility of determining if it’s capable of this.

[Serializable]
public enum StorageProviders
{
    LuceneIo = 0,
    LuceneVirtual = 1
}

public static class StorageProvidersExtensions
{
    public static bool IsVirtual(this StorageProviders provider)
    {
        return provider == StorageProviders.LuceneVirtual;
    }
}

I can now act on the enumeration value it self.

...connectionInfo.ProviderType.IsVirtual()...

//Daniel

Tagged ,

Where are your Scenarios in your domain?

If you design your entities before you design your scenarios, you are data-driven! The Scenarios are primary – The entities are secondary!

- Daniel Wertheim

My words of what I think is wrong in many projects, and thats what this post is about.

Definition of Scenario

a postulated sequence of possible events

- http://wordnetweb.princeton.edu/perl/webwn?s=scenario

In computing, a scenario is a narrative describing foreseeable interactions of types of users (characters) and the system. Scenarios include information about goals, expectations, motivations, actions and reactions. Scenarios are neither predictions nor forecasts, but rather attempts to reflect on or portray the way in which a system is used in the context of daily activity.

http://en.wikipedia.org/wiki/Scenario_(computing)

I tend to bump into situations where I meet people that are proclaiming they are adapting DDD in their projects and that they are writing object-oriented systems. As I’m always interested in learning, I fire of a couple of questions about what they mean by DDD and OO. I will not bother you with the dialog, but will rather skip to a summarize of the conversation. and present five of the artifacts that they think significates that they are conforming to DDD which I think is well worth to notice and discuss about:

  • They are object-oriented
    Meaning “that they are not using datasets anymore to keep track of the state and simple validation.”
  • They have POCO entities
    Meaning “that they have an OR/M that to some extent hides some of the artifacts of dealing with persisting the entities.”
  • They have repositories
    Meaning “there’s at least one repository per aggregate root (most often more), dealing with CRUDs.”
  • They have domain-services
    Meaning “they have some objects named [Entity][Service/Manager] which contains logic and calls to repositories”.
  • They have a model
    Meaning “they have a bunch of the above objects collaborating together”.
  • Ok, so I have designed system like these to. I will probably do it again. But lets look at what I mean by “systems like these” and what I think is wrong.

    Whats wrong?
    So I think we tend to create a model with an emphasis on entities, which more or less becomes a representation of tables in a traditional database. I like to define these entities as “an intelligent data-row” and by intelligent I mean that it might have simple validation of state and some logic for aggregating values, e.g. “Sum of the contained order lines in the order”. Furthermore we tend to create a bunch of services/managers that is named: “[Entity][Service/Manager]“; which to some extent holds together the flow – which most often means, they do some simple validation/rule-evaluation and they interact with a repository. I think these Services/Manager-classes arise from a mental mind of the developer looking like this: “The entity must be POCO and can’t have anything to do with persisting entities. I’ll just create a service that calls the repository and since I’m working with my order-entity, I’ll name it “OrderService”.

    Again, we end up here because we are thinking objects and state and how the objects correlate to each other. And when sitting there, performing TDD with a shallow understanding of the domain; it’s probably the easiest way to get started. Classic example: “Customer places a new order” => “OrderService.PlacerOrder” which receives an order entity with some contained lines and a customer. So my question to you then is: “What exactly is OrderService in the business”? If it is the customer that makes the request, why can’t it be “Customer.PlaceNewOrder”? Or a domain-object named something that mimics the scenario/process, e.g.: “PlaceNewOrder” or “OrderRegistration”.

    To sum it all up: The scenarios/processes in the domain is the core of the business. It’s what make the business unique and is what they are competing with against other competitors. This is what should drive your design. Without a fair understanding of the domain you will risk, getting data centric. The Domain should drive the design of your model, not TDD. Get a deep knowledge of the business and it’s processes and not only the binary rules and the state. These are the aspects that should be reflected in your code.

    //Daniel

    Tagged , , , ,

    The Moq, mockingframework, and the tangling of Stubs and Mocks

    When I need the powers of a Faking-framework I use the Moq-framework, which I use when I feel that it is to much work for creating a manual Fake (which I only do with stubs). There’s one thing that I really don’t like with Moq, the tangling of Mocks and Stubs. I like to see them as follows:

    Fakes

  • Stubs (is used to make interaction work and to return or throw exceptions etc.)
  • Mocks (is used to assert against by putting demands/expectations on the interaction)
  • This is why I wrote “Faking-framework” above. A stub is a fake and a mock is a fake but a mock is not a stub and a stub is not a mock. Glad that we sorted that out!

    Don’t get me wrong here, I like the Moq-framework, but what I would like to see in it is a separation of stubs and mocks. The API should let you clearly create a stub or a mock. You shouldn’t be able to turn a stub into a mock. When creating the stub, a class named Mock is confusing and missleading hence should not be used.

    Look at the following simple test. I have a shoppingcart for a customer and the cart uses a pricelocator to lookup the prices of the products that I’m adding. The price is determined by looking at the customer, the product and the quantity. In the test I create a stub for my pricelocator, so that I’m sure of the prices it will return. I stub the pricelocator so that the test can query it for two different products for the same customer. Then I add two items to the shoppingcart, which will use the pricelocator when I invoke the GetTotal-function on my shoppingcart.

    The test

    [TestClass]
    public class ShoppingCartTests
    {
        [TestMethod]
        public void GetTotal_TwoValidShoppingCartItems_GivesTotal()
        {
            const string customerNo = "2010-1";
            var item1 = new { CustomerNo = customerNo, ProductNo = "P01-01-00001", Quantity = 2, Result = 101.50M };
            var item2 = new { CustomerNo = customerNo, ProductNo = "P01-01-00002", Quantity = 3, Result = 99.75M };
    
            var expectedTotal = item1.Result + item2.Result;
            var priceLocatorStub = GetPriceLocatorStub(item1, item2);
    
            var shoppingCart = new ShoppingCart(customerNo) { PriceLocator = priceLocatorStub };
            shoppingCart.AddProduct(item1.ProductNo, item1.Quantity);
            shoppingCart.AddProduct(item2.ProductNo, item2.Quantity);
            var actualTotal = shoppingCart.GetTotal();
    
            Assert.AreEqual(expectedTotal, actualTotal);
        }
    
        private static IPriceLocator GetPriceLocatorStub(params object[] items)
        {
            var priceLocatorStub = new Moq.Mock<IPriceLocator>();
    
            foreach (var item in items)
            {
                var tmp = TypeCaster.CastAnonymous(item, new { CustomerNo = "", ProductNo = "", Quantity = 0, Result = 0M });
    
                priceLocatorStub
                    .Setup(pl => pl.LookupPrice(
                                     tmp.CustomerNo,
                                     tmp.ProductNo,
                                     tmp.Quantity)).Returns(tmp.Result);
            }
    
            return priceLocatorStub.Object;
        }
    }
    

    What if I add a expectation to my stub? It is possible, but should it be? Yes it’s possible. With the little change of adding “.AtMost(0)” below, I have created an mock and totally changed the semantics, hence the Assert should change to, but that’s not the point in this blog post. The point is that I think the Moq guys should keep the mocks and stubs API’s sepparated, so when I create a stub I can’t add expectations.

    priceLocatorStub
        .Setup(pl => pl.LookupPrice(
                         tmp.CustomerNo,
                         tmp.ProductNo,
                         tmp.Quantity)).Returns(tmp.Result).AtMost(0);
    

    The changes I want are:

    Instead of:

    var stub = new Mock();
    

    I want:

    var stub = new Stub();
    

    and not being able to add e.g. AtMost-expectations to the Stub.

    Even if this change will not come, I will continue to use the Moq-framework.

    To finnish of, I would like to point out that I think this missuse of the terms mocks and mocking is common amongst developers; who gladly uses variablenames like “*Mock” when they actually are creating a stub; who frequently speaks in terms of mocks, when they actually are using stubs.

    The complete sourcecode can be downloaded from here.

    //Daniel

    Tagged , , ,

    Extend IQueryable instead of a certain dataprovider – more decoupled code

    This is going to be a real short post and is more of an update to my last post (Entity validaton using Custom Data Annotation attributes) then a new one. I have made two small technical changes and with these small changes I have gained a more decoupled code.

    Change One
    Instead of having a helper method in my service base class I have put it in the entity store. This due to the fact that the validation is mere simple datavalidation and is pure validation of the entity’s state. Custom validation logic is still in my services but this logic is injected to the validation method in the entity store, via a Func<>.

    Change Two
    I don’t make use of repositories anymore. Instead I have a generic entity store that can handle whichever entity you pass to it, as long as you have provided mapping information about the entity. But what if I want to have “Named queries”, e.g GetAllUsersThatAreBlocked? In this case, I don’t create a custom implementation of my entity store by inheriting the generic implementation and creates the function “GetAllUsersThatAreBlocked”. No, I use my generic implementation that lets me get an IQueryable<T>, which I then extend. In my previous post I extended the entity store, which was really ugly. This new solution decouples my queries from the entity store and gives me the opportunity to execute the queries against any source that can provide me an IQueryable<T>.

    The validation
    The code below shows a Service-method and how it calls the ValidateEntity-method (which now is placed in the EntityStore).

    public ServiceResponse<UserAccount> SetupNewUserAccount(UserAccount userAccount)
    {
        var validationResult = EntityStore.ValidateEntity<UserAccount>(userAccount, CustomValidationForSettingUpNewAccount);
        var serviceResponse = new ServiceResponse<UserAccount>(userAccount, validationResult);
        
        if (!serviceResponse.ValidationResult.HasViolations)
        {
            EntityStore.AddEntity(userAccount);
            EntityStore.SaveChanges();
        }
    
        return serviceResponse;
    }
    
    private IEnumerable<ValidationResult> CustomValidationForSettingUpNewAccount(UserAccount userAccount)
    {
        var violations = new List<ValidationResult>();
        var emailIsTaken = EntityStore.Query<UserAccount>().EmailIsTakenByOther(userAccount.Username, userAccount.Email);
    
        if (emailIsTaken)
            violations.Add(new ValidationResult("Email is allready taken."));
    
        return violations;
    }
    

    The ValidateEntityMethod is as before. Where it uses a EntityValidator that I have written about before. To perform custom validation you can pass in a Func<> which is done above, where I pass a pointer to method “CustomValidationForSettingUpNewAccount” in my service. This function only ensures that the email isn’t allready in use.

    public EntityValidationResult ValidateEntity<T>(T entity, Func<T, IEnumerable<ValidationResult>> customValidation = null)
        where T : IEntity
    {
        Func<T, bool, IEnumerable<ValidationResult>> customValidationProxy = null;
    
        if (customValidation != null)
            customValidationProxy = (e, isValid) => isValid ? customValidation(e) : null;
    
        return new EntityValidator<T>().Validate(entity, customValidationProxy);
    }
    

    Named queries as extensions to IQueryable<T>
    The extension code is the same as in earliear post except that I now extend IQueryable instead of EfEntityStore and can hook on the where clause directly on the injected queryable. A verry little syntax change but a tremendous architechtural change since, the query extension now has no dependency to a certain implementation technique.

    namespace Sds.Christmas.Storage.Queries.UserAccounts
    {
        public static class UserAccountQueries
        {
            public static bool EmailIsTakenByOther(this IQueryable<UserAccount> userAccounts, string username, string email)
            {
                return
                    userAccounts.Where(
                        u =>
                            u.Username.Equals(username, StringComparison.InvariantCultureIgnoreCase) &&
                            u.Email.Equals(email, StringComparison.InvariantCultureIgnoreCase)).Count() > 0;
            }
        }
    }
    

    As always, there’s a complete sample project available for download.

    Enjoy!

    //Daniel

    Tagged , ,

    Does code have to be Localized for achieving a “Ubiquitous Language”?

    Recently I have encountered C# code containing names found in the business that the applications maps to. So far so good except that the names of objects, methods, properties, functions etc. were written in Swedish and of course with special characters like “Å, Ä, Ö”

    My question is “Does code has to be localized for achieving an Ubiquitous Language?

    In my eyes the answer is “No”, even if the corporate language in the business is Swedish, I think that the code should be written in English. Why? Well…for starters: You lose internationalization. What if developers that doesn’t understand Swedish have to work with and understand the code? Another downside is that English and Swedish will be tangled up and even if it’s a Swedish developer that reads the code he/she will probably feel confused since the “normal” language used when writing software is English. So finding e.g. Swedish verbs for functions etc. will be confusing. And for me “Confusion is the result of unclear intent of the code”.

    Personally I would go with 100% English, even if we get terms that doesn’t match the Swedish counterpart to 100%, yes it will require more of the developer whom will have to have two versions of the domain-model in their mind, but rather that than unclear code. And finally to answer my question, I don’t think that the localization of the code affects the degree of how Ubiquitous Language you have achieved. What affects this is how well the structure of the domain-model maps to the business.

    //Daniel

    Tagged ,
    Follow

    Get every new post delivered to your Inbox.