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;
}
}
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();
}
- , , - , .
, ? ? , ?