Problem getting started with FluentValidation - asp.net-mvc-3

I am attempting to use FluentValidation 2.0 with an MVC 3 project. I have followed the instructions here to install FV within the project.
This is my validator class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentValidation;
namespace HandicapTracker.Models.Validation
{
public class TournamentValidator : AbstractValidator<Tournament>
{
public TournamentValidator()
{
RuleFor(x => x.CourseId).NotEmpty();
RuleFor(x => x.TournamentDate).NotEmpty().NotEqual(new DateTime());
}
}
}
Here is where I attempt to use the attribute:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using HandicapTracker.Configuration;
using HandicapTracker.Models.Validation;
using Omu.ValueInjecter;
using FluentValidation;
using HandicapTracker.Models.Validation;
namespace HandicapTracker.Models
{
[Validator(typeof(TournamentValidator))]
public class Tournament : HandicapTrackerModelBase
{
private const int VisitingTeamIndex = 0;
private const int HomeTeamIndex = 1;
public Tournament() : this (new League())
{
}
....
}
However, the attribute is not being recognized. When I build I get the following error message:
"System.ComponentModel.DataAnnotations.Validator" is not an attribute class.
I have actually tried this on two different solutions and am having the same problem on both. It's probably something trivial, but I can't figure it out.
Can someone tell me what i am doing wrong?
Thanks.

It looks like your [Validator] attribute is picking up on another class called Validator in the System.ComponentModel.DataAnnotations namespace. Try fully qualifying the attribute.
[FluentValidation.Attributes.Validator(typeof(TournamentValidator))]
Otherwise, revise your using statements to avoid the Validator name collision.

Dont use the FluentVallidation namespace directly, it should be implemented from servicestack. So you can write it as
using Servicestack.FluentValidation;

Related

WPF Designer Extensibility: how to get the current designer zoom value?

I created a simple custom control with design-time support like the one described here
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.ComponentModel;
namespace CustomControlLibrary
{
public class ButtonWithDesignTime : Button
{
public ButtonWithDesignTime()
{
// The GetIsInDesignMode check and the following design-time
// code are optional and shown only for demonstration.
if (DesignerProperties.GetIsInDesignMode(this))
{
Content = "Design mode active";
}
}
}
}
Now I need to know what is the zoom factor set at design time by Visual Studio, but I cannot figure out how.
Is there an event that I can listen to for finding out this? Or is there a property that could reveal this?
I'm not sure if this is the most elegant way of doing it, but this is what I came up with. This problem has been thwarting me for ages!
private Point GetScaleFactor()
{
var rootVisual = PresentationSource.FromVisual(this)?.RootVisual;
if (rootVisual != null)
{
var transformToRoot = TransformToAncestor(rootVisual);
if (transformToRoot is Transform t)
{
return new Point(t.Value.M11, t.Value.M22);
}
}
return new Point(1, 1);
}

'securestorage' does not contain a definition for setasync

Following this tutorial, https://learn.microsoft.com/en-us/xamarin/essentials/secure-storage?tabs=android
I have installed Xamarin.Essentials and added using Xamarin.Essentials; as instructed but it is not in used.
I got this error: 'securestorage' does not contain a definition for setasync
Here is my code:
using System;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Xamarin_SQLite.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SecureStorage : ContentPage
{
public SecureStorage()
{
InitializeComponent();
try
{
SecureStorage.SetAsync("oauth_token", "secret-oauth-token-value");
}
catch (Exception ex)
{
// Possible that device doesn't support secure storage on device.
}
}
}
}
'securestorage' does not contain a definition for setasync
1) Your class name for the page is SecureStorage and that is where the error is coming from.
Change the class name and|or fully qualify the call to:
`Xamarin.Essentials.SecureStorage.SetAsync`
or create a using alias for Xamarin.Essentials and qualify the static method with that alias)
2) You need to await that call:
`await Xamarin.Essentials.SecureStorage.SetAsync...`

ToggleGroup in Unity3D, which is the active one?

How do we know which toggle is active from the toggle groups?
Unity Doc says use ToggleGroup.ActiveToggles but I can't understand how to use it?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Linq;
private void Example()
{
Toggle theActiveToggle = yourToggleGroup.ActiveToggles().FirstOrDefault();
Debug.Log("It worked! " + theActiveToggle.gameObject.name);
}

Does not contain a definition for 'where' and the best extension method overload

I've searched and found this error all over the stackoverflow forum, but it applies differently each time and I'd like someone help me in this particular matter.
public IEnumerable<Project> FindRange(string filterExpression, string sortingExpression, int startIndex, int count)
{
try
{
using (BusinessContext context = new BusinessContext())
{
if (!String.IsNullOrWhiteSpace(filterExpression))
return context.Projects
.Where(filterExpression)
The last line being the one of the error...
Do you have any idea why is this happening? I've already added all the usings and dlls needed, being:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
What am I missing? I've got code structured the same way, working...
Your filterexpression parameter should not be a string - it should be a Func<>.
See http://msdn.microsoft.com/en-us/library/system.linq.enumerable.where(v=vs.100).aspx
It looks like you may be trying to use the Dynamic Linq library. In that case you need to ensure that you've added the NuGet package to your project and then include this namespace:
using System.Linq.Dynamic;
If your BusinessContext class has a string property such as Name that you are wanting to filter on then your where expression should look something like
.Where(x => x.Name.ToLower().StartsWith(filterExpression.ToLower()))

LINQ not working on IEnumerable

I'm using Autofac (I've registered the base nuget package in a console app) and want to take a look at a list of registrations.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// First, create your application-level defaults using a standard
// ContainerBuilder, just as you are used to.
var builder = new ContainerBuilder();
var appContainer = builder.Build();
appContainer.ComponentRegistry.Registrations.Where(x => true);
}
}
}
The problem is the line
appContainer.ComponentRegistry.Registrations.Where(x => true);
Here intellisense is not giving me the Where linq method however it does compile as far as I can tell without any warnings, errors in messages.
I tried this further down
IEnumerable<string> list = new List<string>();
list.Where(x => true);
And intellisense is working correctly and giving me all the standard list methods.
I've tried this in a few different apps from scratch and I'm getting the same behaviour.
Any ideas as to whats going on?
EDIT:
The following works and shows correctly in intellisense
IEnumerable<IComponentRegistration> test = new List<IComponentRegistration>();
test.Where(x => true);
I'm using
<package id="Autofac" version="3.0.1" targetFramework="net45" /> from nuget.
and hovering over the ComponentRegistrations gives
and in this case scope is defined as
ILifetimeScope _scope;
However I get the same thing if i try directly off this
var builder = new ContainerBuilder();
var appContainer = builder.Build();
appContainer.ComponentRegistry.Registrations.Where(x => true);
Also IComponentRegistry is defined as (in Autofac)
public interface IComponentRegistry : IDisposable
{
...
IEnumerable<IComponentRegistration> Registrations { get; }
...
}
Comment copied to answer.
If I've understood you correctly and your problem is that intellisense isn't working on the line
appContainer.ComponentRegistry.Registrations.Where(x => true);
You should probably try and disable your addons and see if that takes care of it as it's working fine for me. Since you say you had someone else confirm it, maybe start with any addons that your installations have in common.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
// First, create your application-level defaults using a standard
// ContainerBuilder, just as you are used to.
var builder = new ContainerBuilder();
var appContainer = builder.Build();
var registrations = appContainer.ComponentRegistry.Registrations.Where(x => x.Target.Equals("test"));
}
}
}
Try assigning the linq expression to a variable and see if it works

Resources