Using ASP.NET MVC model classes in a Windows Forms application - winforms

Is it possible to use ASP.NET MVC model classes in a Windows Forms application? I referenced the .dll file of my ASP.NET MVC project from my Windows Forms application and I was successfully able to call the models. However, when I tried to build the project, errors suddenly popped up saying the namespace of the ASP.NET MVC project could not be found even though the reference was intact.
Here is one of the issue classes in the Windows Forms Application "BeerRecommenderHelper" that references classes in the ASP.NET MVC 3 project "BeerRecommender":
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using BeerRecommender.Models;
namespace BeerRecommenderHelper.Models
{
public class BeerRecommenderHelperContext : BeerRecommender.Models.BeerRecommenderContext
{
public DbSet<Preference> Preferences { get; set; }
public DbSet<Recommendation> Recommendations { get; set; }
public DbSet<Statistic> Statistics { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new BeerRecommenderInitializer());
base.OnModelCreating(modelBuilder);
}
}
}

Yes, if you have your model classes in a separate project, and thes project doesn't contain any references that are specific for an MVC or web project you're free to use the models in your winform project. If both projects use the same 'backend' logic than i would suggest this a good approach!
What are the errors you get when building the project?

Related

WCF Options when using Entity Framework on backend and WPF on front end

I have been assigned the task of removing the WCF aspects of the solution and tp plug the GUI directly into Entity Framework objects. Currently the WCF service lives between the GUI (WPF) and any entity framework aspects.
I apologize for not being more knowledgeable about what I am asking about but I am brand new to these technologies. I am hoping that something "jumps right off the page" for some to give me answers about.
I am actively in the process of learning about these things and I have the application in a .sln file so I can dissect it in Visual Studio. Thanks in advance for any help.
The GUI needs to have controls bound to the data without using WCF, thanks
According to your question, I assume you have two class libraries for WCF services, entity framework and a WPF application.
What you can do is create a mock class inside the WPF application and Entity framework class library to link both entity framework and WPF application.
The First step, implement methods inside the class of WPF application to call Entity framework class methods.
The Second step, implement methods inside Entity Framework mock class to communicate with entities and tables to retrieve values from the database through ADO Model.
Example :
WPF Application Mock Class
public class DBWrapper
{
public bool AddCandidate(Candidate mCandidate)
{
DBAccess dba=new DBAccess();
dba.AddCandidate(mCandidate);
}
}
Entity Framework Class Library Mock Class
public class DBAccess
{
public bool AddCandidate(Candidate mCandidate)
{
try
{
//Entity framework entities object instantiation
var hrEntities=new HREntities();
hrEntities.Candidate.Add(mCandidate);
hrEntities.SaveChanges();
return true;
}
catch(Exception ex)
{
return false;
}
return false;
}
}
Hope this helps you.

Sharing views and content across Nancy projects

Is there a super duper happy path for sharing views and site content across Nancy projects?
For example, I'd like to run the same site via self hosted / IIS.
The easiest way to do that is to put all of the actual application code into a class library - that is your modules, views, js, css, bootstrapper and whatever supporting code you have. You then setup a view location convention in your Bootstrapper such the view can be found in both a web server and self hosted context. This could be the ResourceViewLocationProvider:
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override NancyInternalConfiguration InternalConfiguration
{
get
{
return NancyInternalConfiguration.WithOverrides(
x => x.ViewLocationProvider = typeof (ResourceViewLocationProvider));
}
}
}
Alongside that you can have a web projects, with e.g. nancy.hosting.owin setup up and a project reference to the class library with the application code. Similarly you can have a console application with just nancy.hosting.self setup and a reference to the class library.
I describe this setup in more detail on my blog and in my Nancy book.

How do I get StructureMap working with an AngularJs / MVC5 and WebApi2 web project

So I have an AngularJs/MVC project with normal Controllers and decided to move more to an SPA app and add WebApi2 to pass data back to my UI instead of using MVC.
In my Global.asax I had the following for my MVC project:
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
My WebApiController has a constructor with an IRepository to talk to the database and get some entities back. When my AngularJS web app makes a call to the controller the break points never hit and I'm getting server 500 errors returned with very little information.
Public class MyController : ApiController
{
public MyController (IThingRepository thingrepository)
{
....
}
}
I started seeing errors like:
"ExceptionType": "System.ArgumentException", "Message": "Type
'MyProject.Web.Controllers.MyController' does not have a default
constructor"
I don't want to add a default constructor. Why am I getting this and how do I fix it?
This is happening because dependency resolution isn't working for the WebApi controller. StructureMap isn't finding the constructor and can't resolve the IThingRepository.
WebApi and MVC work differently and have slightly different dependency resolution mechanics. The Global.asax code "DependencyResolver.SetResolver" works for MVC but not for WebAPi. So how do we get this working?
Install the nuget package StructureMap.MVC5 which has the plumbing to make this work.
Install-Package StructureMap.MVC5
Create a new StructureMapDependencyResolver Class that works for both MVC and WebApi
public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver
{
public StructureMapDependencyResolver(IContainer container) : base(container)
{
}
public IDependencyScope BeginScope()
{
IContainer child = this.Container.GetNestedContainer();
return new StructureMapDependencyResolver(child);
}
}
Update the Global.asax code:
//StructureMap Container
IContainer container = IoC.Initialize();
//Register for MVC
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
//Register for Web API
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
For a full explanation of what is happening check this blog post on ASP.NET MVC 4, Web API and StructureMap

Ria Services : Use Entity Framework Code First Classes From Another Project

I have a solution with this structure :
ProjectName.Domain ==> contains POCO classes (EntityFramework
code first classes) ProjectName.DataAccess ==> contains DbContext
and EntityFramework mapping codes. ProjectName.Task ==> It's my
bushiness layer . ProjectName.Presnetation.MvcClient ==> It's
ASP.NET MVC web client.
ProjectName.Presentation.SilverlightClient ==> It's Silverlight 5
client. ProjectName.WCFRiaClassLibrary ==> It's layer between
business logic and Silverlight client
I've decided to handle logic such as queries and CRUD operations in business logic and use ProjectName.Task in domain service class.
I can't find any sample that use EF code first approach and load entities from another project , can you please help or give me link ? because when I try to create my DomainService class without wizard I can't find generated proxy classes in silverlight client project .
I'm doing something like this :
[EnableClientAccess()]
public class CrudService : DomainService
{
private readonly IEntityTask _entityTask;
public CrudService(IEntityTask entityTask)
{
_entityTask = entityTask;
}
public IQueryable<Entity> GetAll ()
{
return _entityTask.GetAll().AsQueryable();
}
}
Is this possible to use code first classes from another project with WCF Ria Service ?
What is wrong with my approach?
Defintely possible. Take a look at this question to see possible problems with wcf ria + ef
EDIT:
I've just written a small blog post attaching to it a functional project. You can find it here

DomainContext class is not created in a RIA services class library solution

I have created a RIA services class library project and things are not going as I expected. The problem is that when I add domain service classes to the server project, the corresponding domain context classes are not generated on the client project.
I start by creating a new project of type WCF RIA services class library. The generated solution has two projects: RIAServicesLibrary1 (the Silverlight class library project) and RIAServicesLibrary1.Web (the class library that will hold the services).
Then I add a new project item to RIAServicesLibrary1.Web of type DomainServiceClass. I add a sample method so that the resulting class code is:
namespace RIAServicesLibrary1.Web
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
// TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class DomainService1 : DomainService
{
[Invoke]
void DoSomething()
{
}
}
}
Then I generate the whole solution and... nothing happens on the client project. The Generated_Code folder is empty, and the domain context object for the service is not here.
Funny enough, if I add a new item of type Authentication domain service, it works as expected: the file RIAServicesLibrary1.Web.g.cs is created on the client project, containing the AuthenticationDomainService1 class as expected.
So what's happening here? Am I doing something wrong?
Note: I am using Visual Studio 2010 Ultimate RTM and WCF RIA services 1.0.
Change it to:
[Invoke]
public void DoSomething()
{
}
Not having 'public' means there was nothing to generate.

Resources