Created
January 31, 2014 14:53
-
-
Save ucin/8733509 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
[Alias("Groups")] | |
public class Group : IDataMap, IHasId<short>, ISerializable { | |
public Group() { | |
} | |
public Group( SerializationInfo info, StreamingContext context ) { | |
Id = info.GetInt16("Id"); | |
Name = info.GetString("Name"); | |
} | |
#region ISerializable Members | |
public void GetObjectData(SerializationInfo info, StreamingContext context) { | |
info.AddValue("Id", Id); | |
info.AddValue("Name", Name); | |
} | |
#endregion | |
[Index(Unique = true)] | |
[Alias("GroupName")] | |
[StringLength(50)] | |
[Required] | |
public string Name { get; set; } | |
[AutoIncrement] | |
[PrimaryKey] | |
[Alias("GroupId")] | |
public short Id { get; set; } | |
public override int GetHashCode() { | |
unchecked { | |
int hash = 0x65407627; | |
hash = (hash*-1521134295) + Name.SafeGetHashCode(); | |
return hash; | |
} | |
} | |
public override bool Equals(object obj) { | |
if (obj == null) { | |
return false; | |
} | |
var group = obj as Group; | |
if (group == null) { | |
return false; | |
} | |
if (group.Id == Id && !string.IsNullOrWhiteSpace(group.Name) && !string.IsNullOrWhiteSpace(Name) | |
&& Name.Equals(group.Name, StringComparison.InvariantCultureIgnoreCase)) { | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDataMap { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class InitiateDb { | |
readonly IDbConnectionFactory dbFactory; | |
readonly bool dropTableIfExist = false; | |
IList<Type> tables = new List<Type>( ); | |
readonly Assembly asm = typeof( IDataMap ).Assembly; | |
IList<Type> types = new List<Type>(); | |
public InitiateDb( IDbConnectionFactory dbFactory, bool dropTableIfExist ) { | |
this.dbFactory = dbFactory; | |
this.dropTableIfExist = dropTableIfExist; | |
foreach ( Type type in asm.GetTypes( ) ) { | |
// IDataMap is empty interface to identify your data map object. | |
if ( type.GetInterfaces( ).Contains( typeof( IDataMap ) ) ) { | |
types.Add( type ); | |
} | |
} | |
for ( var i = 0; i < types.Count; i++ ) { | |
var model = asm.GetType( types[ i ].ToString( ) ); | |
if ( !tables.Contains( model ) && !IsModelBeingReferenced( model ) ) { | |
tables.Add( model ); | |
} | |
} | |
for ( var i = 0; i < types.Count; i++ ) { | |
var model = asm.GetType( types[ i ].ToString( ) ); | |
if ( !tables.Contains( model ) && IsModelPropertiesHadReferencesAttribute( model ) ) { | |
tables.Add( model ); | |
} | |
} | |
for ( var i = 0; i < types.Count; i++ ) { | |
var model = asm.GetType( types[ i ].ToString( ) ); | |
if ( !tables.Contains( model ) && !IsModelPropertiesHadReferencesAttribute( model ) ) { | |
tables.Add( model ); | |
} | |
} | |
} | |
public void DropTables( ) { | |
for ( var i = 0; i < types.Count; i++ ) { | |
dbFactory.Run( db => db.DropTable( tables[ i ] ) ); | |
} | |
} | |
public void BuildTables( ) { | |
for ( var i = 1; i < types.Count + 1; i++ ) { | |
dbFactory.Run( db => db.CreateTable( dropTableIfExist, tables[ types.Count - i ] ) ); | |
} | |
} | |
private bool IsModelBeingReferenced( Type model ) { | |
foreach ( var item in types ) { | |
var classItem = asm.GetType( item.ToString( ) ); | |
if ( model != classItem ) { | |
foreach ( var property in classItem.GetProperties( ) ) { | |
var referencesAttribute = ( ReferencesAttribute[ ] )property.GetCustomAttributes( typeof( ReferencesAttribute ) ); | |
if ( referencesAttribute.Length > 0 ) { | |
if ( referencesAttribute[ 0 ].Type.Name.Equals( model.Name, StringComparison.InvariantCultureIgnoreCase ) ) { | |
return true; | |
} | |
} | |
} | |
} | |
} | |
return false; | |
} | |
private bool IsModelPropertiesHadReferencesAttribute( Type model ) { | |
foreach ( var item in types ) { | |
var classItem = asm.GetType( item.ToString( ) ); | |
if ( model != classItem && !tables.Contains( classItem ) ) { | |
foreach ( var property in model.GetProperties( ) ) { | |
var referencesAttribute = ( ReferencesAttribute[ ] )property.GetCustomAttributes( typeof( ReferencesAttribute ) ); | |
if ( referencesAttribute.Length > 0 ) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
[Alias("Users")] | |
public class User : IDataMap, IHasId<int>, ISerializable { | |
[AutoIncrement] | |
[Alias("UserId")] | |
[PrimaryKey] | |
public int Id { get; set; } | |
[Index(Unique = true)] | |
[Required] | |
[StringLength(100)] | |
[DataType(DataType.EmailAddress)] | |
public string Email { get; set; } | |
[Required] | |
[StringLength(50)] | |
[DataType(DataType.Text)] | |
public string FullName { get; set; } | |
[Required] | |
[Default(typeof(bool), "0")] | |
public bool IsEnabled { get; set; } | |
[Required] | |
[StringLength(1024)] | |
[DataType(DataType.Password)] | |
public string Hash { get; set; } | |
[Required] | |
[StringLength(1024)] | |
[DataType(DataType.Password)] | |
public string Salt { get; set; } | |
public User() { | |
} | |
public User(SerializationInfo info, StreamingContext context) { | |
Id = info.GetInt32("Id"); | |
Email = info.GetString("Email"); | |
FullName = info.GetString("FullName"); | |
IsEnabled = info.GetBoolean("IsEnabled"); | |
Hash = info.GetString("Hash"); | |
Salt = info.GetString("Salt"); | |
} | |
public void GetObjectData(SerializationInfo info, StreamingContext context) { | |
throw new NotImplementedException(); | |
} | |
public override int GetHashCode() { | |
unchecked { | |
int hash = 0x65407627; | |
hash = (hash * -1521134295) + Id.GetHashCode(); | |
hash = (hash * -1521134295) + Email.SafeGetHashCode(); | |
return hash; | |
} | |
} | |
public override bool Equals(object obj) { | |
if (obj == null) { | |
return false; | |
} | |
var user = obj as User; | |
if ((Object)user == null) { | |
return false; | |
} | |
if (user.Id == Id && | |
!string.IsNullOrWhiteSpace(user.Email) && | |
!string.IsNullOrWhiteSpace(Email) && | |
Email.Equals(user.Email, StringComparison.InvariantCultureIgnoreCase)) { | |
return true; | |
} | |
return false; | |
} | |
public override string ToString() { | |
return string.Format("{Id:{0},Email:{1},FullName:{2},IsEnabled:{3}}", Id, Email, FullName, IsEnabled); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Serializable] | |
[Alias( "UserGroups" )] | |
public class UserGroups : IDataMap, IHasId<int>, ISerializable { | |
[AutoIncrement] | |
[PrimaryKey] | |
[Alias( "UserGroup" )] | |
public int Id { get; set; } | |
[References( typeof( Group ) )] | |
public short GroupId { get; set; } | |
[References( typeof( User ) )] | |
public int UserId { get; set; } | |
public UserGroups( ) { | |
} | |
public UserGroups( SerializationInfo info, StreamingContext context ) { | |
Id = info.GetInt32( "Id" ); | |
GroupId = info.GetInt16( "GroupId" ); | |
UserId = info.GetInt32( "UserId" ); | |
} | |
#region ISerializable Members | |
public void GetObjectData( SerializationInfo info, StreamingContext context ) { | |
info.AddValue( "Id", Id ); | |
info.AddValue( "GroupId", GroupId ); | |
info.AddValue( "UserId", UserId ); | |
} | |
#endregion | |
public override int GetHashCode( ) { | |
unchecked { | |
int hash = 0x65407627; | |
hash = ( hash * -1521134295 ) + Id.GetHashCode( ); | |
hash = ( hash * -1521134295 ) + GroupId.GetHashCode( ); | |
hash = ( hash * -1521134295 ) + UserId.GetHashCode( ); | |
return hash; | |
} | |
} | |
public override bool Equals( object obj ) { | |
if ( obj == null ) { | |
return false; | |
} | |
var userGroups = obj as UserGroups; | |
if ( userGroups == null ) { | |
return false; | |
} | |
if ( userGroups.Id == Id && | |
userGroups.GroupId == GroupId && | |
userGroups.UserId == UserId) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment