AspnetProfile Partial Class

This is the base partial data object class for the “profile” table in the provider database. Using this class implies that there will be a singular relationship between the remaining provider tables and relevant data objects.

  1.  //*****************************************************************************
  2. // This file is part of the data access layer example to the ASP.NET 2.0 provider database
  3. // This file was written by Adam Buenz [WSS MVP] of ARB Security Solutions, LLC
  4. //                  http://www.sharepointsecurity.com
  5. //
  6. // This file and its parts is free for re-distribution, for use in both free
  7. // and commercial applications, however this header must remain intact for legal
  8. // use. The data access layer example is distributed in the hope that it will
  9. // be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. //*****************************************************************************
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Data;
  15. using System.Data.SqlClient;
  16. using Aspnet.Provider.Datalayer.Commands;
  17. using Aspnet.Provider.Datalayer.DataTransferObjects;
  18.  
  19. namespace Aspnet.Provider.Datalayer
  20. {
  21.         /// <summary>
  22.         /// The AspnetProfile class.
  23.         /// </summary>
  24.         public partial class AspnetProfile : IPersistable
  25.         {
  26.                 #region Members
  27.  
  28.                 private bool isNew;
  29.                 private bool _isNew;
  30.                 private Guid userid;
  31.                 private string propertynames;
  32.                 private string propertyvaluesstring;
  33.                 private byte[] propertyvaluesbinary;
  34.                 private DateTime lastupdateddate;
  35.  
  36.                 #endregion
  37.  
  38.                 #region Properties
  39.  
  40.                 /// <summary>
  41.                 /// The Userid.
  42.                 /// </summary>
  43.                 public virtual Guid Userid
  44.                 {
  45.                         get { return this.userid; }
  46.                         set { this.userid = value; }
  47.                 }
  48.  
  49.                 /// <summary>
  50.                 /// The Propertynames.
  51.                 /// </summary>
  52.                 public virtual string Propertynames
  53.                 {
  54.                         get { return this.propertynames; }
  55.                         set { this.propertynames = value; }
  56.                 }
  57.  
  58.                 /// <summary>
  59.                 /// The Propertyvaluesstring.
  60.                 /// </summary>
  61.                 public virtual string Propertyvaluesstring
  62.                 {
  63.                         get { return this.propertyvaluesstring; }
  64.                         set { this.propertyvaluesstring = value; }
  65.                 }
  66.  
  67.                 /// <summary>
  68.                 /// The Propertyvaluesbinary.
  69.                 /// </summary>
  70.                 public virtual byte[] Propertyvaluesbinary
  71.                 {
  72.                         get { return this.propertyvaluesbinary; }
  73.                         set { this.propertyvaluesbinary = value; }
  74.                 }
  75.  
  76.                 /// <summary>
  77.                 /// The Lastupdateddate.
  78.                 /// </summary>
  79.                 public virtual DateTime Lastupdateddate
  80.                 {
  81.                         get { return this.lastupdateddate; }
  82.                         set { this.lastupdateddate = value; }
  83.                 }
  84.  
  85.                 #endregion
  86.  
  87.                 #region ColumnNames
  88.  
  89.                 /// <summary>
  90.                 /// The corresponding schema name.
  91.                 /// </summary>
  92.                 internal const string SchemaName = "dbo";
  93.  
  94.                 /// <summary>
  95.                 /// The corresponding table name.
  96.                 /// </summary>
  97.                 internal const string TableName = "aspnet_Profile";
  98.  
  99.                 /// <summary>
  100.                 /// The column names.
  101.                 /// </summary>
  102.                 internal class ColumnNames
  103.                 {
  104.                         /// <summary>
  105.                         /// The column name of the Userid property.
  106.                         /// </summary>
  107.                         public const string Userid="UserId";
  108.                         /// <summary>
  109.                         /// The column name of the Propertynames property.
  110.                         /// </summary>
  111.                         public const string Propertynames="PropertyNames";
  112.                         /// <summary>
  113.                         /// The column name of the Propertyvaluesstring property.
  114.                         /// </summary>
  115.                         public const string Propertyvaluesstring="PropertyValuesString";
  116.                         /// <summary>
  117.                         /// The column name of the Propertyvaluesbinary property.
  118.                         /// </summary>
  119.                         public const string Propertyvaluesbinary="PropertyValuesBinary";
  120.                         /// <summary>
  121.                         /// The column name of the Lastupdateddate property.
  122.                         /// </summary>
  123.                         public const string Lastupdateddate="LastUpdatedDate";
  124.                 }
  125.  
  126.                 #endregion
  127.  
  128.                 /// <summary>
  129.                 /// The default constructor.
  130.                 /// </summary>
  131.                 public AspnetProfile()
  132.                 {
  133.                         this.isNew = true;
  134.                         PersistenceManager.InvokeInstanceMethod(this, "CreationComplete", null);
  135.                 }
  136.  
  137.                 /// <summary>
  138.                 /// The constructor of the required fields.
  139.                 /// </summary>
  140.                 /// <param name="userid">The Userid.</param>
  141.                 /// <param name="propertynames">The Propertynames.</param>
  142.                 /// <param name="propertyvaluesstring">The Propertyvaluesstring.</param>
  143.                 /// <param name="propertyvaluesbinary">The Propertyvaluesbinary.</param>
  144.                 /// <param name="lastupdateddate">The Lastupdateddate.</param>
  145.                 public AspnetProfile(Guid userid, string propertynames, string propertyvaluesstring, byte[] propertyvaluesbinary, DateTime lastupdateddate)
  146.                 {
  147.                         this.userid = userid;
  148.                         this.propertynames = propertynames;
  149.                         this.propertyvaluesstring = propertyvaluesstring;
  150.                         this.propertyvaluesbinary = propertyvaluesbinary;
  151.                         this.lastupdateddate = lastupdateddate;
  152.  
  153.                         this.isNew = true;
  154.                         PersistenceManager.InvokeInstanceMethod(this, "CreationComplete", null);
  155.                 }
  156.  
  157.                 /// <summary>
  158.                 /// The constructor from IDataReader.
  159.                 /// </summary>
  160.                 /// <param name="reader">An initalized IDataReader.</param>
  161.                 internal AspnetProfile(IDataReader reader)
  162.                 {
  163.                         if ((reader["UserId"] != null) && (reader["UserId"] != DBNull.Value))
  164.                         this.userid = (Guid)reader["UserId"];
  165.                         if ((reader["PropertyNames"] != null) && (reader["PropertyNames"] != DBNull.Value))
  166.                         this.propertynames = (string)reader["PropertyNames"];
  167.                         if ((reader["PropertyValuesString"] != null) && (reader["PropertyValuesString"] != DBNull.Value))
  168.                         this.propertyvaluesstring = (string)reader["PropertyValuesString"];
  169.                         if ((reader["PropertyValuesBinary"] != null) && (reader["PropertyValuesBinary"] != DBNull.Value))
  170.                         this.propertyvaluesbinary = (byte[])reader["PropertyValuesBinary"];
  171.                         if ((reader["LastUpdatedDate"] != null) && (reader["LastUpdatedDate"] != DBNull.Value))
  172.                         this.lastupdateddate = (DateTime)reader["LastUpdatedDate"];
  173.  
  174.                         this.isNew = false;
  175.                 }
  176.  
  177.                 /// <summary>
  178.                 /// Creates an IDbCommand to insert an object into the database.
  179.                 /// </summary>
  180.                 /// <returns>An initialized IDbCommand object.</returns>
  181.                 internal virtual IDbCommand CreateInsertCommand()
  182.                 {
  183.                         SqlCommand cmd = new SqlCommand();
  184.                         cmd.CommandText = "insert into [dbo].[aspnet_Profile] ([UserId], [PropertyNames], [PropertyValuesString], [PropertyValuesBinary], [LastUpdatedDate]) values (@userid, @propertynames, @propertyvaluesstring, @propertyvaluesbinary, @lastupdateddate)";
  185.                         cmd.Parameters.AddWithValue("@userid", this.userid);
  186.                         cmd.Parameters.AddWithValue("@propertynames", this.propertynames);
  187.                         cmd.Parameters.AddWithValue("@propertyvaluesstring", this.propertyvaluesstring);
  188.                         cmd.Parameters.AddWithValue("@propertyvaluesbinary", this.propertyvaluesbinary);
  189.                         cmd.Parameters.AddWithValue("@lastupdateddate", this.lastupdateddate);
  190.  
  191.                         cmd.Connection = PersistenceManager.Connection;
  192.                         return cmd;
  193.                 }
  194.  
  195.                 /// <summary>
  196.                 /// Creates an IDbCommand to update an object in the database.
  197.                 /// </summary>
  198.                 /// <returns>An initialized IDbCommand object.</returns>
  199.                 internal virtual IDbCommand CreateUpdateCommand()
  200.                 {
  201.                         SqlCommand cmd = new SqlCommand();
  202.                         cmd.CommandText = "update [dbo].[aspnet_Profile] set [PropertyNames]=@propertynames, [PropertyValuesString]=@propertyvaluesstring, [PropertyValuesBinary]=@propertyvaluesbinary, [LastUpdatedDate]=@lastupdateddate where ([UserId]=@userid)";
  203.                         cmd.Parameters.AddWithValue("@propertynames", this.propertynames);
  204.                         cmd.Parameters.AddWithValue("@propertyvaluesstring", this.propertyvaluesstring);
  205.                         cmd.Parameters.AddWithValue("@propertyvaluesbinary", this.propertyvaluesbinary);
  206.                         cmd.Parameters.AddWithValue("@lastupdateddate", this.lastupdateddate);
  207.                         cmd.Parameters.AddWithValue("@userid", this.userid);
  208.  
  209.                         cmd.Connection = PersistenceManager.Connection;
  210.                         return cmd;
  211.                 }
  212.  
  213.                 /// <summary>
  214.                 /// Creates an IDbCommand to delete an object in the database.
  215.                 /// </summary>
  216.                 /// <returns>An initialized IDbCommand object.</returns>
  217.                 internal virtual IDbCommand CreateDeleteCommand()
  218.                 {
  219.                         SqlCommand cmd = new SqlCommand();
  220.                         cmd.CommandText = "delete from [dbo].[aspnet_Profile]  where ([UserId]=@userid)";
  221.                         cmd.Parameters.AddWithValue("@userid", this.userid);
  222.  
  223.                         cmd.Connection = PersistenceManager.Connection;
  224.                         return cmd;
  225.                 }
  226.  
  227.                 /// <summary>
  228.                 /// Persists the object.
  229.                 /// </summary>
  230.                 public virtual void Persist()
  231.                 {
  232.                         PersistenceManager.InvokeInstanceMethod(this, "PrePersist", null);
  233.  
  234.                         IDbCommand cmd;
  235.  
  236.                         if (this.isNew)
  237.                         cmd = this.CreateInsertCommand();
  238.                         else
  239.                         cmd = this.CreateUpdateCommand();
  240.  
  241.                         cmd.Transaction = PersistenceManager.Transaction;
  242.                         PersistenceManager.RegisterInTransaction(this);
  243.  
  244.                         bool connWasClosed =
  245.                         PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
  246.                         PersistenceManager.Connection.State.Equals(ConnectionState.Broken);
  247.  
  248.                         try
  249.                         {
  250.                                 if (connWasClosed)
  251.                                 PersistenceManager.Connection.Open();
  252.  
  253.                                 cmd.ExecuteNonQuery();
  254.  
  255.                                 this.isNew = false;
  256.  
  257.                                 if (connWasClosed)
  258.                                 PersistenceManager.Connection.Close();
  259.                         }
  260.                         catch (Exception ex)
  261.                         {
  262.                                 if (connWasClosed)
  263.                                 PersistenceManager.Connection.Close();
  264.                                 throw ex;
  265.                         }
  266.                 }
  267.  
  268.                 /// <summary>
  269.                 /// Deletes the object.
  270.                 /// </summary>
  271.                 public virtual void Delete()
  272.                 {
  273.                         PersistenceManager.InvokeInstanceMethod(this, "PreDelete", null);
  274.  
  275.                         IDbCommand cmd = this.CreateDeleteCommand();
  276.                         cmd.Transaction = PersistenceManager.Transaction;
  277.  
  278.                         bool connWasClosed =
  279.                         PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
  280.                         PersistenceManager.Connection.State.Equals(ConnectionState.Broken);
  281.  
  282.                         try
  283.                         {
  284.                                 if (connWasClosed)
  285.                                 PersistenceManager.Connection.Open();
  286.  
  287.                                 cmd.ExecuteNonQuery();
  288.  
  289.                                 if (connWasClosed)
  290.                                 PersistenceManager.Connection.Close();
  291.                         }
  292.                         catch (Exception ex)
  293.                         {
  294.                                 if (connWasClosed)
  295.                                 PersistenceManager.Connection.Close();
  296.                                 throw ex;
  297.                         }
  298.                 }
  299.  
  300.                 /// <summary>
  301.                 /// Retrieves a AspnetProfile object by its primary key (Throws System.DataException).
  302.                 /// </summary>
  303.                 /// <param name="userid">The Userid.</param>
  304.                 /// <returns>The AspnetProfile object.</returns>
  305.                 public static AspnetProfile Get(Guid userid)
  306.                 {
  307.                         AspnetProfile aspnetprofile;
  308.                         if (AspnetProfile.TryGet(userid, out aspnetprofile))
  309.                         return aspnetprofile;
  310.                         else
  311.                         throw new DataException("'AspnetProfile' object not found.");
  312.                 }
  313.  
  314.                 /// <summary>
  315.                 /// Tries to retrieve a AspnetProfile object by its primary key.
  316.                 /// </summary>
  317.                 /// <param name="userid">The Userid.</param>
  318.                 /// <param name="aspnetprofile">The found AspnetProfile or null if the primary key value does not exist.</param>
  319.                 /// <returns>True if the AspnetProfile exists, else false.</returns>
  320.                 public static bool TryGet(Guid userid, out AspnetProfile aspnetprofile)
  321.                 {
  322.                         SqlCommand cmd = new SqlCommand("select * from [dbo].[aspnet_Profile]  where ([UserId]=@userid)");
  323.                         cmd.Parameters.AddWithValue("@userid", userid);
  324.  
  325.                         IList<AspnetProfile> list = AspnetProfile.Query(cmd);
  326.  
  327.                         if (list.Count == 0)
  328.                         {
  329.                                 aspnetprofile = null;
  330.                                 return false;
  331.                         }
  332.                         else
  333.                         {
  334.                                 aspnetprofile = list[0];
  335.                                 return true;
  336.                         }
  337.                 }
  338.  
  339.                 /// <summary>
  340.                 /// For internal use only.
  341.                 /// </summary>
  342.                 public virtual void SaveState()
  343.                 {
  344.                         this._isNew = this.isNew;
  345.                 }
  346.  
  347.                 /// <summary>
  348.                 /// For internal use only.
  349.                 /// </summary>
  350.                 public virtual void RestoreState()
  351.                 {
  352.                         this.isNew = this._isNew;
  353.                 }
  354.  
  355.                 /// <summary>
  356.                 /// Refreshes the internal state of the object.
  357.                 /// </summary>
  358.                 /// <remarks>This method should be called after xml deserialization to refresh internal flags.</remarks>
  359.                 public virtual void RefreshState()
  360.                 {
  361.                         AspnetProfile aspnetprofile;
  362.  
  363.                         if (AspnetProfile.TryGet(this.userid, out aspnetprofile))
  364.                         {
  365.                                 this.isNew = false;
  366.                         }
  367.                         else
  368.                         {
  369.                                 this.isNew = true;
  370.                         }
  371.                 }
  372.  
  373.                 /// <summary>
  374.                 /// Returns a Data Transfer Object of this AspnetProfile.
  375.                 /// </summary>
  376.                 /// <returns>A Data Transfer Object of this AspnetProfile.</returns>
  377.                 public virtual AspnetProfileDTO GetDTO()
  378.                 {
  379.                         AspnetProfileDTO dto = new AspnetProfileDTO();
  380.  
  381.                         dto.Userid = this.Userid;
  382.                         dto.Propertynames = this.Propertynames;
  383.                         dto.Propertyvaluesstring = this.Propertyvaluesstring;
  384.                         dto.Propertyvaluesbinary = this.Propertyvaluesbinary;
  385.                         dto.Lastupdateddate = this.Lastupdateddate;
  386.  
  387.                         return dto;
  388.                 }
  389.  
  390.                 /// <summary>
  391.                 /// Applies a Data Transfer Object data to this AspnetProfile.
  392.                 /// </summary>
  393.                 /// <param name="dto">The Data Transfer Object.</param>
  394.                 public virtual void SetDTO(AspnetProfileDTO dto)
  395.                 {
  396.                         this.Userid = dto.Userid;
  397.                         this.Propertynames = dto.Propertynames;
  398.                         this.Propertyvaluesstring = dto.Propertyvaluesstring;
  399.                         this.Propertyvaluesbinary = dto.Propertyvaluesbinary;
  400.                         this.Lastupdateddate = dto.Lastupdateddate;
  401.                 }
  402.  
  403.                 /// <summary>
  404.                 /// Perfoms a query on AspnetProfile objects.
  405.                 /// </summary>
  406.                 /// <param name="command">An IDbCommand containing the select statement.</param>
  407.                 /// <returns>A result list of AspnetProfile objects.</returns>
  408.                 internal static IList<AspnetProfile> Query(IDbCommand command)
  409.                 {
  410.                         command.Connection = PersistenceManager.Connection;
  411.                         command.Transaction = PersistenceManager.Transaction;
  412.  
  413.                         bool connWasClosed =
  414.                         PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
  415.                         PersistenceManager.Connection.State.Equals(ConnectionState.Broken);
  416.  
  417.                         try
  418.                         {
  419.                                 if (connWasClosed)
  420.                                 PersistenceManager.Connection.Open();
  421.  
  422.                                 List<AspnetProfile> list = new List<AspnetProfile>();
  423.                                 IDataReader reader = command.ExecuteReader();
  424.  
  425.                                 using (reader)
  426.                                 {
  427.                                         while (reader.Read())
  428.                                         list.Add(new AspnetProfile(reader));
  429.                                 }
  430.  
  431.                                 if (connWasClosed)
  432.                                 PersistenceManager.Connection.Close();
  433.  
  434.                                 return list;
  435.                         }
  436.                         catch (Exception ex)
  437.                         {
  438.                                 if (connWasClosed)
  439.                                 PersistenceManager.Connection.Close();
  440.                                 throw ex;
  441.                         }
  442.                 }
  443.  
  444.                 /// <summary>
  445.                 /// Finds all AspnetProfile objects with a certain Propertynames value.
  446.                 /// </summary>
  447.                 /// <param name="propertynames">The Propertynames value ('*' can be used as a wildcard).</param>
  448.                 /// <returns>All AspnetProfile objects with a certain Propertynames value.</returns>
  449.                 public static IList<AspnetProfile> FindByPropertynames(string propertynames)
  450.                 {
  451.                         return AspnetProfile.Query(AspnetProfileCommands.FindByPropertynames(propertynames));
  452.                 }
  453.  
  454.                 /// <summary>
  455.                 /// Finds all AspnetProfile objects with a certain Propertyvaluesstring value.
  456.                 /// </summary>
  457.                 /// <param name="propertyvaluesstring">The Propertyvaluesstring value ('*' can be used as a wildcard).</param>
  458.                 /// <returns>All AspnetProfile objects with a certain Propertyvaluesstring value.</returns>
  459.                 public static IList<AspnetProfile> FindByPropertyvaluesstring(string propertyvaluesstring)
  460.                 {
  461.                         return AspnetProfile.Query(AspnetProfileCommands.FindByPropertyvaluesstring(propertyvaluesstring));
  462.                 }
  463.  
  464.                 /// <summary>
  465.                 /// Finds all AspnetProfile objects with a certain Propertyvaluesbinary value.
  466.                 /// </summary>
  467.                 /// <param name="propertyvaluesbinary">The Propertyvaluesbinary value.</param>
  468.                 /// <returns>All AspnetProfile objects with a certain Propertyvaluesbinary value.</returns>
  469.                 public static IList<AspnetProfile> FindByPropertyvaluesbinary(byte[] propertyvaluesbinary)
  470.                 {
  471.                         return AspnetProfile.Query(AspnetProfileCommands.FindByPropertyvaluesbinary(propertyvaluesbinary));
  472.                 }
  473.  
  474.                 /// <summary>
  475.                 /// Finds all AspnetProfile objects with a certain Lastupdateddate value.
  476.                 /// </summary>
  477.                 /// <param name="lastupdateddate">The Lastupdateddate value.</param>
  478.                 /// <returns>All AspnetProfile objects with a certain Lastupdateddate value.</returns>
  479.                 public static IList<AspnetProfile> FindByLastupdateddate(DateTime lastupdateddate)
  480.                 {
  481.                         return AspnetProfile.Query(AspnetProfileCommands.FindByLastupdateddate(lastupdateddate));
  482.                 }
  483.  
  484.         }
  485. }
share save 171 16 AspnetProfile Partial Class

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>