groovy :pass class(domain) as parameter and call static method dynmically - grails

i have a tree of inheritance of Class
class Perfer{
String param
String value
}
class PerferChp extends Prefer{
static belongsTo=[chp:Chp]
}
class PerferSec extends Prefer{
static belongsTo=[sec:Sec]
}
When i try to pass domain class as parameter , i try this:
Prefer find(GormStaticApi<Prefer> preferenceFamily,String param){
preferenceFamily.findByParam(param);
}
But in vain

Related

Access controller level variable from action method attribute

I have the following base class for all controllers-
public abstract class BaseController:Controller
{
public string BaseUrl
{
get { return "something"; }
}
}
I also have the following action filter attribute-
public class CheckQueryStringAttribute : ActionFilterAttribute
{
string baseUrl;
public CheckQueryStringAttribute(string BaseUrl)
{
baseUrl = BaseUrl;
}
}
I would like to use BaseUrl from base controller into attribute as follows-
public class LoginController : BaseController
{
[CheckQueryString(BaseUrl)]
public ActionResult LoginSuccess()
{
return View();
}
}
Is there any way to do it?
You couldn't pass a variable or object reference in attribute constructor parameter because attributes will resolve at compile time so you can only pass constant by their constructor.
But if you exactly explain what you want to do may i can solve your problem in other way.

Don't redeclare extended form type as service to pass parent arguments

I created a symfony form type in which I want to be able to use the entity manager.
So I declared it as a service as mentioned in the symfony documentation:
http://symfony.com/doc/current/form/form_dependencies.html#define-your-form-as-a-service
Now I want to create another form type extending the first one.
But it seem I need to declare this new form type as a service too even if it is extending from the first one already declared as a service.
Is it possible to tell symfony to detect that the new form is extending the first form type and automatically inject dependencies of the parent class?
// src/AppBundle/Form/TaskType.php
use Doctrine\ORM\EntityManager;
class TaskType extends AbstractType
{
private $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// ...
}
// ...
}
// src/AppBundle/Form/TaskObject1Type.php
use Doctrine\ORM\EntityManager;
class TaskObject1Type extends TaskType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
// ...
}
// ...
}
You should not extend form type in Symfony by extending classes.
Instead, use should use getParent method to point into parent type.
So, instead
class ChildType extends ParentType
{
...
}
use
class ChildType extends AbstractType
{
...
public function getParent()
{
return ParentType::class;
}
}
So you will need to deal with parent classes dependencies.

Custom methods for use in spring security expression language annotations

I have classes that extends Controller class
public class MyClass extends MainController {
...
}
And a MainController like this
#Controller
public abstract class MainController extends MultiActionController {
...
}
I would like to create a custom method like 'checkUser' to be used somehow like this:
#PreAuthorize("checkUser('attribute')")
public class MyClass extends MainController {
...
}
And add this method in MainController
#Controller
public abstract class MainController extends MultiActionController {
public boolean checkUser(String key) {
...
return true;
}
}
My question is this. If it is possible, how would I go about configuring it in the spring xml configuration files and come someone give me an example of a custom method used in this way?
Thanks

Child class of DbContext returns no data

Here's my sub-class of my DbContext:
public class JalsoxDbContextCache : JalsoxDbContext, IJalsoxDbContext
{
}
Any ideas why it would return no data where as if I use the base class instead it does?
The name of the class is used to create the connection string.
So I changed the derived class to this:
public class JalsoxDbContextCache : JalsoxDbContext, IJalsoxDbContext
{
public JalsoxDbContextCache()
: base("JalsoxDbContext")
{
}
}
How strange!

How can I have multiple HtmlHelperExtensions in MVC3

I created one file and added a HtmlHelperExtensions class.
public static class HtmlHelperExtensions {
private const string Nbsp = " ";
private const string SelAttribute = " selected='selected'";
public static MvcHtmlString NbspIfEmpty(this HtmlHelper helper, string value)
{
var str = string.IsNullOrEmpty(value) ? Nbsp : value;
return new MvcHtmlString(str);
}
etc...
Now I would like to add more files with more HtmlHelperExtensions. However when I do this I get an error saying:
Duplicate definition: HtmlHelperExtensions
Is it possible for me to have more than one of these classes?
Just name the class something different. You're not allowed duplicate type names under one namespace.
Here's a good tutorial on creating custom Html helpers: http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs.
You could name the class differently as stated by #Andrew Whitaker or you could use the partial keyword.
public static partial class HtmlHelperExtensions
{
// helpers ...
}
public static partial class HtmlHelperExtensions
{
// other helpers ...
}

Resources