0

I am trying to learn singleton pattern but had some issues with getting datas from a table and populate my gridview. Btw I am using entity framework.

Here is my gridview:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

My PersonelTable Class:

public class PersonelTable
{
    public PersonelTable(string isim,string soyad)
    {
        Isim = isim;
        Soyad = soyad;
    }
    public int Id { get; private set; }
    public string Isim { get; set; }
    public string Soyad { get; set; }
}

My userbusiness class:

public sealed class UserBusiness
{
    private JuqueryDbEntities entity = new JuqueryDbEntities();
    private static volatile UserBusiness instance;
    private static readonly object syncRoot = new Object();

    private UserBusiness() { }

    public static UserBusiness Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new UserBusiness();
                }
            }
            return instance;
        }
    }

    public List<PersonelTable> GetAllUsers() 
    {
        List<PersonelTable> personelList = new List<PersonelTable>();

        var users = (from u in entity.PersonelTable select u);
        foreach (var user in users)
        {
            personelList.Add(new PersonelTable(user.Isim,user.Soyad));
        }
        return personelList;
    }
}

And lastly codebehind of my webform:

protected void PopulateGridView() 
    {
        List<PersonelTable> personelList = UserBusiness.Instance.GetAllUsers();
        GridView1.DataSource = personelList;
        GridView1.DataBind();
    }

I get an error like 'ProjectName.PersonelTable hasn't got creator without parameter' in the foreach of my 'GetAllUsers' method. Why is this happening and how can I fix it ?

  • 1
    "I am trying to learn singleton pattern". Please just don't. [Singleton is an anti-pattern](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons?rq=1). That's like saying "I'm learning to hit my head against a wall optimizing the blood loss". The valid answer would be "just stop that". – nvoigt Apr 22 '16 at 08:09
  • I red so many articles about singleton and really get confused since everyone say different things. Ok, then what type of pattern can I use to avoid interfere of user objects each other ? Caues I had really hard time about it. One user make something and other user gets affected when they use same function in same time. @nvoigt – Batuhan Ozdal Apr 22 '16 at 08:18
  • You need some kind of syncronization. As we don't know what your problem is, it's hard to help. You may want to ask a question about your actual problem. – nvoigt Apr 22 '16 at 08:20
  • Singletons are not so bad. I'm usually using these structures to keep huge dictionaries and it works perfect. It's better to keep 150 000 definitions selected from database in some object, than asking DB 100 000 per minute for few rows. – Maciej S. Apr 22 '16 at 08:29

1 Answers1

0

I think you are passing parameters in Person table which is really don't need. Could you please remove that parameter and try again ?