Orika must have public setter - orika

I'm trying Orika and I'm wondering if you have to have public setter for the mapping to work ?
I tried without setter, it's not working, neither trying to explicitly tell byDefault() :
factory.classMap(Page.class, PageResource.class).byDefault().register();
any feedback on this one ?

In the default Orika configuration, mapped objects have to follow the JavaBean spec with getter/setter. Another possible way is, having public fields.
To change this behavior you can write your own PropertyResolverStrategy and configure it.
DefaultMapperFactory build = new DefaultMapperFactory.Builder()
.propertyResolverStrategy(new MyCustomPropertyResolvingStrategy())
.build();
see the documentation or the orika code for more information on this topic.

Related

What is the purpose of ApplicationBuilder.Properties?

What is the purpose of the ApplicationBuilder.Properties collection? Why not simply use HttpContext.Items?
About the only thing I can come up with is if you want to pass objects from one piece of middleware to another, but again, you could just as easily use HttpContext.Items for that.
I suppose there is an argument for reducing scope.. if an object is only of use for middleware, why pollute HttpContext.Items?
Back in the Owin days, we would use Owin properties, and you could get these properties in your app by using GetOwinContext(), but there doesn't seem to be any equivalent of that in the asp.net middleware. So that doesn't seem like it holds the answer.
I did some searching through the framework and couldn't find anything using it within the framework itself. I might have missed something though.
Any ideas?
What is the purpose of ApplicationBuilder.Properties?
According to source code on Github
/// <summary>
/// Gets a key/value collection that can be used to share data between middleware.
/// </summary>
IDictionary<string, object> Properties { get; }
to speculate about anything else would be primarily opinion-based.

SharpRepository + OptimisticConcurrency

Wondering if there is any way to set the save/update operation of a repository to use optimistic concurrency? Haven't seen any method to do this? Or maybe it's configured this way by default?
I'm assuming you are talking about the RaveDbRepository based on the question.
Unfortunately, right now there isn't a way to do that. Many of the RavenDb specific needs can't currently be addressed through using SharpRepository to interface with it, but we would like to get to that point.
Currently, a document store is used to instantiate the RavenDbRepository which is then used to create the IDocumentSession (by calling documentStore.OpenSession()). Since the flag for using optimistic concurrency is on the session itself there currently isn't a great way to handle this.
Please post an issue on GitHub for us about this and we'll look into making it happen. Off the top of my head we have some options:
Allow public or protected access to IDocumentSession so that you could access the session directly or in an inherited class and set that flag.
Allow the ability to instantiate a RavenDbRepository from a IDocumentSesison instead of a DocumentStore, so that the settings could be made while creating the repository.
Somehow work these into the default conventions, though I'm not sure how.

In Ninject 2.0, how do I have both a general binding and a binding for a specific case?

