Skip to content

Instantly share code, notes, and snippets.

@HEskandari
Created January 11, 2012 07:30
Show Gist options
  • Save HEskandari/1593555 to your computer and use it in GitHub Desktop.
Save HEskandari/1593555 to your computer and use it in GitHub Desktop.
Failing test for Windsor 3.0
// Copyright 2004-2012 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Castle.Windsor.Tests
{
using System;
using System.ComponentModel;
using System.Reflection;
using Castle.DynamicProxy;
using Castle.Facilities.TypedFactory;
using NUnit.Framework;
using Component = Castle.MicroKernel.Registration.Component;
[TestFixture]
public class ResolvingComponentUsingTypedFactoryTestCase
{
[Test]
public void Should_Resolve_ValidatorFactoryImpl_And_Act_As_Factory()
{
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<Customer>().Proxy.AdditionalInterfaces(typeof(IDataErrorInfo)).Interceptors(typeof(ModelInterceptor)).LifeStyle.Transient)
.Register(Component.For<IValidator<Customer>>().ImplementedBy<CustomerValidator>().LifeStyle.Transient)
.Register(Component.For<ValidatorSelector>().LifeStyle.Transient)
.Register(Component.For<IValidatorFactory>().AsFactory(c => c.SelectedWith<ValidatorSelector>()).LifeStyle.Transient)
.Register(Component.For<ModelInterceptor>().LifeStyle.Transient);
var validator = container.Resolve<IValidator<Customer>>();
Assert.IsNotNull(validator);
var factory = container.Resolve<IValidatorFactory>();
var viaFactory = factory.GetValidator<Customer>();
Assert.NotNull(viaFactory);
}
public delegate Customer CustomerFactory(string number);
public class Customer
{
public string Number { get; set; }
}
public interface IValidator
{
bool Validate(object model);
}
public interface IValidator<T> : IValidator
{
bool Validate(T model);
}
public abstract class AbstractValidator<T> : IValidator<T>
{
public abstract bool Validate(T model);
public bool Validate(object model)
{
T typedModel = (T)model;
return Validate(typedModel);
}
}
public class CustomerValidator : AbstractValidator<Customer>
{
public override bool Validate(Customer model)
{
//Validation code here
return true;
}
}
public class ValidatorSelector : DefaultTypedFactoryComponentSelector
{
protected override Type GetComponentType(MethodInfo method, object[] arguments)
{
return method.ReturnType;
}
}
public interface IValidatorFactory
{
IValidator<T> GetValidator<T>();
}
public class ModelInterceptor : IInterceptor
{
private readonly IValidatorFactory validatorFactory;
public ModelInterceptor(IValidatorFactory validatorFactory)
{
this.validatorFactory = validatorFactory;
}
public void Intercept(IInvocation invocation)
{
if (invocation.Method.DeclaringType == typeof(IDataErrorInfo))
{
var validator = validatorFactory.GetValidator<Customer>();
var result = validator.Validate(invocation.Proxy);
if (result) //object is valid
{
invocation.ReturnValue = string.Empty;
return;
}
if (invocation.Method.Name == "get_Item") //Get the error for specific property
{
var propertyName = (string)invocation.Arguments[0]; //property name to validate
invocation.ReturnValue = propertyName;
}
else if (invocation.Method.Name == "get_Error") //Get all errors of the object
{
invocation.ReturnValue = "Error";
}
}
else
{
//Other methods not belonging to IDataErrorInfo,
//so just invoke the method.
invocation.Proceed();
}
}
}
}
}
@kkozmic
Copy link

kkozmic commented Jan 11, 2012

There's a breaking change that "Get" methods no longer implicitly fallback to resolve by type if the name is not matched. You need to add the following if you want that behaviour or rename the GetValidator method to something like CreateValidator

        public ValidatorSelector():base(getMethodsResolveByName:false)
        {
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment