is there any way to map 2 Models once in my application ( Mapper.CreateMap()) for example in global.asax and then wherever necessary just call Mapper.Map() in codes ?
Yes you can, and in fact that's the recommended way to configure AutoMapper. Creating the initial mappings is the (relatively) expensive part of AutoMapper, so you want to do it only once.
As you suggest, Global.asax is a good place to do it. Although it's a good idea to put it in a separate class, for example Bootstrapper, that the Application_Start method calls into, such that this class and method can also be called from your unit tests.
From the AutoMapper docs:
Where do I configure AutoMapper?
If you're using the static Mapper method, configuration only needs to happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications. Typically, the configuration bootstrapper class is in its own class, and this bootstrapper class is called from the startup method.
See also this question, which has some good ideas including a suggestion from Jimmy Bogard, who wrote AutoMapper.
Related
I'm following this example for a queued hosted service to add this to an ASP.NET Core application, and it's not clear to me where StartMonitorLoop should be called. I ended up modifying it to be EnsureMonitorLoop, added a check so that it's the call to Task.Run is only made once, added a MonitorLoop parameter to constructor for my API controller, and called EnsureMonitorLoop from there. It smells kind of funny to me that the API controller constructor should be kicking off monitoring the queue. The example Program.cs seems is very different from the one generated for me by Visual Studio. Mine uses the WebHost.CreateDefaultBuilder(args).UseStartup<Startup> approach. That is where they call StartMonitorLoop.
Where is the correct place to call StartMonitorLoop, and why? Thanks!
The docs aren't super clear here, but MonitorLoop is not actually part of this. It's an example service for use in a console app, simply to demonstrate how the queued background worker works. You can take some inspiration from this class for your app, but the concept of StartMonitorLoop doesn't apply to ASP.NET Core at all.
Just to be a bit more clear: in actual practice you would inject IBackgroundTaskQueue into a controller class, for example, and then add some task to that, just like MonitorLoop does (without all the key input jazz). You wouldn't actually have MonitorLoop or anything like it though.
I am trying to implement Log4Net file appender in asp.net. I have been successful implementing it. However I am not sure about correct architecture to implement it.
I can add a logger in each page and log information. However, I was thinking to centralize the logger class. May be implement a singleton pattern. But i was wondering what will happen if a request for same page comes from two different browsers. I can implement Thread Static and then every page instead of initializing their own logger would use this centralize logger class to log.
I suppose the log4net file appender or rolling file appender using a queue mechanism to write to the log file. Because only one handle of the file can be acquired to write to a file.
Can anyone help me in this regard. Am i going the right way or i will have issues down the road when there will be tens and hundreds of requests coming from different browsers.
I recommend not to use a singleton, but instead to use a logger for each controller and class that you want to log from. Loggers are cheap to create and cached by log4net - and even more so if you declare them as static within the class - and by having one per class you can change logging per class or namespace by changing the log4net configuration at runtime - say to enable some debug logging to aid diagnosing a problem in production, or to turn down some logging which is unexpectedly noisy. You can do this without recycling your app if you have your log config in a separate file.
Also if you're going to use file logging, make sure you use MinimalLock
I'm starting with a Symfony2 project. I know the framework basics but I have a question:
Where is the right place to pot those helper classes I create for help or for the business logic?
Max's answer is correct. However I question the path he recommends for your code.
The following classes and files have specific emplacements:
Service Container Extensions (belong in) DependencyInjection/
from http://symfony.com/doc/current/cookbook/bundles/best_practices.html
That says your Services should be placed in a folder called 'DependencyInjection', not 'Services'. In full, it should be src/Foo/BarBundle/DependencyInjection
I say this as someone that had the former and has just finished moving them all to the latter (!)
What #Adam says is wrong, you have to store your Dependency Injection Extensions in DependecyInjection directory, not the services itself. In the documentation says that you can store your (custom) business logic classes in any place you like.
http://symfony.com/doc/current/best_practices/business-logic.html
The best way to keep the business logic is create service to handle all the logic. So it will be in:
src/Foo/BarBundle/Service
and you need to call the service in the services.yml.
I recently did some small work on an existing Symfony2 project. As described by answer from Tuong Le, I created my Helper classes under the Helper directory of the bundle and class name with Helper suffix i.e. the helper class is located at:
src/MyBundle/Helper/MyUtilHelper.php
I can use MyUtilHelper class in my bundle without calling the service container i.e. I didn't need to call.
$container->get('my_util');
I don't really know whether there is some special config. in my setup; someone already got it setup and I was just adding new functionality.
You can create the custom classes under your Bundle, such as under a folder Helper/..
However, to use those helper in your code, you'll need to define those Helper(s) in your service description file (such as services.xml)... Then you can use $container->get('your_helper')->
According to official documentation - in particular - Symfony Best Practices - you should store your services in Utils folder under the src. I belive, that this is correct way regardless of whether you want or don't wont to make the functionality provided by services of your bundle available to other parts of application via Service Container. Furthermore, you can store helper classes in any place you consider suitable. Concerning #Adam Knowles and #PachinSV answers - they are not quite right because they do not answer your question - "Where is the right place to pot those helper classes I create for help or for the business logic?" or "Where to store classes which I want to register and use via Service Container" - but not where to put bundle Extension class - which main purpose is to provide information about configuration which should be automatically loaded from your bundle to apps Service Container during the process of booting the Kernel.
I'm working with Orchard CMS and it is better CMS for me. I want to understand how it does the logging and whether I can add my own logging or not. I saw that Orchard uses NullLogger class and it does no work. I've opened the App_Data.Logs folder and have seen that there are the log files. But how? I searched in code where is the trick that replaces NullLogger with log4net (I guess this is log4net, because the log format and the formatting for log4net.config are very similar) but I haven't found this.
Can somebody answer me:
How Orchard does the logging?
Whether I can add my own logger and if yes what best practices exist to do this?
Thanks, Andrey.
An Autofac module (Orchard.Logging.LoggerModule to be precise) handles that. Basically - it scans each dependency and fills all properties of type ILogger with a reference to appropriate logger instance. Each dependency gets its own logger with name equal to full type name (including namespace) of a containing class.
The NullLogger is just a placeholder so accessing the property would not throw NullReferenceExceptions before the property is being set by Autofac.
Extending the default logging is a rather complicated task as it would involve doing three things:
create a custom implementation of ILoggerFactory (just like the default Orchard.Logging.CastleLoggerFactory) and
create an Autofac module that registers that implementation in the container (like the mentioned LoggerModule does)
suppress the current default logging module by decorating your new one with [OrchardSuppressDependency("Orchard.Logging.LoggingModule")]
UPDATE
Just realized I haven't addressed the most important part of the question here:)
Yes, Orchard uses log4net so you may alter the default settings via Config/log4net.config file.
There is a great article on how Orchard Logging works. (I am not sure if it is ok to copy and paste the entire article). This is the link: Injection Logger in Orchard
So the trick is this:
Whenever a class requires a Logger instance, all it needs to do is to
declare a ILogger property, that’s it. And later, in your class, you
can use this property to Logging at anytime
And how is this done?
When Orchard web application startup, the OrchardStarter will be used
to do most of the registration work.
In a few words, it looks all the code in all projects, gets all the classes that use an ILogger property, and implements it for you (if not implemented), using Castle's logger factory.
Can anyone help point me to some good resources for working with Unity 2.0 in an ASP.NET that doesn't talk about ASP.NET MVC?!?
We are not using MVC and I am struggling to get Unity to inject dependencies into my pages following the couple of examples I've read (which are all based on David Hayden's work so they are all presenting the same examples and code).
UPDATE
I tried to go the PageHandlerFactory route but the example (here) is incomplete and no source code is available to accompany the article.
So, I decided to try the custom HttpModule approach described here and here. Again, no source code is available beyond what is shown so it is difficult to troubleshoot issues.
The problem I have now is that all of the plumbing appears to be wiring up correctly but the Buildup method does nothing to my page. I can see all of the types are registered in the container when I set a break-point in the module code and the code is executing as expected. But a break-point in the Page_Load event handler shows that the dependencies are all null.
The property in question is public, with a setter and getter, and is marked with the Dependency attribute. I've tried with and without the attribute, with and without a mapping name, ... every combination I could think of and nothing works.
What am I missing???
It depends what you expect. Most exemples targeting MVC present custom controller factory which allows creating controllers with dependency injection. This is indeed also possible with web forms but instead of controller you must inject dependencies into pages. To do this you must replace PageHandlerFactory with custom implementation.
You can create your own implementation of PageHandlerFactory which will be able to resolve pages directly and inject dependencies defined in constructor or you can use one of these (here, here and here) which instead uses stantandard PageHandlerFactory and builds page instance (resolve property dependencies).