none
Bounded context in EF 5.0 and Repository pattern RRS feed

  • Question

  • Hi,

    I built an EF 5.0 repository pattern. Below is the context, repository and unit test.

    My Question here is regarding Bounded Context. MARKET_VARIABLE and CURRENCY are part of the MarketVariableContext properties but IMPUTATION_METHOD is not part of context. Now when I run the test I get the result back for all the three GetAll methods. I would expect repo.GetAll<T>().ToList(); to fail or throw an exception for IMPUTATION_METHOD as context.Set<T>(); does not exist where T = IMPUTATION_METHOD for MarketVariableContext .

    BTW.. DbSet<IMPUTATION_METHOD>  is part of a different context.

    Context:
    
    public class MarketVariableContext : BaseContext<MarketVariableContext>
        {
            public DbSet<MARKET_VARIABLE> MARKET_VARIABLES { get; set; }
            public DbSet<CURRENCY> CURRENCies { get; set; }
        }
    
    public class BaseContext<TContext>: DbContext where TContext : DbContext
        {
            protected BaseContext()
                : base("name=MarketDataDBEntities")
            {
                this.Configuration.LazyLoadingEnabled = false; 
            }
        }
    Repository:
    
    public abstract class BaseRepository : IRepository
        {
            protected BaseRepository()
            {
            }
    
            public DbContext context { get; set; }        
    
            public virtual IQueryable<T> GetAll<T>() where T: class
            {
                return context.Set<T>();
            }
    }
    
    public class MarketVariableRepositorys : BaseRepository
        {
            public MarketVariableRepositorys()
            {
                    this.context = new MarketVariableContext();
            }
    }
    Unit Test:
    
    [TestMethod]
            public void GetAllTests()
            {
                using (var repo = new MarketVariableRepositorys())
                {
                    List<MARKET_VARIABLE> marketVariables = repo.GetAll<MARKET_VARIABLE>().ToList();
                    List<CURRENCY> currencies = repo.GetAll<CURRENCY>().ToList();
                    List<IMPUTATION_METHOD> im = repo.GetAll<IMPUTATION_METHOD>().ToList();
                }
            }





    • Edited by DevMandala Friday, August 15, 2014 2:47 PM
    Thursday, August 14, 2014 4:14 AM

Answers

  • Hello,

    In your sub context class, adding code as below:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Ignore<OrderDetail>();
            }

    To let the model ignore the type.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 4, 2014 6:03 AM
    Moderator

All replies

  • Hi. You are right you should get an exception telling you that "IMPUTATION_METHOD is not part of the model for the current context" (or similar).
    Thursday, August 14, 2014 5:50 AM
  • Thank you for the reply!! But I am not getting any exception, instead I am getting the list of entities back successfully. Not sure if I am doing anything wrong? How can I stop getting the List<IMPUTATION_METHOD> for the context where it does not belong to? 
    Thursday, August 14, 2014 8:06 PM
  • From the code you have shown you will get an exception. There must be something else going on. If you break when the code is running you can have a look at the context on your repository and where it is getting the IMPUTATION_METHOD DbSet from.

    Friday, August 15, 2014 2:08 AM
  • IMPUTATION_METHOD  is part of my edmx model that I generated from database. When I created the custom context in this case "MarketVariableContext" I was expecting an exception be thrown for entities those does not belong to this context. But instead it is getting the List<IMPUTATION_METHOD> successfully. 
    Friday, August 15, 2014 4:15 AM
  • Hello DevMandala,

    >> But I am not getting any exception, instead I am getting the list of entities back successfully.

    With your code, I made a test and got the same result,based on my understanding, as it describes for the DbSet<T>() method “Returns a DbSet instance for access to entities of the given type in the context, the ObjectStateManager, and the underlying store”, I think this is correct for getting the List<IMPUTATION_METHOD> successfully since in actual the sub  context inherits from the based context – DbContext, so the MarketVariableContext still uses the DbContexts Set<T>() method which contains all entities types imported from database.

    If I misunderstand, please let me know freely.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 22, 2014 6:08 AM
    Moderator
  • Thank you Fred for your help.. How can i achieve bounded context in my case above? I referred to this bounded context pattern from Julia lehrman course on plural sight. Also I am not able to override these methods from DBContext as they are not virtual.

            public DbSet<TEntity> Set<TEntity>() where TEntity : class;
            public DbSet Set(Type entityType);

    • Edited by DevMandala Wednesday, September 3, 2014 4:36 PM
    Wednesday, September 3, 2014 3:21 PM
  • Hello,

    In your sub context class, adding code as below:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Ignore<OrderDetail>();
            }

    To let the model ignore the type.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 4, 2014 6:03 AM
    Moderator