locked
How to implement EF Code First and WCFDataService RRS feed

  • Question

  • A bit of history first.  I created a EF Code First Library that contains POCO Objects as my Models, a generic DataProvider that inherits from DbContext, generic Repostory that implements the generic DataProvider, and a generic Service that implements the repository.  I have used this library successfully in WPF (MVVM), ASP.Net, Window Forms, and ASP MVC applications.

    For this discussion I will reference the Company Model

    From the top down, I create a Service class called CompanyService that inherits from a base Service Class.  The CompanyService class contains all of the business logic for the Company Model.  This class uses the Repository class to perform the CRUD operations.  The Repository then encapsulates all the DataProvider class operations.

    I have done some research on using EF with WCFDataService, but I can't get my head around how to implement my library with it, particulary when it comes to overriding the CreateDataSource() Method. 

    It may be that I should just use a WCF Service instead, maybe I'm not understanding the purpose of the WCFDataService.

    I have listed partial code for the classes involved:

    	public class CompanyService : ServiceBase<Company> ,ICompanyService
        {
            public Company GetCompanyByFolderId(string eFolderId)
            {
                return (Company)GetModelByFolderId(eFolderId);
            }
    	}
    	
        public abstract class ServiceBase<TModel> : IService<TModel> where TModel : class, IModel
        {
            private IDataProvider _dataProvider;
            public IDataProvider DataProvider
            {
                get
                {
                    if (_dataProvider == null)
                    {
                        string connectionStringName = Properties.Settings.Default.DataProvider;
                        bool enableLazyLoading = true;
                        _dataProvider = new DataProvider(connectionStringName, enableLazyLoading);
                    }
                    return _dataProvider;
                }
                set
                {
                    _dataProvider = value;
                }
            }
            private IRepository<TModel> _repository;
            public IRepository<TModel> Repository
            {
                get
                {
                    if (_repository == null)
                    {
                        _repository = new Repository<TModel>(DataProvider);
                    }
                    return _repository;
                }
                set
                {
                    _repository = value;
                }
            }
    		
            public TModel GetModelByFolderId(String folderId)
            {
                return GetTable().FirstOrDefault(o => o.EFolderid == folderId);
            }
            public virtual IQueryable<TModel> GetTable()
            {
                return Repository.GetTable();
            }
    	}
    	
        public class Repository<TModel> : IRepository<TModel> where TModel : class, IModel
        {
            private IDataProvider _dataProvider;
            public Repository(IDataProvider dataProvider)
            {
                _dataProvider = dataProvider;
            }
            private IDbSet<TModel> DbSet
            {
                get
                {
                    return _dataProvider.Set<TModel>();
                }
            }
    		
    		public IQueryable<TModel> GetTable()
            {
                return _dataProvider.GetTable<TModel>();
            }
    	}
    	
    	public class DataProvider : DbContext, IDataProvider 
        {
            public DataProvider()
            {
            }
            public DataProvider(string connectionStringName, bool enableLazyLoading = true)
                : base(connectionStringName)
            {
                Configuration.LazyLoadingEnabled = enableLazyLoading;
                //Configuration.ProxyCreationEnabled = false;
            }
            public new IDbSet<TModel> Set<TModel>() where TModel : class
            {
                return base.Set<TModel>();
            }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Configurations.Add(new CompanyMapping());
                base.OnModelCreating(modelBuilder);
            }
            public IQueryable<TModel> GetTable<TModel>() where TModel : class
            {
                return Set<TModel>().AsQueryable();
            }
    	}

    Then my Test looks something like this:

    [TestClass()]
        public class CompanyServiceTest
        {
            [TestMethod()]
            public void GetCompanies()
            {
                CompanyService target = new CompanyService();
                IQueryable<Company> companies = target.GetTable();
                Assert.IsNotNull(companies);
            }
            [TestMethod()]
            public void GetCompanyByFolderId()
            {
                CompanyService target = new CompanyService();
                Company company = target.GetCompanyByFolderId("0000000000000000000000000172403");
                Assert.IsNotNull(company);
            }
    	}


    Bill Behning

    Wednesday, May 29, 2013 11:56 PM

All replies