Funky stuff with Generate from XML Template

Funky stuff with Generate from XML Template

Old forum URL: forums.lhotka.net/forums/t/1792.aspx


wjcomeaux posted on Friday, November 17, 2006

What's happening is if I generate agains the XML object below I get a CSLA class that does not have a property for CompanyID and the stored procedures used are the default assigned in TemplateBase.cs. I have found that it's my XML for the ValidationRules segments. If I remove these tags, the class generates just fine. However, this is very strange behavior from the generator.
Do we have access to a XSD for the XML so that we can see exactly what a valid XML file would be?
Thanks, Will
- <Objects>
- <Object Access="public" Type="EditableRoot" Name="BuildingER" NameSpace="">
- <Properties>
- <Property Name="ID" Type="int" DbColumnName="ID" Default="" IsPrimaryKey="true" IsIdentity="true">
- <ValidationRules>
  <Required />
  </ValidationRules>
  </Property>
- <Property Name="Name" Type="string" DbColumnName="Name" Default="">
- <ValidationRules>
  <StringMaxLength>50</StringMaxLength>
  </ValidationRules>
  </Property>
  <Property Name="CompanyID" Type="int" DbColumnName="CompanyID" Default="" />
  </Properties>
- <DbCommands DbName="PTracker">
  <FetchCommand Type="StoredProcedure">gen_BuildingER_Fetch</FetchCommand>
  <InsertCommand>gen_BuildingER_Insert</InsertCommand>
  <UpdateCommand>gen_BuildingER_Update</UpdateCommand>
  <DeleteCommand>gen_BuildingER_Delete</DeleteCommand>
  </DbCommands>
  </Object>
  </Objects>

rasupit replied on Tuesday, November 21, 2006

I have no problem generating your data.  Xsd file (CslaProject.xsd) is available, you can see the example (mysample.xml) on how to use it.
===== output =========
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;

[Serializable()]
public class BuildingER : Csla.BusinessBase<BuildingER>
{
    #region Business Properties and Methods

    //declare members
    private int _id = 0;
    private string _name = string.Empty;
    private int _companyID = 0;

    [System.ComponentModel.DataObjectField(true, true)]
    public int ID
    {
        get
        {
            CanReadProperty("ID", true);
            return _id;
        }
    }

    public string Name
    {
        get
        {
            CanReadProperty("Name", true);
            return _name;
        }
        set
        {
            CanWriteProperty("Name", true);
            if (value == null) value = string.Empty;
            if (!_name.Equals(value))
            {
                _name = value;
                PropertyHasChanged("Name");
            }
        }
    }

    public int CompanyID
    {
        get
        {
            CanReadProperty("CompanyID", true);
            return _companyID;
        }
        set
        {
            CanWriteProperty("CompanyID", true);
            if (!_companyID.Equals(value))
            {
                _companyID = value;
                PropertyHasChanged("CompanyID");
            }
        }
    }
 
    protected override object GetIdValue()
    {
        return _id;
    }

    #endregion //Business Properties and Methods

    #region Validation Rules
    private void AddCustomRules()
    {
        //add custom/non-generated rules here...
    }

