0

I want to improve the architecture of my ASP.NET MVC application. In my controller i use service layer with ViewModel. Example Service Layer:

 public interface ICashRegisterManager
{
    void CreateCashRegister(CashRegisterTransactionModel model, int? programId);
}

Example Controller:

 public class CashRegisterTransactionController : PersonContextController<CashRegisterTransactionModel, CashRegisterTransactionFilter>
{
    public CashRegisterTransactionController(IPersonContextProvider personContextProvider, ICashRegisterManager cashRegisterManager)
        : base(personContextProvider)
    {
        ExceptionUtil.NotNull(cashRegisterManager, "cashRegisterManager");
        this.cashRegisterManager = cashRegisterManager;
    }

    public override ActionResult Create(DataSourceRequest request, CashRegisterTransactionModel contract)
    {
        cashRegisterManager.CreateCashRegister(contract, contract.ProgramId);
        return base.Create(request, contract);
    }

But in Service layer i should to create instance of IRepository and every time map TContract into TEntity.

My idea is in Service layer to use intermediate class , how make this.

Example:

  public class CashRegisterManager : ICashRegisterManager
  {
      public void CreateCashRegister(CashRegisterTransactionModel model, int? programId)
     {
         var persister = Persistence.GetPersister<CashRegisterTransactionModel>();
         persister.Add(model);
      }
   }

  public interface IPersister<TContrct>
  {
    void Add(TContrct model);
  } 

I don't know how to implement the Add method, which to use IRepository. I guess that should comply with the naming conventions of TContract and TEnitity(CashRegisterTransactionModel/CashRegisterTransaction) and how can I return a instance of IPersister. I apologize for my English. Thank you for your time!!!

  • You may find this answer useful - http://stackoverflow.com/questions/28338868/how-to-organize-dal-in-asp-net-mvc/28339287#28339287 – SBirthare Apr 20 '15 at 10:43

2 Answers2

0

Personally I think creating your own generic repositories is a waste of time. however, perhaps you could try something like this :

public interface IPersister<T>
{
    void Add(T model);
}

public class Persister<T> : IPersister<T>
{
    protected List<T> data;

    public Persister()
    {
        this.data = new List<T>();
    }

    public void Add(T model)
    {
        this.data.Add(model);
    }
}
Ewan
  • 1,261
  • 1
  • 14
  • 25
0

This is simple implementation for IPersister :

 public class Persister<TContract, TEntity> : IPersister<TContract, TEntity> where TEntity : class,IDeletableEntity
{

    public Persister(IDeletableEntityRepository<TEntity> repository)
    {
        this.repository = repository;
    }

    public void Add(TContract model)
    {
        var entity = Mapper.Map<TContract, TEntity>(model);
        repository.Add(entity);
        repository.SaveChanges();
    }


    public IQueryable<TContract> All<TContract>()
    {
        return repository.All().Where(x => !x.IsDeleted).Project().To<TContract>();
    }

    #region Private Members
    private IDeletableEntityRepository<TEntity> repository;
    #endregion
}

But I don't want to use TEnitity when creating IPersister, because the mvc project doesn't have a reference to Database Entities.