taking advantage of inheritance in Controllers and Views

前端 未结 3 420
清歌不尽
清歌不尽 2021-01-04 06:10

I had posted this review on codereview.stackexchange.com a while ago... I feel it may be more suitable for stackoverflow, since it is more of a question than a code review.

3条回答
  •  一整个雨季
    2021-01-04 06:56

    More abstraction -> more abstraction leaks.

    I have complete solution how to generate controllers from EF model definition using exression trees

    Check this, how the controller's code looks after all "duplicated code" is removed:

    https://github.com/DashboardCode/Routines/blob/master/AdminkaV1/Injected.AspCore.MvcApp/Controllers/UsersController.cs

    or this ("Roles" can be created when "Users" was imported from AD )

    https://github.com/DashboardCode/Routines/blob/master/AdminkaV1/Injected.AspCore.MvcApp/Controllers/RolesController.cs

    Those blocks on start configures full controller with a lot of features (e.g. rowversion support, sql server constraints error parsers and etc., one-to many, many-to-many, unhandled expceptions support)

    static ControllerMeta meta = new ControllerMeta(
                // how to find entity by "id"      
                findByIdExpression: id => e => e.UserId == id,
                // how to extract "id" from http responce      
                keyConverter: Converters.TryParseInt,
                // configure EF includes for Index page
                indexIncludes: chain => chain
                           .IncludeAll(e => e.UserPrivilegeMap)
                // ... and so on, try to read it
    

    But those definitions actually is a kind of new Internal DSL. In fact you are asking "how to write new DSL that defines controllers/pages in bigger bricks". Answer is - it is easy, but there is a reason why people stick to general purpose languages. It is because it is "general".

    P.S. One detail: if you want that "full controller" could be contracted/configured at run time, therefore you are forced to parse http requests by youself - and ignore MS parameters binding model - it is because BindAttribute - important binding modificator - can't be "set up" run time simple way. For many people - even when they loose "int id" in parameters list - is too high price. Even if refusing of MS parameters binding is very logical: why would you need to keep MS parameters binding magic when you are going to configure whole controller magically?

提交回复
热议问题