    private void AddCommonRules()
    {
        //
        // Name
        //
        ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs("Name", 50));
    }

    protected override void AddBusinessRules()
    {
        AddCommonRules();
        AddCustomRules();
    }
    #endregion //Validation Rules

    #region Authorization Rules
    protected override void AddAuthorizationRules()
    {
        //TODO: Define authorization rules in BuildingER
        //AuthorizationRules.AllowRead("ID", "BuildingERReadGroup");
        //AuthorizationRules.AllowRead("Name", "BuildingERReadGroup");
        //AuthorizationRules.AllowRead("CompanyID", "BuildingERReadGroup");

        //AuthorizationRules.AllowWrite("Name", "BuildingERWriteGroup");
        //AuthorizationRules.AllowWrite("CompanyID", "BuildingERWriteGroup");
    }


    public static bool CanGetObject()
    {
        //TODO: Define CanGetObject permission in BuildingER
        return true;
        //if (Csla.ApplicationContext.User.IsInRole("BuildingERViewGroup"))
        //    return true;
        //return false;
    }

    public static bool CanAddObject()
    {
        //TODO: Define CanAddObject permission in BuildingER
        return true;
        //if (Csla.ApplicationContext.User.IsInRole("BuildingERAddGroup"))
        //    return true;
        //return false;
    }

    public static bool CanEditObject()
    {
        //TODO: Define CanEditObject permission in BuildingER
        return true;
        //if (Csla.ApplicationContext.User.IsInRole("BuildingEREditGroup"))
        //    return true;
        //return false;
    }

    public static bool CanDeleteObject()
    {
        //TODO: Define CanDeleteObject permission in BuildingER
        return true;
        //if (Csla.ApplicationContext.User.IsInRole("BuildingERDeleteGroup"))
        //    return true;
        //return false;
    }
    #endregion //Authorization Rules

    #region Factory Methods
    private BuildingER()
    { /* require use of factory method */ }

    public static BuildingER NewBuildingER()
    {
        if (!CanAddObject())
            throw new System.Security.SecurityException("User not authorized to add a BuildingER");
        return DataPortal.Create<BuildingER>();
    }

    public static BuildingER GetBuildingER(int id)
    {
        if (!CanGetObject())
            throw new System.Security.SecurityException("User not authorized to view a BuildingER");
        return DataPortal.Fetch<BuildingER>(new Criteria(id));
    }

    public static void DeleteBuildingER(int id)
    {
        if (!CanDeleteObject())
            throw new System.Security.SecurityException("User not authorized to remove a BuildingER");
        DataPortal.Delete(new Criteria(id));
    }

    public override BuildingER Save()
    {
        if (IsDeleted && !CanDeleteObject())
            throw new System.Security.SecurityException("User not authorized to remove a BuildingER");
        else if (IsNew && !CanAddObject())
            throw new System.Security.SecurityException("User not authorized to add a BuildingER");
        else if (!CanEditObject())
            throw new System.Security.SecurityException("User not authorized to update a BuildingER");

        return base.Save();
    }

    #endregion //Factory Methods

    #region Data Access

    #region Criteria

    [Serializable()]
    private class Criteria
    {
        public int ID;

        public Criteria(int id)
        {
            this.ID = id;
        }
    }

    #endregion //Criteria

    #region Data Access - Create
    [RunLocal]
    protected override void DataPortal_Create()
    {
        ValidationRules.CheckRules();
    }

    #endregion //Data Access - Create

    #region Data Access - Fetch
    private void DataPortal_Fetch(Criteria criteria)
    {
        using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
        {
            cn.Open();

            ExecuteFetch(cn, criteria);
        }//using
    }

    private void ExecuteFetch(SqlConnection cn, Criteria criteria)
    {
        using (SqlCommand cm = cn.CreateCommand())
        {
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "gen_BuildingER_Fetch";

            cm.Parameters.AddWithValue("@ID", criteria.ID);

            using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }//using
    }

    private void FetchObject(SafeDataReader dr)
    {
        dr.Read();
        _id = dr.GetInt32("ID");
        _name = dr.GetString("Name");
        _companyID = dr.GetInt32("CompanyID");
    }

    private void FetchChildren(SafeDataReader dr)
    {
    }
    #endregion //Data Access - Fetch

    #region Data Access - Insert
    protected override void DataPortal_Insert()
    {
        using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
        {
            cn.Open();

            ExecuteInsert(cn);

            //update child object(s)
            UpdateChildren(cn);
        }//using

    }

    private void ExecuteInsert(SqlConnection cn)
    {
        using (SqlCommand cm = cn.CreateCommand())
        {
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "gen_BuildingER_Insert";

            AddInsertParameters(cm);

            cm.ExecuteNonQuery();

            _id = (int)cm.Parameters["@NewID"].Value;
        }//using
    }

    private void AddInsertParameters(SqlCommand cm)
    {
        cm.Parameters.AddWithValue("@Name", _name);
        cm.Parameters.AddWithValue("@CompanyID", _companyID);
        cm.Parameters.AddWithValue("@NewID", _id);
        cm.Parameters["@NewID"].Direction = ParameterDirection.Output;
    }
    #endregion //Data Access - Insert

    #region Data Access - Update
    protected override void DataPortal_Update()
    {
        using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
        {
            cn.Open();

            if (base.IsDirty)
            {
                ExecuteUpdate(cn);
            }

            //update child object(s)
            UpdateChildren(cn);
        }//using

    }

    private void ExecuteUpdate(SqlConnection cn)
    {
        using (SqlCommand cm = cn.CreateCommand())
        {
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "gen_BuildingER_Update";

            AddUpdateParameters(cm);

            cm.ExecuteNonQuery();

        }//using
    }

    private void AddUpdateParameters(SqlCommand cm)
    {
        cm.Parameters.AddWithValue("@ID", _id);
        cm.Parameters.AddWithValue("@Name", _name);
        cm.Parameters.AddWithValue("@CompanyID", _companyID);
    }

    private void UpdateChildren(SqlConnection cn)
    {
    }
    #endregion //Data Access - Update

    #region Data Access - Delete
    protected override void DataPortal_DeleteSelf()
    {
        DataPortal_Delete(new Criteria(_id));
    }

    private void DataPortal_Delete(Criteria criteria)
    {
        using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
        {
            cn.Open();

            ExecuteDelete(cn, criteria);

        }//using

    }

    private void ExecuteDelete(SqlConnection cn, Criteria criteria)
    {
        using (SqlCommand cm = cn.CreateCommand())
        {
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = "gen_BuildingER_Delete";

            cm.Parameters.AddWithValue("@ID", criteria.ID);

            cm.ExecuteNonQuery();
        }//using
    }
    #endregion //Data Access - Delete
    #endregion //Data Access
}


Copyright (c) Marimer LLC