• 5 Subscribers
  • 114 Views
2

I'm looking for an approach to auto-generate a record everytime a class I have is persisted to the data base, without triggers or database functions.

My classe Item is something like that

/// <summary>
/// Modelo de representação do Item (Produto) 
/// </summary>
[Table("Item")]
public class Item : History
{
    /// <summary>
    /// Id único gerador pelo sistema
    /// </summary>
    [Key]
    public long Id { get; set; }

    /// <summary>
    /// Nome do produto
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Código de barras
    /// </summary>
    public string BarCode { get; set; }

    /// <summary>
    /// Código da imagem do produto na base de dados
    /// </summary>
    public long Image_Id { get; set; }
}

My repository class of Item has an insert to persist it. Like that

 /// <summary>
/// Repositório do objeto Item
/// </summary>
public class ItemRepository : BaseRepository<Item>
{
    #region Constructors
    /// <summary>
    /// Inicializa o repositório de Item 
    /// </summary>
    /// <param name="context">Contexto usado em sua inicialização</param>
    public ItemRepository(DbContext context) : base(context)
    {

    }
    #endregion

    #region Methods

    public override void Insert(Item entity)
    {
        base.Insert(entity);
    }

    #endregion
}

My question is: How can I generate a record to my objetc Item__History which represents a mirror for my item object in this momento? Everytime I update this objetct Item, I need the method of my repository (update) to generate another insert into Item__History, creating another object that reflects this exact moment of Item.

Later, I can go Item__History and check all the states this class Item already have.

I can do It by creating a model for Item__History (ok, it's created) and creating a repository class to Item__History, and during the insert/update of Item inside the repository class, I create an instance of Item__History, copy all the values into this new instance and call the Item__Repository to insert it to. I'm not sure if it's the best approach, since I'll have a lot of classes which I want to manager the history, and don't want to have a lot of "XXX___History" classes and repositories.

Sorry, btw if the question isn't good enough. I hope someone understand it.


1 answers in total

3
M. Wiśnicki Posted at 2016-12-14 17:38:47Z

What about if You create GenericHistoryRepository class with IGenericHistoryRepository interface and implement methodsAdd(),AddList(),Update() and Remove(). Such a generic class you will be able to take advantage of its various entities. You can follow this code.

namespace DataAccessLayer.DataAccessObject
{
    public interface IGenericHistoryRepository<T> where T : class
    {

        void Add(params T[] items);
        void AddList(params T[] items);
        void Update(params T[] items);
        void Remove(params T[] items);

    }
    public class GenericHistoryRepository<T> : IGenericHistoryRepository<T> where T : class
    {

        public virtual void Add(params T[] items)
        {
            using (var context = new MainContext())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = EntityState.Added;
                }
                context.SaveChanges();
            }
        }
        public void AddList(params T[] items)
        {
            using (var context = new MainContext())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = EntityState.Added;
                }
                context.SaveChanges();
            }
        }
        public virtual void Update(params T[] items)
        {
            using (var context = new MainContext())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = EntityState.Modified;
                }

                context.SaveChanges();

            }
        }
        public virtual void Remove(params T[] items)
        {
            using (var context = new MainContext())
            {
                foreach (T item in items)
                {
                    context.Entry(item).State = EntityState.Deleted;
                }
                context.SaveChanges();
            }
        }

    }

 }

Usage

   public override void Insert(Item entity)
    {
        base.Insert(entity);
        new GenericHistoryRepository<Item_History>().Add(entity);
    }
    ...

    new GenericHistoryRepository<XXX_History>().Add(entity);

Answer this questsion