Razor void function - asp.net-mvc

I have Razor function which outputs some data and as result does not return anything (that's a long story why it is done this way):
#functions
{
public static void SampleHelperMethod()
{
//...
}
}
How can I call it in view now? I tried #MyFunctions.SampleHelperMethod() but it doesn't work for void functions.

Declaration
#functions
{
public static void TestFunction()
{
}
}
Use in View
#{ TestFunction(); }
Because this is a function that does not return anything, you need to wrap it in the braces like you would and if/for statement. However, like Erik said, it is really unclear why this logic would be declared in the view...you may consider creating a helpers class that your views can include. This will allow for reuse and better separations of concerns.

Related

#UiThread pre/post Execute

With android-annotations I can replace AsyncTask<Param, Progress, Result> with #UiThread.
But is possible to handle something like onPreExcecute / onPostExecute of AsynkTask?
There is no callback methods to handle this. But you can have the same result by calling methods at the beginning and at the end of your #Background annotated method (you said #UIThread but I hope this was a mistake :)).
Example :
#Background
void longRunningProcess() {
longRunningProcessStarted();
//...
longRunningProcessEnded();
}
#UiThread
void longRunningProcessStarted() {
}
#UiThread
void longRunningProcessEnded() {
}
We also wrote a cookbook about this.

Dart, never allow nested Generics?

I would like to use the nested Generics, like
class Class<List<T>> {
...
}
But always Dart Editor gives me alerts. How should I avoid these alerts?
Well, Dart Editor is right. This code doesn't make any sense. Without further information on what you are trying to do (don't hesitate to update your question), I am assuming you actually mean one of those:
class MyClass<T> {
List<T> listField;
// other stuff
}
Or maybe the list itself should be generic?
void main() {
MyClass<SomeCustomListClass<String>> instance = new MyClass();
}
class MyClass<T extends List<String>> {
T listField;
// ...
}
Or maybe everything has to be generic:
void main() {
MyClass<String, SomeCustomListClass<String>> instance = new MyClass();
}
class MyClass<TElement, TList extends List<TElement>> {
TList listField;
TElement _firstListElement;
// whatever that could be used for
}

Access function defined in _ViewStart.cshtml

In an ASP.MVC application, I have a function defined in my _ViewStart.cshtml like this:
#functions
{
public void Foo()
{
....
}
}
When I try to use this function in of my views, I get an exception saying that :
The name 'Foo' does not exist in the current context
I thought that all what I define in _ViewStart.cshtml is accessible to all my views that define it as layout.
I am missing something here ...
Thank you for your help
You could place reusable functions them inside Razor views of the special App_Code folder.
For example inside ~/App_Code/Foo.cshtml you could declare a Bar function:
#functions {
public static void Bar() {
}
}
that will be accessible from any Razor view:
#{Foo.Bar();}
Also notice that the function must be declared as static.

Best practices: Calling GetInstance method to instantiate object

Lets discuss one thing:
I have some simple interface:
public interface ICar
{
void StartEngine();
void StopEngine();
}
public interface IRadio
{
//doesn't matter
}
and some implementation:
public class SportCar : ICar
{
private IRadio radio;
public SportCar(IRadio radioObj)
{
radio = radioObj;
}
//all the rest goes here
}
also we have our StructureMap initialization code, and we calling it on Program initialization:
private void InitializeStructureMap()
{
ObjectFactory.Initialize(x=>
{
x.For<ICar>.Use<SportCar>();
x.For<IRadio>.Use<CarAudioSystem>();
});
}
And my question is: what is the best practice to instantiate SportCar? Is calling:
ObjectFactory.GetInstance<ICar>()
a good practice (now I don't now other way to resolve this)?
ObjectFactory.GetInstance is your starting point, that is what you use to resolve the first object in the hierarcy.
This is how i start my WinForms applications, the same technique should apply to WebForms, Windows Services and Console Applications:
var main = ObjectFactory.GetInstance<Main>();
Application.Run(main);
For ASP.NET MVC the framework allows you to register a factory that creates your controllers, but even in that factory you would call ObjectFactory.GetInstance to instanciate your controller.
As a side note:
When you do initialization, you don't explicitly need to map ICar to SportCar unless you have multiple ICar implementations, you can just do
x.Scan(a => { a.TheCallingAssembly(); a.WithDefaultConventions(); });
which wil map your interfaces with default implementations.

Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory

I am using windsor castle as my IoC container, and has run in to a bit of a problem. This is best explained in code, so I´ll give it a try.
I have a factory class, that should provide me with implementations of a certain interface:
public interface IObjectCreatorFactory
{
IObjectCreator GetObjectCreator(Type objectType);
}
public interface IObjectCreator
{
T CreateObject<T>(IDataRow data);
bool SupportsType(Type type);
}
Implementation of the factory class could look like this, though I am not sure this is the way to go:
public interface ObjectCreatorFactory:IObjectCreatorFactory
{
IEnumerable specificCreators;
IObjectCreator defaultCreator;
public ObjectCreatorFactory(IEnumerable<IObjectCreator> specificCreators, IObjectCreator defaultCreator)
{
this.specificCreators= specificCreators;
this.defaultCreator= defaultCreator;
}
public IObjectCreator GetObjectCreator(Type objectType)
{
foreach (IObjectCreator creator in specificCreators)
{
if (creator.SupportsType(objectType))
{
return creator;
}
}
return defaultCreator;
}
}
Now this would work out ok, but if I want my IObjectCreator instance to create child objects using a specific IObjectCreator, i would like to call ObjectCreatorFactory, and this obviously results in a circular reference:
public void SpecificObjectCreator:IObjectCreator
{
IObjectCreatorFactory objCreatorFactory;
public SpecificObjectCreator(IObjectCreatorFactory objCreatorFactory)
{
this.objCreatorFactory = objCreatorFactory;
}
T CreateObject<T>(IDataRow data)
{
T obj = new T;
ChildObject childObject = objCreatorFactory.GetObjectCreator(typeof(ChildObject)).CreateObject<ChildObject>(data);
.......
}
bool SupportsType(Type type);
}
This does not work out. What would be the way to go for this scenario, where the created objects are refering back to the factory for child object creators?
I would simply move the factory out of the constructors of the various specific object creators, and introduce a method on the IObjectCreator interface instead, responsible for initialising the creators:
public interface IObjectCreator
{
T CreateObject<T>(IDataRow data);
bool SupportsType(Type type);
void Initialize(IObjectCreatorFactory factory);
}
And then just invoke Initialze(this) on each object creator passed into the factory.
In the past I've used custom life cycle stages to take care of automatically invoking "post-construction" setup of components to both avoid circular dependencies and also to take care of other associated concerns (i.e. applying additional component configuration from an external source like a database) - but it's probably overkill for what you need.

Resources