I have a situation where I want to dependency inject my user object, but also place the current user in the IoC container. I want the following lines to work:
kernel.Get<User>(); // Should return a new User()
kernel.Get<User>("Current"); // Should return the current user
One might think bindings like this would work:
Bind<User>().ToSelf();
Bind<User>().ToMethod(LoadCurrentUser).InRequestScope().Named("Current");
Of course, that gives:
Ninject.ActivationException: Error activating User
More than one matching bindings are available.
Activation path:
1) Request for User
Suggestions:
1) Ensure that you have defined a binding for User only once.
I understand the error since a Named binding does not restrict the application of that binding, so both bindings apply. It seems clear that I need to use the contextual bind with the .When*() methods but I can't come up with any way to do that. I feel like there should be when methods that detect whether a named instance is applied. Something like:
// Not valid Ninject syntax
Bind<User>().ToSelf().WhenUnnamedRequested();
Bind<User>().ToMethod(LoadCurrentUser).WhenNamedRequested().InRequestScope().Named("Current");
I can't find any place on the IRequest interface or it's properties that tells me the name requested. How do I do this?
This question was answerd on the mailing list:
http://groups.google.com/group/ninject/browse_thread/thread/cd95847dc4dcfc9d?hl=en
If you are accessing the user by calling Get on the kernel (which I hope you do not) then give the first binding a name as well and access User always by name. Actually, there is a way to get an instance from the binding without a name. But because I heartily recommend not to do this, I do not show how to to this here. If you still want to do it this way I'll tell you later how this would work.
If you are doing it the better and prefered way and inject the user to the objects that require it as dependency there are two options:
The easier one: Give the first binding a name and add a named attribute to the parameters e.g. ctor([Named("NewUser") IUser newUser, [Named("Current")] IUser
currentUser)
Or the prefered way to keep the implementation classes free of the IoC framework: Specify custom attributes and add them to the parameters e.g. ctor([NewUser] IUser newUser, [CurrentUser]IUser currentUser). Change the Bindings to:
Bind<User>().ToSelf()
.WhenTargetHas<NewUserAttribute>();
Bind<User>().ToMethod(LoadCurrentUser)
.InRequestScope()
.WhenTargetHas<CurrentUserAttribute>();

Ninject Intercept

from what dll can i get the extension of Intercept ?
I've added Ninject.Extensions.Interception from http://github.com/danielmarbach/ninject.extensions.interception
No luck there.
Is there some sample working ?
What I need is to make an interceptor that will path through from WcfClient to WcfServer a different functions with different parameters , that way I wouldn't have to implement already implemented functions behind the WcfServer code.
So the the signature of the function I've already implemented is -
public static T InvokeService<T>(MethodInfo MethodName, Type genericType, Type BlClass, params object[] ParamList)
What it does is activates the Method by BlClass and sends to it the ParamList .
I'd like to make an Interceptor that will dynamically fill the parameters while addressing the WcfServer side .
But first how can I get access to the Intercept extension in my ninjectModule?
Thanks in advance for any help you can provide :)
p.s. Tried out :
using Ninject.Extensions.Interception;
and:
using Ninject.Core;
using Ninject.Core.Interception;
the intercept() Method is within Ninject.Extensions.Interception.Infrastructure.Language.
I had trouble myself to find it.
All of the ninject projects are under http://github.com/ninject
You can find the latest pre-built binaries on our CI server at CodeBetter.com. In order to extend the interception mechanism, you need to reference the Ninject.Extensions.Interception dll and implement the IInterceptor interface or extend the SimpleInterceptor class.
-Ian

Why does NHibernate need non-settable members to be virtual?

NHibernate requires not only settable properties of your domain to be virtual but also get-only properties and methods. Does anyone know what the reason for this is?
I cannot imagine a possible use.
The reason is lazy loading. In order to make lazy loading possible, a proxy class is created. It must intercept every call from "outside" in order to load your entity before actual method/property is executed. If some methods/properties were not virtual it would be impossible to intercept these calls and entity wouldn't be loaded.
I'm not an NHibernate expert, but from reading Oren's blogs, and from what I've learned of NH, the basic pattern of use is to proxy the objects for the ORM. This means, among other things, that the only things you'll be able to map are going to be things that are made virtual, otherwise NH would have to use a different strategy to redefine the implementations under the hood.
Because you may want to access your settable properties from there, and maybe in some fancy indirect or reflection way. So it's to be 100% sure that when your entity is used, it's initialized.
Example:
public string GetSmth
{
get
{
// NHibernate will not know that you access this field.
return _name;
}
}
private string _name;
public virtual string Name { get { return _name; } set { _name = value; } }
Here's Ayende explaing this in relation to Entity Framework:
http://ayende.com/Blog/archive/2009/05/29/why-defer-loading-in-entity-framework-isnrsquot-going-to-work.aspx
AddProduct is a non virtual method
call, so it cannot be intercepted.
Accessing the _products field also
cannot be intercepted.
The only reason I see why one would want method execution without messing with NH proxy (i.e. loading data) is when you have method that do not access your class' data. But in this case, if this method does not use your class' data, what does it belong to that class at all?