开发者

ActiveRecordValidationBase<T> not throwing ActiveRecordValidationException on Save. Model invalid

Unsolved problem....ActiveRecordValidationBase does not throw ActiveRecordValidationException on Save (Update, as well )... But, call Create throws ActiveRecordValidationException...I really don't understand why...sample code below.

[Web.Config]

<castle>
    <facilities>
        <facility
                id="ActiveRecordFacility"
                type="Castle.Facilities.ActiveRecordIntegration.ActiveRecordFacility, Castle.Facilities.ActiveRecordIntegration"
                isDebug="true"
                isWeb="true">
            <assemblies>
                <item>Base.Models</item>
            </assemblies>
            <config>
                <add key="connection.driver_class"      value="NHibernate.Driver.SqlClientDriver"/>
                <add key="dialect"                      value="NHibernate.Dialect.MsSql2005Dialect"/>
                <add key="connection.connection_string" value="ConnectionString = ${connstr}"/>
                <add key="connection.provider"          value="NHibernate.Connection.DriverConnectionProvider"/>
                <add key="proxyfactory.factory_class"   value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
                <add key="hbm2ddl.auto"                 value="update"/>
                <add key="connection.release_mode"      value="on_close" />
                <add key="use_outer_join"               value="true"/>
                <add key="use_proxy_validator"          value="true"/>
                <add key="hbm2ddl.keywords"             value="auto-quote"/>
                <add key="use_reflection_optimizer"     value="true"/>
                <add key="bytecode.provider"            value="lcg"/>
                <add key="max_fetch_depth"              value="2"/>
                <add key="show_sql"                     value="false"/>
            </config>
        </facility>
    </facilities>
</castle>

<system.web>
    <httpModules>
        <add name="SessionScopeWebModule" type="Castle.ActiveRecord.Framework.SessionScopeWebModule, Castle.ActiveRecord.Web"/>
    </httpModu开发者_运维技巧les>
</system.web>

[Model (Permission - Only Properties and validation)]

#region Public Virtual Properties

[PrimaryKey("permission_id", Generator = PrimaryKeyType.GuidComb)]
public virtual Guid Id { get; set; }

[ActiveRecordProperty("area", Length = 256, SqlType = "NVARCHAR(256)", UniqueKey = "UQ_PERMISSION")]
[ValidateRegExp(@"^\D\w+$", ErrorMessageKey = "Permission_Area_Regexp", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
public virtual string Area { get; set; }

[ActiveRecordProperty("controller", Length = 256, SqlType = "NVARCHAR(256)", UniqueKey = "UQ_PERMISSION")]
[ValidateNonEmpty(ErrorMessageKey = "Permission_Controller_NonEmpty", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
[ValidateRegExp(@"^\D\w+$", ErrorMessageKey = "Permission_Controller_Regexp", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
public virtual string Controller { get; set; }

[ActiveRecordProperty("action", Length = 256, SqlType = "NVARCHAR(256)", UniqueKey = "UQ_PERMISSION")]
[ValidateNonEmpty(ErrorMessageKey = "Permission_Action_NonEmpty", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
[ValidateRegExp(@"^\D\w+$", ErrorMessageKey = "Permission_Action_Regexp", ResourceType = typeof(Common.L10n.ModelValidations.Messages))]
public virtual string Action { get; set; }

[ActiveRecordProperty("date_created")]
public virtual DateTime DateCreated { get; set; }

[ActiveRecordProperty("date_modified")]
public virtual DateTime DateModified { get; set; }

[HasAndBelongsToMany(Cascade = ManyRelationCascadeEnum.All, ColumnKey = "permission_id", ColumnRef = "role_id", Lazy = true, MapType = typeof(Role), Table = "permissions_in_roles")]
[ValidateCollectionNotEmpty]
public virtual IList<Role> Roles { get; set; }

#endregion

[Controller - Save Action]

[HttpPost]
public ActionResult Save(Permission permission)
{
    try
    {
        if (permission.Id == Guid.Empty)
            permission.Create();
        else
            permission.Update();

        // permission.Save();

        // Call Update or Save, with invalid model and does not throw invalid exception (???)
        // Create seems ok.
    }
    catch (Exception ex)
    {
        TempData["permission"] = permission;
        TempData["exception"] = ex;

        if (permission.Id == Guid.Empty)
            return RedirectToAction("Create");

        return RedirectToAction("Edit", new { id = permission.Id });
    }

    return RedirectToAction("List", new { scope = SearchScope.Restricted, id = permission.Id });
}

[ModelBinder]

public sealed class PermissionModelBinder : IModelBinder
{
    #region IModelBinder Members

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var request = controllerContext.HttpContext.Request;
        var model = Permission.TryFind(request.Get<Guid>("permission.Id")) ?? new Permission();
        var now = DateTime.Now;

        model.Area = request.Get<string>("permission.Area");
        model.Controller = request.Get<string>("permission.Controller");
        model.Action = request.Get<string>("permission.Action");

        if (model.DateCreated == DateTime.MinValue)
            model.DateCreated = now;

        model.DateModified = now;

        return model;
    }

    #endregion
}

Using MS Sql2005, ActiveRecord 3, NHibernate 3, ASP.NET MVC 2. Has anyone had a similar problem with which I am dealing with? Managed to solve?

EDIT: I realized that only when the entity is updated, the ActiveRecordValidationBase class does not call the BeforeSave(IDictionary state) method, but when the entity is created, the class calls it. Since this method checks whether the entity is valid or not. Is this a bug or some wrong configuration?? (Debugged with ActiveRecord source and NHibernate)

Thanks!

Added sample in: ARVBErrorSample.zip

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