English 中文(简体)
How do I validate a property that needs to access a repository in xVal?
原标题:

I m trying to use xVal to validate the registration of a new user. I ve ran into a buzz saw when trying to implement the logic that checks to see if the user name that the new user is trying to register with is already taken. I can t seem to find a way to accomplish this without having my User entity have a dependence on my UsersRepository. Here s the method I need to find a way to implement in the my User Entity:

public IEnumerable<ErrorInfo> ValidateUniqueUserName(string username)
{
    if(usersRepository.Users.Exists(m => (m.UserName == username)))
        yield return new ErrorInfo("UserName", "User name already exists");
}

Any ideas on how I can continue to use xVal for this scenario and keep my User entity decoupled from my UsersRepository?

最佳回答

DDD would suggest that you might have a Domain Service to abstract the Users repo from the domain validation (invariant enforcing).

I d like to know where the code from your example above resides (validation service)? But i d suggest you make sure it is in the domain. Here is a clever way to attach complex validation to entities that still support the IDataErrorInfo interface.

What i would suggest is an Domain Service within your validation method that returns your Users.Exists query. Something like:

        base.AddRule(new ValidationRule()
        {
            Properties = "username",
            Description = "User name already exists",
            validator = () => !(new UserService()).Users.Exists(m => (m.UserName == username))
        });

In the example above i will be using DI to inject the appropriate dependencies into the UserService for accessing of the repo/data but you could use a factory or manual DI object creation method if you like:

        base.AddRule(new ValidationRule()
        {
            Properties = "username",
            Description = "User name already exists",
            validator = () =>
                {
                    UserService us = ObjectFactory.GetInstance<UserService>();
                    return !us.Users.Exists(m => (m.UserName == username));
                }
        });

NOTE: The above method requires the validator property to be set to false to indicate an invalid state (in case that wasn t clear).

问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签