StructureMap cannot find GenericRepository - asp.net-mvc-2

In MVC 2 I am working on something and have ran into a snag. I got my repository put instead of putting in the same class the interface is I have it same project as the EDMX file.
Initializing StructureMap is what's killing me at this point. Here's where I'm initizing StructureMap (in Global.asax.cs.)
ObjectFactory.Initialize(x =>
{
x.ForRequestedType<IUnitOfWorkFactory>()
.TheDefaultIsConcreteType<EFUnitOfWorkFactory>()
.CacheBy(InstanceScope.HttpContext);
x.ForRequestedType(typeof(IRepository<>))
.CacheBy(InstanceScope.HttpContext)
.TheDefaultIsConcreteType(typeof(GenericRepository<>));
});
The Namespace for this project is GpdsCreationTaxidermy.Data (which is the same Namespace as my GenericRepository.cs). I would post the code for this file but I dont believe that is the problem here. In my Global.asax I import the proper Namespace
using GodsCreationTaxidermy.Data;
The error I'm getting is:
Error 3 The type or namespace name
'GenericRepository' could not be found
(are you missing a using directive or
an assembly reference?)
Also attached is an image showing this particular projects layout
Can someone help with this issue, or what I'm doing wrong here
EDIT I have even tried adding GodsCreationTaxidermy.Data to the file name and still no luck.

Thanks for sending the files :-)
It looks like the definition of GodsCreationTaxidermy.Data has changed.
This is what I did to fix the problem:
Remove these references from GodsCreationTaxidermy.Data.Repository Class Library:
GodsCreationTaxidermy.Data
GodsCreationTaxidermy.Data.Repository
Remove these references from GodsCreationTaxidermy.Data Class Library:
GodsCreationTaxidermy
GodsCreationTaxidermy.Data
Remove the reference to GodsCreationTaxidermy.Data in the GodsCreationTaxidermy MVC project and re-add the reference, choosing GodsCreationTaxidermy.Data from the Project tab
Hopefully that'll get the GenericRepository working :-)
I did notice that the following line no longer works though:
EFUnitOfWorkFactory.SetObjectContext(() => new GodsCreationTaxidermyEntities());
GodsCreationTaxidermyEntities doesn't seem to exist in GodsCreationTaxidermy.Data any more. Does that cause you an issue?

Try this:
.TheDefaultIsConcreteType(typeof(GodsCreationTaxidermy.Data.GenericRepository<>));
Possibly remove <> after GenericRepository. Has GodsCreationTaxidermy.Data been added to the MVC site as a reference?

Related

Name property in #ManagedBean annotation is not available

I am trying to make my first JSF application with help of youtube tutorial video. (eclipse/JSF2.0 Dynamic web project). In the first class created, I have to add the below line
#ManagedBean (name = "userRegistration")
But, when I did the same, eclipse shows an error on "name" keyword and giving suggestion to replace it with "value".
#ManagedBean (value = "userRegistration")
But it is not working as expected and threw below error.
/FirstPage.xhtml #10,66 value="#{userRegistration.name}": Target Unreachable, identifier 'userRegistration' resolved to null
Can anyone please help me here?
#ManagedBean(name="userRegistration") is correct. You can avoid (name="userRegistration") part if your java class is UserRegistration. Check if you have imported proper class: java.faces.bean.ManagedBean
Piggy backing on previous answer, I just had this problem as well. Make sure you have the correct import. There are 2 imports that have the #ManagedBean annotation.
javax.annotation.ManagedBean & javax.faces.bean.ManagedBean
javax.annotation.ManagedBean does not have the attribute name, or eager for that matter.
I noticed that if I just annotated my bean class with #annotation that it would import the javax.annotation.ManagedBean class and not the faces one that I wanted.

MVC4 Add controller error

