AspnetRoles Partial Class

This is the base partial data object class for the “roles” 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 AspnetRoles class.
///

public partial class AspnetRoles : IPersistable
{
#region Members

private bool isNew;
private bool _isNew;
private Guid applicationid;
private Guid roleid;
private string rolename;
private string loweredrolename;
private string description;

#endregion

#region Properties

///

/// The Applicationid.
///

public virtual Guid Applicationid
{
get { return this.applicationid; }
set { this.applicationid = value; }
}

///

/// The Roleid.
///

public virtual Guid Roleid
{
get { return this.roleid; }
set { this.roleid = value; }
}

///

/// The Rolename.
///

[StringInfo(256, true)]
public virtual string Rolename
{
get { return this.rolename; }
set { this.rolename = value; }
}

///

/// The Loweredrolename.
///

[StringInfo(256, true)]
public virtual string Loweredrolename
{
get { return this.loweredrolename; }
set { this.loweredrolename = value; }
}

///

/// The Description.
///

[StringInfo(256, false)]
public virtual string Description
{
get { return this.description; }
set { this.description = value; }
}

#endregion

#region ColumnNames

///

/// The corresponding schema name.
///

internal const string SchemaName = “dbo”;

///

/// The corresponding table name.
///

internal const string TableName = “aspnet_Roles”;

///

/// The column names.
///

internal class ColumnNames
{
///

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

public const string Applicationid=”ApplicationId”;
///

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

public const string Roleid=”RoleId”;
///

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

public const string Rolename=”RoleName”;
///

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

public const string Loweredrolename=”LoweredRoleName”;
///

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

public const string Description=”Description”;
}

#endregion

///

/// The default constructor.
///

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

///

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

/// The Applicationid. /// The Roleid. /// The Rolename. /// The Loweredrolename. public AspnetRoles(Guid applicationid, Guid roleid, string rolename, string loweredrolename)
{
this.applicationid = applicationid;
this.roleid = roleid;
this.rolename = rolename;
this.loweredrolename = loweredrolename;

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

///

/// The constructor from IDataReader.
///

/// An initalized IDataReader. internal AspnetRoles(IDataReader reader)
{
if ((reader[“ApplicationId”] != null) && (reader[“ApplicationId”] != DBNull.Value))
this.applicationid = (Guid)reader[“ApplicationId”];
if ((reader[“RoleId”] != null) && (reader[“RoleId”] != DBNull.Value))
this.roleid = (Guid)reader[“RoleId”];
if ((reader[“RoleName”] != null) && (reader[“RoleName”] != DBNull.Value))
this.rolename = (string)reader[“RoleName”];
if ((reader[“LoweredRoleName”] != null) && (reader[“LoweredRoleName”] != DBNull.Value))
this.loweredrolename = (string)reader[“LoweredRoleName”];
if ((reader[“Description”] != null) && (reader[“Description”] != DBNull.Value))
this.description = (string)reader[“Description”];

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_Roles] ([ApplicationId], [RoleId], [RoleName], [LoweredRoleName], [Description]) values (@applicationid, @roleid, @rolename, @loweredrolename, @description)”;
cmd.Parameters.AddWithValue(“@applicationid”, this.applicationid);
cmd.Parameters.AddWithValue(“@roleid”, this.roleid);
cmd.Parameters.AddWithValue(“@rolename”, this.rolename);
cmd.Parameters.AddWithValue(“@loweredrolename”, this.loweredrolename);
if (String.IsNullOrEmpty(this.description))
cmd.Parameters.AddWithValue(“@description”, DBNull.Value);
else
cmd.Parameters.AddWithValue(“@description”, this.description);

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_Roles] set [ApplicationId]=@applicationid, [RoleName]=@rolename, [LoweredRoleName]=@loweredrolename, [Description]=@description where ([RoleId]=@roleid)”;
cmd.Parameters.AddWithValue(“@applicationid”, this.applicationid);
cmd.Parameters.AddWithValue(“@rolename”, this.rolename);
cmd.Parameters.AddWithValue(“@loweredrolename”, this.loweredrolename);
if (String.IsNullOrEmpty(this.description))
cmd.Parameters.AddWithValue(“@description”, DBNull.Value);
else
cmd.Parameters.AddWithValue(“@description”, this.description);
cmd.Parameters.AddWithValue(“@roleid”, this.roleid);

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_Roles] where ([RoleId]=@roleid)”;
cmd.Parameters.AddWithValue(“@roleid”, this.roleid);

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 AspnetRoles object by its primary key (Throws System.DataException).
///

