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.

[csharp]

 //*****************************************************************************
// This file is part of the data access layer example to the ASP.NET 2.0 provider database
// This file was written by Adam Buenz [WSS MVP] of ARB Security Solutions, LLC
// http://www.sharepointsecurity.com
//
// This file and its parts is free for re-distribution, for use in both free
// and commercial applications, however this header must remain intact for legal
// use. The data access layer example is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//*****************************************************************************
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Aspnet.Provider.Datalayer.Commands;
using Aspnet.Provider.Datalayer.DataTransferObjects;

namespace Aspnet.Provider.Datalayer
{
///

/// The AspnetProfile class.
///

public partial class AspnetProfile : IPersistable
{
#region Members

private bool isNew;
private bool _isNew;
private Guid userid;
private string propertynames;
private string propertyvaluesstring;
private byte[] propertyvaluesbinary;
private DateTime lastupdateddate;

#endregion

#region Properties

///

/// The Userid.
///

public virtual Guid Userid
{
get { return this.userid; }
set { this.userid = value; }
}

///

/// The Propertynames.
///

public virtual string Propertynames
{
get { return this.propertynames; }
set { this.propertynames = value; }
}

///

/// The Propertyvaluesstring.
///

public virtual string Propertyvaluesstring
{
get { return this.propertyvaluesstring; }
set { this.propertyvaluesstring = value; }
}

///

/// The Propertyvaluesbinary.
///

public virtual byte[] Propertyvaluesbinary
{
get { return this.propertyvaluesbinary; }
set { this.propertyvaluesbinary = value; }
}

///

/// The Lastupdateddate.
///

public virtual DateTime Lastupdateddate
{
get { return this.lastupdateddate; }
set { this.lastupdateddate = value; }
}

#endregion

#region ColumnNames

///

/// The corresponding schema name.
///

internal const string SchemaName = “dbo”;

///

/// The corresponding table name.
///

internal const string TableName = “aspnet_Profile”;

///

/// The column names.
///

internal class ColumnNames
{
///

/// The column name of the Userid property.
///

public const string Userid=”UserId”;
///

/// The column name of the Propertynames property.
///

public const string Propertynames=”PropertyNames”;
///

/// The column name of the Propertyvaluesstring property.
///

public const string Propertyvaluesstring=”PropertyValuesString”;
///

/// The column name of the Propertyvaluesbinary property.
///

public const string Propertyvaluesbinary=”PropertyValuesBinary”;
///

/// The column name of the Lastupdateddate property.
///

public const string Lastupdateddate=”LastUpdatedDate”;
}

#endregion

///

/// The default constructor.
///

public AspnetProfile()
{
this.isNew = true;
PersistenceManager.InvokeInstanceMethod(this, “CreationComplete”, null);
}

///

/// The constructor of the required fields.
///

/// The Userid. /// The Propertynames. /// The Propertyvaluesstring. /// The Propertyvaluesbinary. /// The Lastupdateddate. public AspnetProfile(Guid userid, string propertynames, string propertyvaluesstring, byte[] propertyvaluesbinary, DateTime lastupdateddate)
{
this.userid = userid;
this.propertynames = propertynames;
this.propertyvaluesstring = propertyvaluesstring;
this.propertyvaluesbinary = propertyvaluesbinary;
this.lastupdateddate = lastupdateddate;

this.isNew = true;
PersistenceManager.InvokeInstanceMethod(this, “CreationComplete”, null);
}

///

/// The constructor from IDataReader.
///

/// An initalized IDataReader. internal AspnetProfile(IDataReader reader)
{
if ((reader[“UserId”] != null) && (reader[“UserId”] != DBNull.Value))
this.userid = (Guid)reader[“UserId”];
if ((reader[“PropertyNames”] != null) && (reader[“PropertyNames”] != DBNull.Value))
this.propertynames = (string)reader[“PropertyNames”];
if ((reader[“PropertyValuesString”] != null) && (reader[“PropertyValuesString”] != DBNull.Value))
this.propertyvaluesstring = (string)reader[“PropertyValuesString”];
if ((reader[“PropertyValuesBinary”] != null) && (reader[“PropertyValuesBinary”] != DBNull.Value))
this.propertyvaluesbinary = (byte[])reader[“PropertyValuesBinary”];
if ((reader[“LastUpdatedDate”] != null) && (reader[“LastUpdatedDate”] != DBNull.Value))
this.lastupdateddate = (DateTime)reader[“LastUpdatedDate”];

this.isNew = false;
}

///

/// Creates an IDbCommand to insert an object into the database.
///

/// An initialized IDbCommand object.
internal virtual IDbCommand CreateInsertCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “insert into [dbo].[aspnet_Profile] ([UserId], [PropertyNames], [PropertyValuesString], [PropertyValuesBinary], [LastUpdatedDate]) values (@userid, @propertynames, @propertyvaluesstring, @propertyvaluesbinary, @lastupdateddate)”;
cmd.Parameters.AddWithValue(“@userid”, this.userid);
cmd.Parameters.AddWithValue(“@propertynames”, this.propertynames);
cmd.Parameters.AddWithValue(“@propertyvaluesstring”, this.propertyvaluesstring);
cmd.Parameters.AddWithValue(“@propertyvaluesbinary”, this.propertyvaluesbinary);
cmd.Parameters.AddWithValue(“@lastupdateddate”, this.lastupdateddate);

cmd.Connection = PersistenceManager.Connection;
return cmd;
}

///

/// Creates an IDbCommand to update an object in the database.
///

/// An initialized IDbCommand object.
internal virtual IDbCommand CreateUpdateCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “update [dbo].[aspnet_Profile] set [PropertyNames]=@propertynames, [PropertyValuesString]=@propertyvaluesstring, [PropertyValuesBinary]=@propertyvaluesbinary, [LastUpdatedDate]=@lastupdateddate where ([UserId]=@userid)”;
cmd.Parameters.AddWithValue(“@propertynames”, this.propertynames);
cmd.Parameters.AddWithValue(“@propertyvaluesstring”, this.propertyvaluesstring);
cmd.Parameters.AddWithValue(“@propertyvaluesbinary”, this.propertyvaluesbinary);
cmd.Parameters.AddWithValue(“@lastupdateddate”, this.lastupdateddate);
cmd.Parameters.AddWithValue(“@userid”, this.userid);

cmd.Connection = PersistenceManager.Connection;
return cmd;
}

///

/// Creates an IDbCommand to delete an object in the database.
///

/// An initialized IDbCommand object.
internal virtual IDbCommand CreateDeleteCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = “delete from [dbo].[aspnet_Profile] where ([UserId]=@userid)”;
cmd.Parameters.AddWithValue(“@userid”, this.userid);

cmd.Connection = PersistenceManager.Connection;
return cmd;
}

///

/// Persists the object.
///

public virtual void Persist()
{
PersistenceManager.InvokeInstanceMethod(this, “PrePersist”, null);

IDbCommand cmd;

if (this.isNew)
cmd = this.CreateInsertCommand();
else
cmd = this.CreateUpdateCommand();

cmd.Transaction = PersistenceManager.Transaction;
PersistenceManager.RegisterInTransaction(this);

bool connWasClosed =
PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
PersistenceManager.Connection.State.Equals(ConnectionState.Broken);

try
{
if (connWasClosed)
PersistenceManager.Connection.Open();

cmd.ExecuteNonQuery();

this.isNew = false;

if (connWasClosed)
PersistenceManager.Connection.Close();
}
catch (Exception ex)
{
if (connWasClosed)
PersistenceManager.Connection.Close();
throw ex;
}
}

///

/// Deletes the object.
///

public virtual void Delete()
{
PersistenceManager.InvokeInstanceMethod(this, “PreDelete”, null);

IDbCommand cmd = this.CreateDeleteCommand();
cmd.Transaction = PersistenceManager.Transaction;

bool connWasClosed =
PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
PersistenceManager.Connection.State.Equals(ConnectionState.Broken);

try
{
if (connWasClosed)
PersistenceManager.Connection.Open();

cmd.ExecuteNonQuery();

if (connWasClosed)
PersistenceManager.Connection.Close();
}
catch (Exception ex)
{
if (connWasClosed)
PersistenceManager.Connection.Close();
throw ex;
}
}

///

/// Retrieves a AspnetProfile object by its primary key (Throws System.DataException).
///

/// The Userid. /// The AspnetProfile object.
public static AspnetProfile Get(Guid userid)
{
AspnetProfile aspnetprofile;
if (AspnetProfile.TryGet(userid, out aspnetprofile))
return aspnetprofile;
else
throw new DataException(“‘AspnetProfile’ object not found.”);
}

///

/// Tries to retrieve a AspnetProfile object by its primary key.
///

