Tech Lover
Tech Lover

Reputation: 111

How to get data using Repository pattern in the code-behind?

I am a new ASP.NET WebForms developer and I am struggling right now with retrieving the data in the code-behind of .aspx page using the Repository Pattern. For instance, let us assume that I would like to get the value of the system configurations in the code-behind of About Us page in my test application. How can I do that?

I already have all repositories classes and interfaces in the Data Access Layer.

Here's the code of SysConfigRepository class:

public class SysConfigRepository : IDisposable, ISysConfigRepository
    {
        //internal variable
        private readonly TestEntities dbContext = new DbdKurdoaEntities();

        public IEnumerable<T_SysConfig> GetSysConfigs()
        {
            return dbContext.T_SysConfig.ToList();
        }

        public T_SysConfig GetSysConfig(int id)
        {
            T_SysConfig sysConfigObj = GetSysConfigs().SingleOrDefault(s => s.Id == id);
            return sysConfigObj;
        }

        private bool disposedValue = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposedValue)
            {
                if (disposing)
                {
                    dbContext.Dispose();
                }
            }
            this.disposedValue = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }

Code of ISysConfigRepository :

public interface ISysConfigRepository : IDisposable
    {
        IEnumerable<T_SysConfig> GetSysConfigs();
        T_SysConfig GetSysConfig(int sysConfigId);
    }

Before I used to have one class in the DAL for each entity and then I can call an object of this class in the code-behind of any .aspx page by writing the following:

SysConfig configObj = new SysConfig();
var result = configObj.GetSysConfigs(configObj);

So how can I call an object of repository now to get the data in the code-behind of any .aspx page?

Upvotes: 0

Views: 781

Answers (2)

hamid_reza hobab
hamid_reza hobab

Reputation: 929

your SysConfigRepository does not use Constructor! so you can not get new Object of class. by put a public SysConfigRepository(){} code bellow work: var configs = sysConfigRepository.GetSysConfigs ();

Upvotes: 0

user5684647
user5684647

Reputation:

You probably need to provide some more detail here. Have you tried using the class? Is there an error?

using (var sysConfigRepository = new SysConfigRepository())
{
    var configs = sysConfigRepository.GetSysConfigs ();
    // Do some stuff here
}

Upvotes: 1

Related Questions