Common LINQ method using EF - asp.net-mvc-3

So this is probably a stupid question, but I am still not exactly sure how the entity frameworks objects work. I am using EF4 in an MVC3 app, and have two controllers that need to use the same LINQ query against it. Is it best to use a static method that takes the db entity by ref, or should the method use a "using" block for its own entity (as seen in this question)?
I would think the using block would add additional overhead, but I didn't find any examples of the other method. Is there a proper way to make "library" methods for EF access?

In an MVC application the ObjectContext should be scoped to the request. Most DI containers can do this automatically. So you would prefer not using a using block within a method. Instead inject the context via the constructor or pass it as a method argument.

Related

External POCO classes to Aspnetboilerplate AbpEntities. i.e. no inheritace possible

We have a pretty common situation and I'd like to understand the best-practice or trade-offs in Aspnetboilerplate/AspNetZero.com to handle this best.
We import a package (NuGet) of pure C# classes (POCO). These are shared across several system. In our AspNetZero server, we want these to be first class persistent objects. However, they can't inherit from Entity, since they come from the Nuget. What is the best practice here?
My ideas to date (not being the expert here, of course):
If we were to use these classes as EF Navigation Properties in Apb Entities, i.e. always use them as complex-type properties of an Abp Entity class, it could do the trick. In this scenario, one would not even need to define a DbSet, although one could (see: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application )
Alternatively, if we just reference these complex types from an Apb Entity, doesn't EF generate Entity-Proxies for these and automatically make them into EF Navigation Properties (see: https://blogs.msdn.microsoft.com/adonet/2009/12/22/poco-proxies-part-1/ ) or is this not an option the default Abp flow. We'd like to avoid too much custom code (risk).
Any other way via delegation?
Thanks for any Tips/Example/Info!

Do I always have to inject all the dependencies first before using any of its methods in Magento 2?

I'm new to Magento 2. In Magento 1, as you know, we can call any method from other class(es) more easily, thanks to Mage::
In Magento 2, I notice every time I want to use a method(s) from other class(es), I have to inject the dependency(ies) first which can make the constructor looks very long with so many injections. I read we can use object manager but it's not preferable. Not sure why.
The most obvious advantage for me using dependencies instead of object manager, is that you can leverage it anywhere in your class. Using object manager you have to call the methods for each function individually. It may seem a more practical approach at first, but with more complex code your functions will bloat because you always have to refer to object manager instead of referring to the method directly via dependency. I'd rather have a "big block of construction" on top instead of all these object manager instances in my functions.
Also, it can be quite tricky to use object manager correctly. Maybe have a look at this:
https://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-directly

Copy Metadata from Entities to ViewModels in Automapper 5

In my MVC application I use Autofac IoC container together with Automapper. It gives me an opportunity to implement custom ViewModels.
The main question: How can I copy Metadata from Entities to ViewModels using Automapper 5.1.1?
I tried to implement Bettys solution, but I can't correctly register new providers using this approach.
Code is also presented below:
ModelMetadataProviders.Current = new MetadataProvider(
AutoMapper.Mapper.Engine.ConfigurationProvider);
ModelValidatorProviders.Providers.Add(new ValidatorProvider(
AutoMapper.Mapper.Engine.ConfigurationProvider);
or
ModelMetadataProviders.Current = new MetadataProvider(
(AutoMapper.IConfigurationProvider)AutoMapper.Mapper.Configuration);
ModelValidatorProviders.Providers.Add(new ValidatorProvider(
(AutoMapper.IConfigurationProvider)AutoMapper.Mapper.Configuration));
There is no property like Mapper.Engine.
When I implement a second approach, I got invalid operation exception:
Additional information: Mapper not initialized. Call Initialize with
appropriate configuration. If you are trying to use mapper instances
through a container or otherwise, make sure you do not have any calls
to the static Mapper.Map methods, and if you're using ProjectTo or
UseAsDataSource extension methods, make sure you pass in the
appropriate IConfigurationProvider instance.
So, how should I initialize AutoMapper.Mapper.Configuration?
Thank you in advance for your time and help.
Or maybe there is another, better way to map metadata with viewmodels?

Is there a way to Iterate all Controllers/Actions in an ASP.NET MVC3 Site?

I'm trying to make a dynamic menu function in an ASP.NET MVC 3 website - and I'd like to know if there is a built-in way to get all of the Controllers and Actions at runtime?
I realize that I can use reflection to find all public methods on my controllers, but this doesn't exactly give me the relative URL that I should put in the <a href="..."> tag.
Also, I'm going to be decorating some of the 'actions' with filter attributes that dictate whether the current user can see/goto those pages. So it would be best if I had access to the filters as well so as to be able to call the IsAccessGranted() method.
What are my options? What is the best option?
I actually just did that two weeks ago.
var q = from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsClass && type.Namespace != null && type.Namespace.Contains("Controller")
select type;
q.ToList().ForEach(type => doWhatYouNeedToDo(type)));
if you are using T4MVC, then this script will return double entries. To avoid this, work with
&& !type.Name.Contains("T4MVC")
In the method doWhatYouNeedToDo() you could transform the Type object into a DTO that suits your needs and add work further with it.
As far as your dynamic menu is concerned, you could use the MvcSiteMapProvider and implement your own dynamic sitemapprovider with it, so you are no longer bound to the static sitemap xml file.
But reflection is quite slow in .NET, so you might want to store representations of your controllers and method in the database.
There is no built in mechanism in MVC to enumerate over all of your controllers and actions. You would have to use reflection to inspect all the loaded types and look at their methods and the associated attributes. Of course this is assuming that you are using the default reflection-based action dispatching mechanism. Since MVC's pipeline can be replaced in a number of places its easy to inject a system for invoking action methods that is not based on CLR classes and methods. But if you have complete control over your application than you life is easier.
Try TVMVC. You'll still have to use reflection, but the t4 templates will generate a class that's easier to iterate over.

MVC, how view should be accessed from controller?

I'm just learning MVC so you could find my question rather strange...
My Controller have access to different shared objects through Container object passed to Controller's constructor. To access shared objects I should do $this->container->db to access Database adapter or $this->container->memcache to access Memcached adapter. I want to know should I put View object into Container with shared objects or no?
From one side it is really comfortable to take view from this container, but this way I couldn't create multiple Views instances (for example, every time I'm calling Controller's method from View I should have one more View instance). What is the solution? How should I pass View object into Controller and/or how should I create new View instances from Controller?
Thank you!
If you want that DI experience, do it on views as well, but I don't know if it really helps you anyway. Never call controller methods from views. Instead write some partial view methods and call them from views, which define the page layout (something similar to what Rails does).
IMHO if you want to get on MVC gradually, start from core principles and iteratively get to details, but don't learn architectural/design pattern as MVC by parts - architecture, design, the whole matters:)
Hmm, maybe try implementing caching for static parts. IMHO try inserting cacher object (through DI) to controller, and let that object decide if you want to send cached partial view or instantiate a new one. If you want to cache data from db, use the same pattern from controller towards models, so whenever in a controller you need models, ask db cacher object (same DI principle). Is it clear enough?

Resources