Simplest model code - zend-framework

I am very experienced with the CakePHP framework but am checking out the Zend Framework for an application that will receive massive traffic.
I'm going through the quickstart tutorial in the documentation and got to the "Create a Model and Database Table" page.
Must I or should I create all those model classes it mentions, i.e.
application/models/DbTable/Guestbook.php
application/models/GuestbookMapper.php
application/models/Guestbook.php
Coming from CakePHP it seems like quite a lot of code for some functionality I would of thought of as quite basic and generic.
Or can I just create application/models/Guestbook.php and have it extend Zend_Db_Table_Abstract?
Any help would be much appreciated.

You can create your model after the DbTable class, however one of the benefits of doing a dataMapper class between your model and your DbTable class is that you can abstract more the data engine and create strong business rules.

Zend Framework does not actually impose any restrictions or requirements on your Model classes and you are free to create them however you would like.
Depending on the requirements and scope of the project I generally still go with subclassing Zend_Db_Table_Abstract (often with my own custom extension of it as well). When it comes to a large or complicated project I have found that using a dataMapper pattern has been very helpful.
At the same time I have had some models that do not extend any class at all. They simply are there to provide some logic and do not relate to a database.

Related

Can’t find best way for apply best in code design techniques in software dev

[Pre]
I have to say that I'm dummy newbie who is trying to get together important puzzles with such crucial details as DDD, TDD, MVVM, and EFCore. I have an about 10 years of windows form develop experience in complete wrong manner, and after I'm joined to Plurasight I'm understood that I'm just lost my last 10 years, and this is really sad :).
[Problem description]
I have an App that i want to re-write from scratch by using latest and greatest technics that've learned for the last 6 month on Pluralsight, but the problem is that these new knowledge’s is stopping me, because simply I'm afraid that I'll do it wrong again...(that is stupid I know, but it is what it is).
So back to my questions, I have a big problem domain, and pretty well documented business logic, which i have to turn in to the code. I'm understand that my start point is design data layer, for these purposes I want to use Entity framework core (I saw Julie Lerman's course on Pluralsight and I think's she is amazing and inspires me to use EFCore as ORM for my app). But at the same time leakage of experience produces more questions than what I’ve learned with Pluralsight, and I will try to write them all(please don’t judge me too hard)
It is looks like that I will need 2 or even more data model projects in my solution, and here is why I have multiple document set types, each of the type contain more than one reference books used to generate unique file names and data sheets. But it looks weird to me have 3 Data model projects such as MyApp.PackType1.DataModel, MyApp.PackType2.DataModel, and each of them will be preinstalled with the EFCore, and each of them will generates its own database based on Data Context defined by EF. Isn’t it very redundant or this is correct way?
I don’t understand how to join these multiple Data Models projects, including Shared Kernel into the one nice model
I don’t understand what is the best way to design my data classes? Should they be just POCO’s or I can design them as nice looking classes with the private var’s and public properties? What are the best practices in here?
Also I don’t understand what is the best practice to use a MVVM pattern on top of that, and is it applicable at all to use MVVM in this case?
Should I keep my Tests in separate projects like MyApp.PackType1.DataModel.Tests, or keep them in same project?
Best regards,
Maks!
P.S.
Apologize for unclear definitions and questions, English isn't my native language.
It's very complicated to answer your question because you have asked for a lot of details, but I going to provide a brief answer and I hope it will be helpful.
You can have only one model for your entities (DDD) and create sub model from this model in your end level projects (Web API or UI)
Read point #1
You have to create an Entity Layer project that represents your database and then you can create DTO's for specific scenarios
From my point of view, use Angular but you can use another UI framework such as React or VueJs, but I prefer to use Angular to build UI interfaces and consume .NET Core Web API from client
Create unit tests and integration tests for you Web API projects and as additional feature you can use Db in memory provider for tests
May be this guide is useful: https://www.codeproject.com/Articles/1160586/Entity-Framework-Core-for-Enterprise
Regards
Hm, multiple DbContexts (models) usually come about when you have distinct databases you are using. General rule is one Context = one Database. Exceptions can occur when there are a lot of tables that can be grouped functionally, but there are downsides to that approach.
A DbContext is a repository pattern but for individual tables. Using a Unit of Work pattern and layering with a custom repository provider would allow you to make it "appear" as a single database, hiding the complexity from the front-end.
Your entity descriptions are usually created as straight POCO. You can get creative with different DTOs
In a nutshell, an MVVM pattern goes like this:
Request from UI to a controller
Controller possibly issues multiple calls to Data Layer to gather data
Assemble data in a single ViewModel (everything the page needs)
Return to UI
The beauty of the approach is single roundtrip (request/response) to the UI
Separate Project in my opinion. There are techniques to spoof the database connection using EF so you are not using "live" data.
That CodeProject article will come in handy.

Entity Framework with interfaces does not work - what the best way to handle the same?

