My favorites | Sign in
Project Home Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Project Information
Members

It´s a class mapper that uses C# attributes features to map each property to it´s own column in database table.

If the table isn´t created yet, the Mapper class creates the tables respecting it´s primary key / foreign key settings as well 1 -> 1, 1 -> N, N -> N relationships.

It´s a bit stable now, so i´ve decided to release it to the community. Suggestions are appreciated!

Simple Usage (no relations):

// Address Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AttributesFrameworkExample.Classes
{
    [AttributesFramework.Attributes.DBTable(Name = "Address")]
    public class Address : SuperClass
    {
        [AttributesFramework.Attributes.DBColumn(Name = "AddressId", DataType = System.Data.SqlDbType.Int, isAutoIncrement = true, isPrimaryKey = true, isReadOnly = true)]
        public int AddressId { get; set; }

        [AttributesFramework.Attributes.DBColumn(Name = "Street", DataType = System.Data.SqlDbType.VarChar, Size = 256, AllowNull = false)]
        public string Street { get; set; }

        [AttributesFramework.Attributes.DBColumn(Name = "Number", DataType = System.Data.SqlDbType.VarChar, Size = 128, AllowNull = false)]
        public string Number { get; set; }
    }
}

// Person Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AttributesFrameworkExample.Classes
{
    [AttributesFramework.Attributes.DBTable(Name = "Person")]
    public class Person : SuperClass
    {
        [AttributesFramework.Attributes.DBColumn(Name = "PersonId", DataType = System.Data.SqlDbType.Int, isAutoIncrement = true, isPrimaryKey = true, isReadOnly = true)]
        public int PersonId { get; set; }

        [AttributesFramework.Attributes.DBColumn(Name = "Name", DataType = System.Data.SqlDbType.VarChar, Size = 256, AllowNull = false)]
        public string Name { get; set; }

        [AttributesFramework.Attributes.DBColumn(Name = "BirthDate", DataType = System.Data.SqlDbType.DateTime, AllowNull = false)]
        public DateTime BirthDate { get; set; }

        [AttributesFramework.Attributes.DBColumn(Name = "Active", DataType = System.Data.SqlDbType.Bit, AllowNull = false)]
        public bool Active { get; set; }

        [AttributesFramework.Attributes.DBRelation(Name = "Address", ClassType = typeof(Address), RelationType = AttributesFramework.Attributes.DBRelationType.ONE_ONE)]
        public Address Address { get; set; }

        private List<Person> _Childs;
        [AttributesFramework.Attributes.DBRelation(Name = "Childs", ClassType = typeof(Person), RelationType = AttributesFramework.Attributes.DBRelationType.ONE_N)]
        public List<Person> Childs { get { if (this._Childs == null) { Childs = new List<Person>(); } return this._Childs; } set { this._Childs = value; } }
    }
}

// Super Class (The Most Important)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AttributesFrameworkExample.Classes
{
    public abstract class SuperClass : MarshalByRefObject
    {
        public void Insert()
        {
            AttributesFramework.Mapper.Data.Insert(this);
        }

        public void Update()
        {
            AttributesFramework.Mapper.Data.Update(this);
        }

        public void Delete()
        {
            AttributesFramework.Mapper.Data.Delete(this);
        }

        public SuperClass RetrieveById(object p_idSuper)
        {
            Dictionary<AttributesFramework.Attributes.DBColumnAttribute, object> _primaryKeys = new Dictionary<AttributesFramework.Attributes.DBColumnAttribute, object>();
            _primaryKeys.Add(AttributesFramework.Mapper.Attributes.GetDBColumnsPrimaryKeys(this.GetType())[0], p_idSuper);

            return (SuperClass)AttributesFramework.Mapper.Data.RetrieveByPrimaryKeys(_primaryKeys, this.GetType());
        }

        public ArrayList RetrieveAll()
        {
            return AttributesFramework.Mapper.Data.RetrieveAll(this.GetType());
        }

        public ArrayList RetrieveByQuery(string p_Query)
        {
            return AttributesFramework.Mapper.Data.RetrieveByQuery(p_Query, this.GetType());
        }
    }
}

// If my Classes aren´t mapped yet, i´ll do it then.
AttributesFramework.Mapper.Data.Map(typeof(Classes.Address));
AttributesFramework.Mapper.Data.Map(typeof(Classes.Person));   
Powered by Google Project Hosting