/// The Roleid. /// The AspnetRoles object.
public static AspnetRoles Get(Guid roleid)
{
AspnetRoles aspnetroles;
if (AspnetRoles.TryGet(roleid, out aspnetroles))
return aspnetroles;
else
throw new DataException(“‘AspnetRoles’ object not found.”);
}

///

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

/// The Roleid. /// The found AspnetRoles or null if the primary key value does not exist. /// True if the AspnetRoles exists, else false.
public static bool TryGet(Guid roleid, out AspnetRoles aspnetroles)
{
SqlCommand cmd = new SqlCommand(“select * from [dbo].[aspnet_Roles] where ([RoleId]=@roleid)”);
cmd.Parameters.AddWithValue(“@roleid”, roleid);

IList list = AspnetRoles.Query(cmd);

if (list.Count == 0)
{
aspnetroles = null;
return false;
}
else
{
aspnetroles = 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()
{
AspnetRoles aspnetroles;

if (AspnetRoles.TryGet(this.roleid, out aspnetroles))
{
this.isNew = false;
}
else
{
this.isNew = true;
}
}

///

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

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

dto.Applicationid = this.Applicationid;
dto.Roleid = this.Roleid;
dto.Rolename = this.Rolename;
dto.Loweredrolename = this.Loweredrolename;
dto.Description = this.Description;

return dto;
}

///

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

/// The Data Transfer Object. public virtual void SetDTO(AspnetRolesDTO dto)
{
this.Applicationid = dto.Applicationid;
this.Roleid = dto.Roleid;
this.Rolename = dto.Rolename;
this.Loweredrolename = dto.Loweredrolename;
this.Description = dto.Description;
}

///

/// Perfoms a query on AspnetRoles objects.
///

/// An IDbCommand containing the select statement. /// A result list of AspnetRoles 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 AspnetRoles(reader));
}

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

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

///

/// Finds all AspnetRoles objects with a certain Applicationid value.
///

/// The Applicationid value. /// All AspnetRoles objects with a certain Applicationid value.
public static IList FindByApplicationid(Guid applicationid)
{
return AspnetRoles.Query(AspnetRolesCommands.FindByApplicationid(applicationid));
}

///

/// Finds all AspnetRoles objects with a certain Rolename value.
///

/// The Rolename value (‘*’ can be used as a wildcard). /// All AspnetRoles objects with a certain Rolename value.
public static IList FindByRolename(string rolename)
{
return AspnetRoles.Query(AspnetRolesCommands.FindByRolename(rolename));
}

///

/// Finds all AspnetRoles objects with a certain Loweredrolename value.
///

/// The Loweredrolename value (‘*’ can be used as a wildcard). /// All AspnetRoles objects with a certain Loweredrolename value.
public static IList FindByLoweredrolename(string loweredrolename)
{
return AspnetRoles.Query(AspnetRolesCommands.FindByLoweredrolename(loweredrolename));
}

///

/// Finds all AspnetRoles objects with a certain Description value.
///

/// The Description value (‘*’ can be used as a wildcard). /// All AspnetRoles objects with a certain Description value.
public static IList FindByDescription(string description)
{
return AspnetRoles.Query(AspnetRolesCommands.FindByDescription(description));
}

}
}

[/csharp]

Share

Leave a Reply

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