读书人

Entity这个种是要自己写的还是自动生辰

发布时间: 2013-09-12 22:07:00 作者: rapoo

Entity这个类是要自己写的还是自动生辰的

//===================================================================================
// Microsoft Developer & Platform Evangelism
//===================================================================================
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//===================================================================================
// Copyright (c) Microsoft Corporation. All Rights Reserved.
// This code is released under the terms of the MS-LPL license,
// http://microsoftnlayerapp.codeplex.com/license
//===================================================================================

namespace xx.xx.xx.xx
{
using System;


/// <summary>
/// Base class for entities
/// </summary>
public abstract partial class Entity
{
#region Members

int? _requestedHashCode;
Guid _Id;

#endregion

#region Properties

/// <summary>
/// Get the persisten object identifier
/// </summary>
public virtual Guid Id
{
get
{
return _Id;
}
protected set
{
_Id = value;


}
}

#endregion

#region Public Methods

/// <summary>
/// Check if this entity is transient, ie, without identity at this moment
/// </summary>
/// <returns>True if entity is transient, else false</returns>
public bool IsTransient()
{
return this.Id == Guid.Empty;
}

/// <summary>
/// Generate identity for this entity
/// </summary>
public void GenerateNewIdentity()
{
if ( IsTransient())
this.Id = IdentityGenerator.NewSequentialGuid();
}

/// <summary>
/// Change current identity for a new non transient identity
/// </summary>
/// <param name="identity">the new identity</param>
public void ChangeCurrentIdentity(Guid identity)
{
if ( identity != Guid.Empty)
this.Id = identity;
}

#endregion

#region Overrides Methods

/// <summary>


/// <see cref="M:System.Object.Equals"/>
/// </summary>
/// <param name="obj"><see cref="M:System.Object.Equals"/></param>
/// <returns><see cref="M:System.Object.Equals"/></returns>
public override bool Equals(object obj)
{
if (obj == null || !(obj is Entity))
return false;

if (Object.ReferenceEquals(this, obj))
return true;

Entity item = (Entity)obj;

if (item.IsTransient() || this.IsTransient())
return false;
else
return item.Id == this.Id;
}

/// <summary>
/// <see cref="M:System.Object.GetHashCode"/>
/// </summary>
/// <returns><see cref="M:System.Object.GetHashCode"/></returns>
public override int GetHashCode()
{
if (!IsTransient())
{
if (!_requestedHashCode.HasValue)
_requestedHashCode = this.Id.GetHashCode() ^ 31; // XOR for random distribution (http://blogs.msdn.com/b/ericlippert/archive/2011/02/28/guidelines-and-rules-for-gethashcode.aspx)



return _requestedHashCode.Value;
}
else
return base.GetHashCode();

}

public static bool operator ==(Entity left, Entity right)
{
if (Object.Equals(left, null))
return (Object.Equals(right, null)) ? true : false;
else
return left.Equals(right);
}

public static bool operator !=(Entity left, Entity right)
{
return !(left == right);
}

#endregion
}
}



我看到某个工程下面的这个类,请问这个类是自动生成的还是自己写的。。。。。。请详细说下。。谢谢
[解决办法]
看着应该不是自动生成的。

VS自动生成的,一般文件头部会有类似这样的注释

//------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------


[解决办法]
看着像第三方工具生成的
[解决办法]
自动生成的Entity这个种是要自己写的还是自动生辰的

读书人网 >.NET

热点推荐