Friday, January 14, 2005

Mutil Tier Structure .Net Sample


Solution: Projects Name Space Objects File

----------Portal Corp.Portal Login Login.aspx.cs
----------Common Corp.Portal.Common CommonUtil CommonUtil.cs
SqlHelper SqlHelper.cs
----------AdminInfo Corp.Portal.Admin.Info UserInfo UserInfo.cs
UserProfile UserProfile.cs
LoginResults EnumInfo.cs
MenuInfo MeneInfo.cs
RoleInfo RoleInfo.cs
GroupInfo GroupInfo.cs
----------AdminBAL Corp.Portal.Admin.BAL UserBAL UserBAL.cs
----------AdminDAL Corp.Portal.Admin.DAL UserDAL UserDAL.cs


=================================================
Login.aspx.cs Corp.Portal
=================================================
protected void btnLogin_Click(Object sender, EventArgs e)
{
UserInfo oUser = new UserInfo(UserDAL.GetUserByEmail(null,null,UserEmail.Text));
if (oUser.us_login == null) {
//Login failed
}
LoginResults result = UserBAL.LoginUser(username, password, "W");
//LoginResults.LoginOK, LoginClientNotAllowed, LoginInvalidUser ...
//if good
UserProfile oUP = UserBAL.GetUserProfile(oUser.us_login, "W");
//Set Session and more process
}

=================================================
EnumInfo.cs Corp.Portal.Admin.Info
=================================================
public enum LoginResults
{
LoginOK = 0,
LoginFailed,
LoginException,
LoginInvalidUser,
LoginNotAllowed,
LoginClientNotAllowed
}

=================================================
UserInfo.cs Corp.Portal.Admin.Info
=================================================
public class UserInfo
{
//Properties
public string us_login;
public string us_online;
...
//A valid DataSet containing a User record in a DataTable
public UserInfo(DataSet ds)
{
LoadUserInfo(ds.Tables[0].Rows[0]);
}
//A valid DataRow representing a User record
public UserInfo(DataRow dr)
{
LoadUserInfo(dr);
}
private void LoadUserInfo(DataRow dr)
{
this.us_login = dr["us_login"].ToString();
...
}
public bool IsOnline();

}


=================================================
UserProfile.cs Corp.Portal.Admin.Info
=================================================
public class UserProfile
{
//Properties
public UserInfo User;
public Hashtable Roles;
public Hashtable Groups;
public Hashtable Applications;
public Stack History;

public UserProfile()
{
this.User = new UserInfo();
this.Roles = new Hashtable();
this.History = new Stack(5);
}
public bool IsInRole(string sRoleCode);
public bool IsInGroup(string sGrpCode);
...
}

=================================================
UserBal.cs Corp.Portal.Admin.BAL
=================================================
public static LoginResults LoginUser(string sLogin, string sPwd, string sClientType)
{
UserInfo oUser = UserDAL.GetUserInfo(null,null,sLogin);
//if good, update user last login
UserDAL.UpdateUserLastLogin(null,sLogin);
//validate the supplied password
string pwdHash = CommonUtil.CreatePasswordHash(sPws, oUser.us_pwdsalt);
if (!pwdHash.Equals(oUser.us_password)) return LoginResults.LoginFailed;
}

public static UserProfile GetUserProfile(string sLogin, string sClientType)
{
SqlConnection cn = null;
DataSet dsGroup = null;
DataSet dsMenu = null;
GroupInfo oGroup = null;
MenuInfo oMenu = null;
UserProfile oProfile = new UserProfile();
cn = new SqlConnection(SqlHelper.GetConnectionString(null));
cn.Open();
oProfile.User = UserDAL.GetUserInfo(null, cn, sLogin);
dsGroup = GroupDAL.GetGroupListByUser(null, cn, sLogin);
//for each row in dsGroup
oGroup = new GroupInfo(dsGroup.Tables[0].Rows[i]);
oProfile.Groups.Add(oGroup.gr_code, oGroup);
}

=================================================
UserDal.cs Corp.Portal.Admin.DAL
=================================================
public static DataSet GetUserByEmail(SqlTransaction tr, SqlConnection cn, string sEmail)
{
bool mustDisposeConnection = false;
SqlCommand cmd = null;
StringBuilder sb = new StringBuilder();
sb.Append("SELECT * from Users ");
sb.Append("WHERE us_email = @us_email");
cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sb.ToString();
cmd.Parameters.Add("@us_email", SqlDbType.VarChar, 128).Value = sEmail;
if (tr == null)
{
if (cn == null)
{
try
{
cn = SqlHelper.GetConnection(SqlHelper.GetConnectionString(null));
mustDisposeConnection = true;
}
catch (Exception ex)
{
...
}
}
try
{
return SqlHelper.ExecuteDataset(cn, cmd);
}
catch
{
...
}
finally
{
if (mustDisposeConnection)
{
if (cn.State == ConnectionState.Open)
cn.Close();
cn.Dispose();
}
}
}
else
{
try
{
return SqlHelper.ExecuteDataset(tr, cmd);
}
catch (Exception ex)
{
...
}
}
}

No comments: