Configure EF Application Structure

I am working on a prototype EF application using POCOs. Basically, as an introduction to the structure, I am wondering how to create an application in a good structure. Later I plan to include WCF in it.

I have done the following:

1) I created an edmx file, but with the Code Generation Property set to None, and generated my database schema,

2) I created POCOs that look like this:

public class Person
{
    public Person()
    { 
    }

    public Person(string firstName, string lastName)
    {        

        FirstName = firstName;
        LastName = lastName;
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

3) I created the context

public class PocoContext : ObjectContext, IPocoContext
{
    private IObjectSet<Person> persons;

    public PocoContext() : base("name=PocoContainer", "PocoContainer")
    {
        ContextOptions.LazyLoadingEnabled = true;
        persons= CreateObjectSet<Person>();
    }

    public IObjectSet<Person> Persons
    {
        get
        {
            return persons;
        }
    }

    public int Save()
    {
        return base.SaveChanges();
    }
}

The interface is as follows:

public interface IPocoContext
{
    IObjectSet<Person> Persons { get; }

    int Save();
}

4) Finally, I created a repository that implements the interface:

public class PersonRepository : IEntityRepository<Person>
{
    private IPocoContext context;

    public PersonRepository()
    {
        context = new PocoContext();
    }

    public PersonRepository(IPocoContext context)
    {
        this.context = context;
    }

    // other methods from IEntityRepository<T>
}

public interface IEntityRepository<T>
{   
    void Add(T entity);
    List<T> GetAll();
    T GetById(int id);
    void Delete(T entity);

}

Now, when I deal with this, this project dictates to me to create an instance of the repository every time I want to get or change some data, for example:

using (var context = new PocoContext())
{   
    PersonRepository prep = new PersonRepository();

    List<Person> pers = prep.GetAll();
}

- , , - , .

, ? ? , ?

+5
3

:

using (var context = new PocoContext())
{   
    PersonRepository prep = new PersonRepository();

    List<Person> pers = prep.GetAll();
}

, , ? . , , ?

? - . ( ), , . .

, - .

, :

private SomeRepositoryType _someRepository
public SomeRepositoryType SomeRepository
{
    get { _someRepository ?? (_someRepository = new SomeRepositoryType(context)) }
}

. , , factory, factory, / .

Btw. ?

+2

POCO , maeby EF Code First? , Code First, , EDMX .

+1

Injection Dependency , Castle Windsor, AutoFac .., .

0

All Articles