Select happy medium in practices of Entity Framework

Between 3 most known practices of Entity Framework (Code First, Model First, Database First) I selected one happy medium, because no one is perfect by itself. I like clean POCO classes of Code First, but it is not so easy to handle a good scheme/structure of database without modelling. In Database First and Model First I don't like EDMX files and a lot of redundancy.

So I found a perfect solution for me:

1. I do modelling in any Modelling tool which supports generating SQL scripts for SQL Server.

Picture. Part of ready scheme.

2. Create database and execute script in SQL Management Studio

3. And... here is... make a Reverse Engineering of database into model POCO classes (just like Code First) with Visual Studio plug-in: Entity Framework Power Tools

Picture. Plugin for Reverse Engineering installed.

Picture. Here command to select for Reverse Engineering.

Picture. Result is POCO classes in the Models folder of my Model project. It includes database context, classes and mapping-classes.

Next step is to use ANY useful Inversion Of Control Framework and use Repository and Storage patterns to use database in any part of my project. So, I got flexible and nice structure. You may find an example of using Simple Injector in my MVC project. ModelContainer is class which registers all classes/repositories and services to resolve dependencies between them. Repositories supports simple operations for any database object/POCO class: Insert, GetById, Delete, Update.

namespace Phi.Repository
{
    using System.Web;
    using Microsoft.AspNet.Identity;
    using Phi.Models;
    using SimpleInjector;

    public class ModelContainer
    {
        private static readonly Container _instance;

        static ModelContainer()
        {
            _instance = new Container();

            _instance.Register<IDbContext, phiContext>();
            _instance.RegisterManyForOpenGeneric(
                typeof(IRepository<>),
                new[]
                {
                    typeof(EfRepository<PhiUser>),
                    typeof(EfRepository<Role>),
                    typeof(EfRepository<UserAttribute>),
                    typeof(EfRepository<UserRole>),
                    typeof(EfRepository<UserClaim>),
                    typeof(EfRepository<UserLogin>),
                    typeof(EfRepository<Setting>),
                    …
                });
            _instance.Register<IAuthProvider, AuthProvider>();
            _instance.Register<HttpContextBase>(() => new HttpContextWrapper(HttpContext.Current));
            _instance.Register<IUserStore<PhiUser>, UserStore>();
            _instance.Register<IUserClaimStore<PhiUser>, UserStore>();
            _instance.Register<IUserLoginStore<PhiUser>, UserStore>();
            _instance.Register<IUserRoleStore<PhiUser>, UserStore>();
            _instance.Register<IUserPasswordStore<PhiUser>, UserStore>();

            _instance.Register<IRoleStore<Role>, RoleStore>();

            …
        }

        public static Container Instance
        {
            get
            {
                return _instance;
            }
        }
    }
}

...and last step is to register your Injector in Application_Start method:

protected void Application_Start()
{
    …
    var container = ModelContainer.Instance;

    var assem = Assembly.GetExecutingAssembly();
    container.RegisterMvcControllers(assem);
    container.RegisterMvcAttributeFilterProvider();
    container.Verify();

    // Set resolver.
    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    …
}

So, I got simple POCO classes describing database model and didn't write any peace of code by myself. I create only logic. So, any change in my database could be done by simple drawing of relations and rectangles on my schema and pressing couple of buttons to generate script, create database and update model. All done automatically.

Комментарии

  1. Thank you for your great article. If you are looking for asp.net hosting feel free to visit my asp.net hosting review site at http://cheaphostingasp.net.

    ОтветитьУдалить

Отправить комментарий

Популярные

Кастомизируем ASP.NET Identity 2.0

Делаем себе бесплатный VPN на Amazon EC2

Выбираем все плюсы из трех парадигм Entity Framework