I am using Entity Framework and would like to use TPH with interfaces. So I have a created an interface, "ICustomer", which maps to "SimpleCustomer" and "DiscountedCustomer" class as shown below. Below is the model builder code. From what I understand we can not use interfaces with Entity Framework, so what's the best way?
modelBuilder.Entity<ICustomer>().ToTable("tblCustomer")
.Map<SimpleCustomer>(x => x.Requires("CustomerType").HasValue("S"))
.Map<DiscountedCustomer>(x => x.Requires("CustomerType").HasValue("D"));
My application uses interfaces all over the UI and would like to have a smooth type casting to Entity Framework. So is what the best way?
Entity Framework does not support TPH with interfaces (sorry for stating the obvious). This may not be the solution you are looking for, but I am still going to put it there because it seems to be the only solution as of 16 April 2015.
In Entity Framework 6, the closest you can get is - Use abstract classes instead of interfaces. This article talks about TPH in EF in great detail.
My suggestion is if you want to use interfaces and maintain the hierarchy and also still want smooth typecasting, consider using automapper with abstract classes. This way your UI will still use Interfaces, but can be mapped to domain model using automapper profiles. Atleast till the interface support arrives. It will not be a quick one if the application is large and has hundreds of domain models, so need to plan it wisely.
If you are creating it from scratch, you can simply use abstract classes from UI layer to DAL without any re-factoring.

How to use a RESTful API as ORM in CakePHP 3?

In CakePHP 1.x/2.x, it was fairly simple to have a model's data come from a REST API (as opposed to a relational database), by defining a custom datasource. (Neil Crookes' CakePHP-ReST-DataSource-Plugin was a great place to start.) Slap your custom datasource on your model, and you're making calls like $this->MyModel->find() just like you were querying a MySQL table called my_models.
I'm trying to figure out how to achieve this same effect under CakePHP 3.0. That is, make find()/save()/set()/get() calls against a Table/Entity driven by a REST API.
Since 3.0's ORM system is A) fairly new, and B) a rather large departure from the old way of doing things, I haven't found any information about how to do something like this. In fact, based on this SlideShare from
José Lorenzo Rodríguez, it sounds like it might not be possible.
This means:
not going to connect to stuff that is not a relational database.
Is there someone more familiar with CakePHP 3.0 that could clarify if what I'm looking for is possible under the new ORM system? If so, could you explain what classes you'd have to extend or implement to achieve such a function?
If you want to create a complete adapter for your Rest datasource using the interfaces and classes provided by CakePHP, take a look at this early experiment fro the CakePHP team on making a datasource for Elastic Search.
Elastic Search uses a Rest API and this plugin attempts to create classes that work similar to the CakePHP ORM:
https://github.com/cakephp/elastic-search
What it implements is basically the following:
A Type class that implements the RepositoryInterface
A Document class that implements the EntityInterface
A Query class that can be used as a collection object and has similar methods
In the near future it will provide a paginator adaptor and a form helper adaptor.
If you want to save yourself this trouble, because there for you there is little value in exposing your datasource as something ORM-like, you can just use guzzle or any similar library to interface with your API and use it as a service instead of a full-blown layer.
In the year since I asked this question, UseMuffin has built a Webservice plugin that purports to "bring [...] the power of the CakePHP ORM to your favourite webservices." This sounds like exactly what I wanted at the time.

Does Entity Framework DB First (EDMX) prevent proper Separation of Concerns?

I am new to entity framework and MVC, and trying to understand what constitutes a good design approach for a new application.
There are several ways of using Entity Framework. However, for my project, the best looking option is DB First. I've played around with an EDMX file, and I have got as far as using the DbContext code generator to create my wrapper classes.
I plan on using the repository and unit-of-work patterns, and using ninject for DI.
However, it does not seem "proper", from a SoC point of view, that whilst my respository will hide the implementation of the data store (EF) from my code, the model classes themselves are very much EF flavoured.
It seems that using EDMX-based approaches to EF blur the separation of concerns. Only POCO support seems to allow a true separation, but POCO has some other limitations that I don't like.
Am I missing something, or does using EDMX have this drawback?
Are people using an auto mapper to convert between the entity model and another, clean, SoCced model?
thanks
Tian
I don't have a strong opinion on the Separation Of Concerns question, but I have used both the standard ADO.Net version of EF and POCO and it is not difficult at all to customise the output of the the T4 code generation script for POCO to address any concerns you have about the structure of the objects created. That sounds like it would probably be a good starting point for what you are looking to do.
Once you know you are looking for T4 templates there are quite a few tutorials and a lot of helpful SO questions that can give you an idea of what you need to do.

is it that easy working with ADO.NET Entity framework in real programming?

HI Guys,
I was watching these videos series about Entity Framework:
http://msdn.microsoft.com/en-us/data/ff191186.aspx
is that easy building application in real world programming??? and is it ....reliable...has good performance...
"I am a graduate.."
thanks
Entity Framework is a valid real world data access tool. It is very easy to get up and running with EF. You simply import (or create in EF 4) your data model. You then can rename it to make it more code friendly. And then you are off querying databases.
Performance
I have been on multiple projects that use it, some which require high throughput, others that have low performance requirements. Entity Framework out of the box is not the fastest solution in the world, so there are a lot of performance tweaks that have to go on, but its all do able.
Reliability
We never have issues with reliability. We have never had an issue with EF in general, its always data content related. Trying to insert duplicated data, etc.
Other Tangibles
EF follows a pattern which allows for you to do some fun stuff with templates and abstract classes. All entities inerit from a class, entities that have references inherit from other classes. All Entity Contexts inherit from ;) ObjectContext classes, which provide a base set of functionality that allows you to create generic DAO implementations that can be reused throughout the enterprise.
If you are using UI dev, you can also use Data Services that wrap EF, as a fast gateway to your databse. The only downside of this is that you dont have access to the full suite of the Entity Framework.