我在这里走在正确的轨道上吗?编码风格 [英] am I on the right track here? coding style

查看:54
本文介绍了我在这里走在正确的轨道上吗?编码风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写我的第一个实体框架应用程序,我需要知道我是否正确地接近这个。 下面的代码示例是我将很快与WCF一起讨论的业务对象。 业务对象服务器将是无状态和负载平衡的(因此绝对
业务对象中没有状态保存)。 对象将转到客户端,可以是任何东西(移动,Windows,Linux等),所以我不能假设EF将在另一边。 我想知道的是,我是否要正确添加/更新/删除这些
对象? 我有没有看到不同的方式?

Writing my first entity framework app and I need to know if I am approaching this the right way.  The code sample below is a business object that I will front with WCF soon.  The business object servers will be stateless and load balanced (so absolutely no state saving in the business objects).  The objects will go to clients that can be really anything (mobile, windows, linux etc) so I can't assume EF will be on the other side.  What I want to know is if I am going about add/update/deleting these objects correctly?  Is there a different way that I'm just not seeing?


	public class Security
	{
		#region Security

		public static bool Authenticate(string username, string password)
		{
			using (CF2Entities context = new CF2Entities())
			{

				var users = from cf in context.CFUsers where cf.UserName.Equals(username) select cf;

				if ((users.Count() > 0) && (users.First().Password == password))
					return true; // success
				else
					return false;	// failed
			}
		}

		#endregion


		#region Roles


		/// <summary>
		/// Get a role by ID
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public static SecurityRole GetRoleByID(int id)
		{
			SecurityRole sr = null;

			using (CF2Entities context = new CF2Entities())
			{
				var roles = from secroles in context.SecurityRoles where secroles.ID.Equals(id) select secroles;
				if (roles.Count() > 0)
					return roles.First();
			}

			return sr;
		}

		/// <summary>
		/// Insert Role
		/// </summary>
		/// <param name="role"></param>
		/// <returns>Auto Identity ID</returns>
		public static int InsertRole(SecurityRole role)
		{
			using (CF2Entities context = new CF2Entities())
			{
				context.AddToSecurityRoles(role);
				context.SaveChanges();
			}

			return role.ID;
		}

		/// <summary>
		/// Update Role
		/// </summary>
		/// <param name="role"></param>
		public static void UpdateRole(SecurityRole role)
		{
			using (CF2Entities context = new CF2Entities())
			{
				context.Attach(role);
				context.ObjectStateManager.ChangeObjectState(role, System.Data.EntityState.Modified);
				context.ApplyCurrentValues("SecurityRoles", role);
				context.SaveChanges();
			}
		}

		/// <summary>
		/// Delete Role
		/// </summary>
		/// <param name="role"></param>
		public static void DeleteRole(SecurityRole role)
		{
			using (CF2Entities context = new CF2Entities())
			{
				context.Attach(role);
				context.DeleteObject(role);
				context.SaveChanges();
			}			
		}


		#endregion


	}

推荐答案

您的代码似乎效率不高。使用Count()和First()方法时,查询将执行两次。实际上,您可以将其更改为


$
var roles =(来自secroles in context.SecurityRoles where secroles.ID.Equals(id)select secroles).FirstOrDefault();

if(roles!= null )

{

    返回角色;

}


这篇关于我在这里走在正确的轨道上吗?编码风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