T
Tony Johansson
What I want to accomplish is to use class FlowerNetDbContext in class
OrderRepository.
So when I call OrderRepository property in RepositoryFactory I return an
instans of OrderRepository and pass in dbContext
which is a FlowerNetDbContext.
OrderRepository is implementing IOrderRepository
So what do is this. I can't see any problem error in doinf this.
public IOrderRepository OrderRepository
{
get {return new OrderRepository(this.dbContext); ;}
}
The problem start when I do this row
private ProductRepository productRepository =
repositoryFactory.ProductRepository
in class ProductsDisplay below.
The error I get is this
Error 2 A field initializer cannot reference the non-static field, method,
or property 'Assignment_2.ProductsDisplay.repositoryFactory'
c:\datadrivenwebapplicationprogramming_2014\assignment_2\productsdisplay.aspx.cs
19 53 Assignment_2
I mean I instanciate RepositoryFactory and tell him to give me the
ProductRepository by calling the property that is named ProductRepository
So how can I fix this ?
public partial class ProductsDisplay : System.Web.UI.Page
{
private RepositoryFactory repositoryFactory = new RepositoryFactory();
private ProductRepository productRepository =
repositoryFactory.ProductRepositor
....
....
}
public class RepositoryFactory
{
private IOrderRepository orderRepo;
private FlowerNetDbContext dbContext;
public RepositoryFactory()
{
dbContext = new FlowerNetDbContext();
}
public IOrderRepository OrderRepository
{
get {return new OrderRepository(this.dbContext); ;}
}
}
public class OrderRepository : IOrderRepository
{
private FlowerNetDbContext dbContext;
public OrderRepository(FlowerNetDbContext context)
{
dbContext = context;
}
}
//Tony
OrderRepository.
So when I call OrderRepository property in RepositoryFactory I return an
instans of OrderRepository and pass in dbContext
which is a FlowerNetDbContext.
OrderRepository is implementing IOrderRepository
So what do is this. I can't see any problem error in doinf this.
public IOrderRepository OrderRepository
{
get {return new OrderRepository(this.dbContext); ;}
}
The problem start when I do this row
private ProductRepository productRepository =
repositoryFactory.ProductRepository
in class ProductsDisplay below.
The error I get is this
Error 2 A field initializer cannot reference the non-static field, method,
or property 'Assignment_2.ProductsDisplay.repositoryFactory'
c:\datadrivenwebapplicationprogramming_2014\assignment_2\productsdisplay.aspx.cs
19 53 Assignment_2
I mean I instanciate RepositoryFactory and tell him to give me the
ProductRepository by calling the property that is named ProductRepository
So how can I fix this ?
public partial class ProductsDisplay : System.Web.UI.Page
{
private RepositoryFactory repositoryFactory = new RepositoryFactory();
private ProductRepository productRepository =
repositoryFactory.ProductRepositor
....
....
}
public class RepositoryFactory
{
private IOrderRepository orderRepo;
private FlowerNetDbContext dbContext;
public RepositoryFactory()
{
dbContext = new FlowerNetDbContext();
}
public IOrderRepository OrderRepository
{
get {return new OrderRepository(this.dbContext); ;}
}
}
public class OrderRepository : IOrderRepository
{
private FlowerNetDbContext dbContext;
public OrderRepository(FlowerNetDbContext context)
{
dbContext = context;
}
}
//Tony