How to use TransactionScope while following mvc4 dependency Injection - asp.net

As i am new to Dependency Injection concept. I am following Database first approach and using Entity Framework.
I have created IRepository Interface containing all crude operation.
and i have class Repository class implementing this`interface
I am creating the classes and interfaces of db tables and implementing these two into them.`How can I use Transaction scope while Insertion and Update.
And How to use joins in controller while following this pattern

If this three tables present three linked entities, you should use navigation properties in your E-F mapping. In this case, you create one entity with navigation properties and save it. Transaction will be addwd automatically.
If your entities is not linked with navigation properties, you should have in your repository special method, that will add all three entities to db and in this method you should use TransactionScope

Related

ASP.NET Using one controller from another

I am creating an mvc project, for simplification i have two entitys: Movies and MoviesGenre.
I want to display a list of genres and the amount of movies each of them contains.
Now i have a problem with the design. I am not sure who is responsible for it. I solved that by creating a method in MovieController that returns the amount of movies by genre id and created a method on the MoviesGenreController that select all the genres and uses the MovieController(By instantiating an object) method to get their count.
That doesn't seems like good design to me. Which controller is responsible for this? Do I maybe need to create an extra controller for this logic? Thanks.
You need a data layer project which will manage the access of each controller to the underlying database.
I would suggest the following design:
create a library project (DataLayer) project which connects to the database.
Potential methods exposed:
List GetAllGenres();
List GetMoviesByGenre()
You can either inject the DataLayer as a service or just simply allocate a new object in each controller ctor. This is more like a personal preference... The DI approach is more flexible a more in line with the DotNetCore architecture.
Both MovieController and MovieGenreController should use the methods from the DataLayer.

How can I give business logic to a Symfony model class that is related to other entities?

In a Symfony 2 application I need a model class (without persistence in database) with a business logic features that involves to other entities (with persistence).
In my conceptual model there are "Rooms", "Connections" and "Algorithms". The Rooms and the Connections are Symfony entities based on doctrine. The Algorithms should be model classes or entities without persistence in database.
The business logic is the follow:
- There are different kinds of Algorithms
- One Algorithm makes relations between Rooms and Connections in function different criterias. This implies to retrieve Rooms or Connections from database, apply the logic, modify or create new entities to database.
The first approach I've made is an Algorithm Factory as a Symfony Service to provide a object to create an Algorithm base on a type parameter.
The controller invokes this service and call the algorithm's function to apply the logic.
The conceptual problem I've is the following: I don't know which type of Symfony element should be the Algorithm class. If it's an Entity it can't get Rooms or Connections from database with Doctrine Manager, isn't it? Should be it a Controller as a Service in terms of Symfony?
I've my doubts in how to represent this in Symfony architecture.
Thank you
It doesn't have to be controller or entity, you can have it as a normal PHP class and call it and work with it within the other, Symfony-specific elements.
Where to put it is also a matter of preference.
If you need to access Entities, you can define this new class as service and inject other services as arguments.
Example:
In your config.yml
app.algorithms_factory:
class: AppBundle\Utils\AlgorithmsFactory
arguments: [#doctrine.orm.entity_manager]
In your controller:
class RoomController extends Controller
{
public function editRoomAction($roomId, Request $request = null)
{
$algorithmFactory = $this->get('app.algorithms_factory');
....
}
}

Identity 2.0: ApplicationUser extension using a database first approach

From a couple of articles I have found online
http://typecastexception.com/post/2013/10/27/Configuring-Db-Connection-and-Code-First-Migration-for-Identity-Accounts-in-ASPNET-MVC-5-and-Visual-Studio-2013.aspx
http://www.codeproject.com/Articles/790720/ASP-NET-Identity-Customizing-Users-and-Roles
I have seen it is very simple to extend the ApplicationUser class in MVC 5/Identity 2.0. It basically requires adding of property to that class and all dependent views/viewmodels etc to implement the new functionality. The only question I have remaining is due to the fact that these articles all give you examples in regards to a code first perspective. How would extending the Applicationser class work with a database first perspective?
Here is what I imagine.
1.) Change the connection string to your production database. (In my case SQL Azure)
2.) Create the tables that are normally automatically created by identity 2.0 in SQL Azure.
3.) Fill those tables with the default properties and types.
4.) Add custom properties to the AspNetUsers table. (E.G. City, Zip, etc)
5.) Add those properties to the actual ApplicationUser class
6.) Update dependent views, controllers, viewmodels etc.
Is there a more simple way in doing this?
No, there is no other way to extend ApplicationUser. Code-First is pretty much the same, only adding properties first, create migration, run migration, update your controllers/views.

Shared PKs and ObjectContext errors

I am using EF 4.3 in an ASP.NET WebForms application. I've started with model first approach with context object of type ObjectContext and POCO code generator (via T4).
At the beginning the Context was created at the beginning of every service method. While working on performance I decided to switch to context per web request. Unfortunately I have encountered an issue with Table-per-Type inheritance. I have two entities: Offer and OfferEdit. They are in a one to one relationship, and both share the same primary key. Basically an OfferEdit (OfferEdits table) is created once an Offer is being edited.
I query the context for a particular Offer entity more then once during web request. The error I get trying to execute:
var offer = Context.Offer.Where(o => o.Id == offerId).FirstOrDefault()
when this offer is already loaded to Context.Offer.EntitySet is
All objects in the EntitySet 'RuchEntities.Offer' must have unique primary keys.
However, an instance of type 'Ruch.Data.Model.OfferEdit' and an instance of type'Ruch.Data.Model.Offer' both have the same primary key
value,'EntitySet=Offer;Id=4139'.
Will appreciate all advice.
Sounds like you are misusing TPT inheritance. To make it clear EF inheritance works exactly same way as in .NET - the entity can be either of type Offer or OfferEdit. You can convert OfferEdit to Offer but it is still OfferEdit. It can never be of both types which means you can never have Offer entity with Id same as OfferEdit entity because same key cannot be used by two entity instances. You also never can change instance of Offer to OfferEdit because .NET doesn't allow you changing type of existing instance.

Why does Entity Framework automatically use the ObjectContext instead of the DbContext when mapping database tables using ADO.NET Entity datamodel

I am following the database approach first; I have created the tables in my SQL Server 2008 database, then I map those tables to Entity Framework classes using an ADO.NET Entity Data Model. But when I opened the designer.cs file I found the following code in the class definition which was created automatically:
public partial class PortalEntities : ObjectContext
so I have the following three question that get my confused:
Why does my PortalEntities class derive from ObjectContext and not DbContext as I was expecting?
Is there a major difference between ObjectContext & DbContext, or they are mainly the same and offer that same capabilities
When I try to write the something similar to the following code:
Student student = db.Students.Find(id);
I found that I cannot use .Find() method as I used to do using DbContext, so does this mean that ObjectContext & DbContext have different methods that I can use?
BR
The DbContext is a wrapper around the ObjectContext which simplifies the interface for the things we do most.
If you have an DbContext you can still access the ObjectContexttrough ((IObjectContextAdapter)dbContext).ObjectContext;
If you want to use the DbContext instead of the ObjectContext when using database first, you can switch the template that's used for generating your code. You can do this by right-clicking in your EDMX and selecting 'Add Code Generation Item'. You can then select the DbContext template.
Here is an example of the whole process.
Since VS2012 the default code generation changed from ObjectContext to DbContext.

Resources