/// The Userid. /// The found AspnetProfile or null if the primary key value does not exist. /// True if the AspnetProfile exists, else false.
public static bool TryGet(Guid userid, out AspnetProfile aspnetprofile)
{
SqlCommand cmd = new SqlCommand(“select * from [dbo].[aspnet_Profile] where ([UserId]=@userid)”);
cmd.Parameters.AddWithValue(“@userid”, userid);

IList list = AspnetProfile.Query(cmd);

if (list.Count == 0)
{
aspnetprofile = null;
return false;
}
else
{
aspnetprofile = list[0];
return true;
}
}

///

/// For internal use only.
///

public virtual void SaveState()
{
this._isNew = this.isNew;
}

///

/// For internal use only.
///

public virtual void RestoreState()
{
this.isNew = this._isNew;
}

///

/// Refreshes the internal state of the object.
///

/// This method should be called after xml deserialization to refresh internal flags.
public virtual void RefreshState()
{
AspnetProfile aspnetprofile;

if (AspnetProfile.TryGet(this.userid, out aspnetprofile))
{
this.isNew = false;
}
else
{
this.isNew = true;
}
}

///

/// Returns a Data Transfer Object of this AspnetProfile.
///

/// A Data Transfer Object of this AspnetProfile.
public virtual AspnetProfileDTO GetDTO()
{
AspnetProfileDTO dto = new AspnetProfileDTO();

dto.Userid = this.Userid;
dto.Propertynames = this.Propertynames;
dto.Propertyvaluesstring = this.Propertyvaluesstring;
dto.Propertyvaluesbinary = this.Propertyvaluesbinary;
dto.Lastupdateddate = this.Lastupdateddate;

return dto;
}

///

/// Applies a Data Transfer Object data to this AspnetProfile.
///

/// The Data Transfer Object. public virtual void SetDTO(AspnetProfileDTO dto)
{
this.Userid = dto.Userid;
this.Propertynames = dto.Propertynames;
this.Propertyvaluesstring = dto.Propertyvaluesstring;
this.Propertyvaluesbinary = dto.Propertyvaluesbinary;
this.Lastupdateddate = dto.Lastupdateddate;
}

///

/// Perfoms a query on AspnetProfile objects.
///

/// An IDbCommand containing the select statement. /// A result list of AspnetProfile objects.
internal static IList Query(IDbCommand command)
{
command.Connection = PersistenceManager.Connection;
command.Transaction = PersistenceManager.Transaction;

bool connWasClosed =
PersistenceManager.Connection.State.Equals(ConnectionState.Closed) ||
PersistenceManager.Connection.State.Equals(ConnectionState.Broken);

try
{
if (connWasClosed)
PersistenceManager.Connection.Open();

List list = new List();
IDataReader reader = command.ExecuteReader();

using (reader)
{
while (reader.Read())
list.Add(new AspnetProfile(reader));
}

if (connWasClosed)
PersistenceManager.Connection.Close();

return list;
}
catch (Exception ex)
{
if (connWasClosed)
PersistenceManager.Connection.Close();
throw ex;
}
}

///

/// Finds all AspnetProfile objects with a certain Propertynames value.
///

/// The Propertynames value (‘*’ can be used as a wildcard). /// All AspnetProfile objects with a certain Propertynames value.
public static IList FindByPropertynames(string propertynames)
{
return AspnetProfile.Query(AspnetProfileCommands.FindByPropertynames(propertynames));
}

///

/// Finds all AspnetProfile objects with a certain Propertyvaluesstring value.
///

/// The Propertyvaluesstring value (‘*’ can be used as a wildcard). /// All AspnetProfile objects with a certain Propertyvaluesstring value.
public static IList FindByPropertyvaluesstring(string propertyvaluesstring)
{
return AspnetProfile.Query(AspnetProfileCommands.FindByPropertyvaluesstring(propertyvaluesstring));
}

///

/// Finds all AspnetProfile objects with a certain Propertyvaluesbinary value.
///

/// The Propertyvaluesbinary value. /// All AspnetProfile objects with a certain Propertyvaluesbinary value.
public static IList FindByPropertyvaluesbinary(byte[] propertyvaluesbinary)
{
return AspnetProfile.Query(AspnetProfileCommands.FindByPropertyvaluesbinary(propertyvaluesbinary));
}

///

/// Finds all AspnetProfile objects with a certain Lastupdateddate value.
///

/// The Lastupdateddate value. /// All AspnetProfile objects with a certain Lastupdateddate value.
public static IList FindByLastupdateddate(DateTime lastupdateddate)
{
return AspnetProfile.Query(AspnetProfileCommands.FindByLastupdateddate(lastupdateddate));
}

}
}

[/csharp]

Share

Leave a Reply

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