When I try to add a controller to my WebUI I get the following error:
'DTO.Games' is not part of the specified 'DAL.GamesEntities' class, and
the 'DAL.GamesEntities' class could not be modified to add a
'DbSet<DTO.Games>' property to it. (For example, the
'Dal.GamesEntities' class might be in a compiled assembly
I searched a before asking here but none of the solutions online helped me. I'm working DBFirst. Any code that could help solving this: ask and you shall receive.
Thanks in advance
CODE
Games.Context.cs (contains GamesEntities) - http://pastebin.com/rL9U2eTp

problems registering types with structuremap for generic repository

My source is at https://github.com/tonyeung/generic-repo
I want to create a generic repository that I can reference in different projects. Most generic repository implementations that I've seen make that difficult. I stumbled across http://blog.damianbrady.com.au/2012/07/24/a-generic-repository-and-unit-of-work-implementation-for-entity-framework/ and found that it would work great for what I have in mind. Each of my projects can have a context defined in it, then I just need to reference a generic repository project or dll and pass in the context and I'm ready to go.
The only thing I'm trying to get my head around is how do I wire structuremap since there are a few nested dependencies that need to get resolved. Unfortunately, the blog doesn't really address how to implement dependency injection with the generic repository. I tried to tinker around with the implementation (see above referenced git hub repo), but I'm doing something wrong while configuring structure map.
The dependencies work out like this:
the repository takes a context in the constructor, and the context in turn gets a connection string. The constructor parameters are all interfaces which match the default convention of IType maps to Type. My assumption is that since I have auto registration, all I need to do is explicitly define the registration for the context to tell structuremap that it should get the connection string from the app.config, I should be good to go.
However, structuremap is not able to work out the registrations like I thought it would:
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily Generic_Repository_Test.IGenericRepository`1
[[ViewModel.Customer, ViewModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]],
Generic Repository Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
The registration code
//This is commented out since it should not be necessary.
//I tried to put this in for grins
//and see if it would resolve the issue but it doesn't.
//For<IGenericRepository<Customer>>().Use<GenericRepository<CustomerContext, Customer>>();
For<ICustomerContext>()
.Use<CustomerContext>()
.Ctor<string>("connectionString")
.EqualToAppSetting("ConnectionString");
//I've tried moving scan to the top but it didn't make a difference
Scan(x =>
{
x.AssembliesFromApplicationBaseDirectory();
x.WithDefaultConventions();
});
The problem did in fact turn out to be a configuration issue.
The issue was that the app.settings connectionstring setting was returning null, I'm not sure why that happens but I hardcoded the connection string just for grins. Will figure that issue out later.
With that resolved, I still had to manually configure the repo and the context, since another error popped up.
With that resolved, it looks like structuremap was trying to resolve itself, so I had to add a namespace exclusion.
The completed code looks like this:
public class DependencyRegistry : Registry
{
public DependencyRegistry()
{
For<ICustomerContext>()
.Use<CustomerContext>()
.Ctor<string>("connectionString")
//.EqualToAppSetting("ConnectionString");
.Is(#"Data Source=(localdb)\Projects;Initial Catalog=EventStorage;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False");
For<IGenericRepository<Customer>>().Use<GenericRepository<ICustomerContext, Customer>>();
Scan(x =>
{
x.AssembliesFromApplicationBaseDirectory();
x.ExcludeNamespace("StructureMap");
x.WithDefaultConventions();
});
}
}

Namespace or type specified in project level imports does not contain a public member

I have an ASP.NET 3.5 web application project in which I'm trying to implement a searchable gridview. I originally started the project as a web site and converted it to a web application. After conversion, my class ended up in the folder Old_App_Code and is called SearchGridView.vb.
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing.Design
<Assembly: TagPrefix("MyApp.WebControls", "SearchGridView")>
Namespace MyApp.WebControls
#Region "TemplateColumn"
Public Class NumberColumn
Implements ITemplate
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
End Sub
End Class
#End Region
<ToolboxData("<{0}:SearchGridView runat=server></{0}:SearchGridView>")> _
<ParseChildren(True, "SearchFilters")> _
Public Class SearchGridView
Inherits GridView
The class file continues, but this is the first part of it.
Unfortunately, I receive the error message
Warning 1 Namespace or type specified in the project-level Imports 'MyApp.WebControls' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. DielWebProj
In web.config, I included a namespace tag for MyApp.WebControls and I included an imports tag in the .aspx page as well.
Can anyone shed light as to why this error is being raised and how I would remedy it?
Thanks,
Sid
I have a broadly similar problem to you. I have a website project using a custom control, inheriting from GriView, in the app_code folder. I was recieving the very same error, but noted that it happened only after I would add a second class or module to app_code, and would disappear if I removed it.
So the workaround I have at the moment is to just leave my custom control as the sole occupant of app_code.
One option might be to make the control part of its own project and add it as a reference to the we site/app?
I'll update this if I can find a decent solution.
EDIT:
Well, in my case it was because the control I was using was written in C#, whereas the rest of the project, and classes I added to app_code, were in VB.
The app_code folder is compiled to a single assembly, so classes of different languages cannot share it, unless you create seperate sub-folders and do some config file jiggerypokery. More details here

XmlSerializer can't find EntityObject even though its referenced

I hope that someone can help me with this problem that I've been having with XmlSerializer.
I've already looked through this thread: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/551cee76-fd80-48f8-ac6b-5c22c234fecf/
The error I am getting is:
System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I've made sure that my unit test has a reference to System.Data.Entity, so it is able to compile at least. I've also in the app.config made an assembly binding to System.Data.Entity.
Here's my rough class structure
[Serializable]
[XmlRoot(Namespace = XmlSupport.MyNamespace, ElementName = XmlSupport.WantToSerialize)]
[XmlInclude(typeof(WantToSerializeBaseClass)]
[XmlInclude(typeof(EntityObject)]
[XmlInclude(typeof(MyEntityObjectSubClass)]
public class WantToSerialize : WantToSerializeBaseClass, IXmlSerializable (I've tried putting this on the baseclass and the current class)
{
// methods and classes
// I've included XmlIncludes for all the classes that this class has a reference too
// even though in the WriteXml it just uses .NET base classes
}
The WantToSerializeBaseClass makes use of some generics, but I've decorated it with XmlIncludes for (EntityObject, and any other classes it makes reference to as well).
the calling code:
var serializerWrite = new XmlSerializer(typeof (WantToSerialize), XmlSupport.ITNNamespace);
fails
However if I do:
var serializerWrite = new XmlSerializer(typeof (WantToSerialize), new Type[] {typeof(EntityObject)});
it is succesfull.
Any thoughts would be most helpful.
UPDATED
I've tracked the problem down to a method in the WantToSerializeBaseClass
public abstract void ConvertFromEntity<TtoCopy>(TtoCopy toCopy) where TtoCopy : MyEntityObjectSubClass;
Where MyEntityObjectSubClass is a subclass of EntityObject, that adds a few methods that I want on my entity objects. The MyEntityObjectSubClass looks like this:
[Serializable]
[XmlInclude(typeof(EntityObject))]
public abstract class MyEntityObjectSubClass : EntityObject, IMyEntityObjectSubClass
Again any thoughts would be great
If you don't have any code that requires a reference at compile time then that reference won't be included in the built assembly. You can use a tool like Reflector to check whether the reference is making it into your assembly.
One thing you can try is adding a static method to WantToSerialize that creates the XmlSerializer. The assembly containing WantToSerialize must already have a good reference to EntityObject, so this should solve the problem.
I have this same problem too (in VB). what I found is that you can use the generic parameter, but it errors because the type MyEntityObjectSubClass is in another assembly. If you remove the type restriction on the generic parameter it will work fine.
I believe this to be an error in the framework itself. I've submitted a feedback ticket to microsoft. I attached a VB.net
I ended up removing the generic code and it worked fine.
I realize this is an older question but for posterity's sake set the CopyLocal parameter on the .dll reference to True.