Nuget Package from a Class Library Required in Main Project? - c#

I have created a Data Access Layer as a Class Library. This class library has a nuget package installed for my ORM. When I try to use my Class Library in my main project it complains that the main project doesn't have my ORM Nuget Package installed.
I thought that if the Class Library had that Nuget Package installed then I wouldn't need that package installed for every project that uses it.
Am I doing something wrong here or is that just the way it works?
Thanks!
UPDATE
In my DAL I have a DataRepositoryBase class that has a method called GetPaged. Under the covers this DataRepositoryBase class uses ServiceStack's ORMLite.
In my Program I have the following code:
using (DataRepositoryBase<Inventory> InvRepo = new DataRepositoryBase<Inventory>())
{
IEnumerable<Inventory> invList = InvRepo.GetPaged(i => i.Cust_ID == CustID, 0, 10);
}
My main program is complaining about not having a reference to ORMLite.

If you are referencing types in the DAL from the Main Project then you have to reference that assembly from there as well.

Related

How to reference a Fody MethodDecorator in another project?

I'm trying to use the Fody MethodDecorator to log method entry/exit in multiple projects. I've created a project in visual studio and implemented the IMethodDecorator interface as described in the ReadMe.md in the Fody MethodDecorator project.
The Fody MethodDecorator works fine in the one project where I've implemented the IMethodDecorator interface and I can log method entries/exits. But when I try to log method entry/exits in other projects by adding a reference to my first project, there is no logging in any other projects.
I've created a github repository with a minimal working example just to show the issue, click here for link.
There are others that have had the same issue, like this post from 3 yeas ago. But the solution in that post is to add the Fody and MethodDecorator nuget packages to all projects. I have already added both nuget packages to all projects. I have also added the FodyWeavers.xml to all projects.
If you want to know what my code looks like, just check out the git repo. Is there anything else I have to do to get logging of method entry/exit working in other projects?
I would like to simply add a reference to my first project and reuse the method decorator from the first project. I'm using .Net 4.8, Fody 6.5.3 and MethodDecorator.Fody 1.1.1.
Edit
If you download the repository and get the error: A numeric comparison was attempted on "$(MsBuildMajorVersion)" that evaluates ... This is apparantly due to a bug in Visual Studio, that can be overcome by a restart.
Not really an answer to the question, but I ended up switching to MethodBoundaryAspect.Fody.
It's a nuget package that does the same thing as MethodDecorator.Fody, and has an interface that you implement in an almost identical manner.
I got MethodBoundaryAspect.Fody to work in multiple projects, by referencing an attribute that I only created in one of the projects. You still have to add the nuget packages MethodBoundaryAspect.Fody and Fody, and also add the FodyWeavers.xml file to each project though.
For Fody.MethodDecorator to work in other projects, you need to add the module
[module: Interceptor] in each project.
My suggestion is to create an Interface as below in each project, and make sure your classes inherit from the interface. ( This way you only added the module at one place in the project)
[module: Interceptor]
public interface IInterceptor
{
// no methods here.
// the interface is used only to register the Interceptor attribute at the module level.
}
public class SomeClass : IInterceptor
{
[Interceptor]
public void SomeMethod()
{
}
}

Creating and maintaining same codebase (class library) with multiple assembly versions as dependency

Given following source code which need to be maintained within a class library project:
using Newtonsoft.Json;
namespace Zephyr.SharedProject.Core
{
public class TestClass
{
public TestClass()
{
string json = JsonConvert.SerializeObject(new
{
PropertyA = 1
});
}
}
}
What are the options do we have if our class library need to support different versions of Newtonsoft.Json?
For example, it was known that the product which consumes the class library have following dependencies:
Product version
Newtonsoft.Json version
1
10.0.1
2
11.0.1
3
12.0.1
Assuming Newtonsoft.Json does not introduce breaking change and same source code can be used with all version above, how would one create and maintain Visual Studio solution to support scenarios above?
I'm thinking having a single project which holds the source code and creating each version-specific project by adding the source code as link1 with corresponding dependencies which looks like following:
Solution
Core Project references Newtonsoft.Json v???
TestClass (source code)
Project_v1 references Newtonsoft.Json v10.0.1
TestClass (added as link)
Project_v2 references Newtonsoft.Json v11.0.1
TestClass (added as link)
Project_v3 references Newtonsoft.Json v12.0.1
TestClass (added as link)
1Right click project, Add > Existing Item > Add As Link
Having project structure above would allow us to maintain a single file and each project can have their own dependency which is fine where we can have unit test for each project as well.
However I'm in dilemma to define the dependency on Core Project as it's ambiguous and would shows compilation error in Visual Studio due to missing reference.
I'm aware binding redirect would solve the problem at consumer-side for version mismatch but I'm interested with the solution from producer (class library) perspective, kindly enlighten me if there's any better approach, cheers!
A shared project can be created in Microsoft Visual Studio which acts a central repository that contains the source codes or files.
The project itself doesn't require any references which can then be added as reference for version specific projects.
When open the source code in editor, one can easily switch between the context of referenced projects to make sure everything's good in case there are any conflict due to different dependencies.
The final project structure would then looks similar to:
Product version
Project type
Newtonsoft.Json version
All
Shared
N/A
1
Class library
10.0.1
2
Class library
11.0.1
3
Class library
12.0.1
P/S: This feature has been around for quite some time and I just recently found out about it, hopefully the information provided helps!
Extra: channel 9 video - Sharing Code Across Platforms With Visual Studio 2015

.NET Core, .NET Standard and Transitive Dependencies across Solutions

My question is similar to this one, although it doesn't really address my issue.
I am working on some new AWS Lambda functions, and I would like to keep their implementation in separate class libraries for reuse. I'm testing this concept using two solutions:
A solution with a single .NET Standard class library project. This class library has a reference to HTML Agility Pack.
A solution with a single .NET Core 2.0 console application project.
Class library:
using System;
using HtmlAgilityPack;
namespace ClassLibrary1
{
public class Class1
{
public static bool FoundDotNet(string html)
{
bool foundDotNet = false;
HtmlDocument document = new HtmlDocument();
document.LoadHtml(html);
var titleNode = document.DocumentNode.SelectSingleNode("//title");
if (titleNode != null)
{
string titleText = titleNode.InnerText;
if (titleText.ToLower().Contains(".net"))
{
foundDotNet = true;
}
}
return foundDotNet;
}
}
}
Console application:
using System;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
var foundDotNet = ClassLibrary1.Class1.FoundDotNet("<html><head><title>.NET WTF Buddy</title></head><body>You're doin' me a confuse.</body></html>");
Console.WriteLine(foundDotNet);
}
}
}
Both projects build without issue. However, the HTML Agility Pack assembly isn't copied into the Debug directory for either of the projects, and when I try to run the console application, I get Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'HtmlAgilityPack'
I have the package management format set to "PackageReference" for both projects, which I thought would handle the transitive dependency correctly. HTML Agility Pack is listed in the json.deps file, so I'm not sure what the problem is.
"HtmlAgilityPack/1.7.1": {
"dependencies": {
"System.Net.Http": "4.3.2",
"System.Xml.XPath": "4.3.0",
"System.Xml.XPath.XmlDocument": "4.3.0",
"System.Xml.XmlDocument": "4.3.0"
}
If I move the the class library project into the same solution as the console application, it works fine. What's preventing me from separating my code into separate solutions?
I'm using a large, complicated library in several solutions and the library has many transitive dependencies.
First, set up your library. Right click on the library's project name and choose Properties. About halfway down you'll see a tab labeled Packages. You can use that to auto-generate the NuGet package every time you rebuild the project. Just increment the version number. I use four position version numbering -- the first three are semver-style (major release, minor release, patch release), and the fourth one I increment manually for each new build.
I recommend creating a folder on your drive or network specifically for your local NuGet packages. You can create folders under that for each project. Then you point your debug and release build output to that project folder, and the NuGet package will be generated there, too.
Finally, back in Visual Studio, go to Tools -> Options -> NuGet Package Manager -> Package Sources and add that top-level folder as a package source.
From there it's simple -- open your NuGet dependencies in your consuming app. There's a drop-down at the top right where you can choose the package source. It will automatically search all the child folders and find whatever packages you've created. Now when you tweak your library, it's just a single click to update the client apps.

Enable Migrations from context on different assembly

So basically I've two projects on the same solution. One of the projects its a class library where I have the all the Models and the Database Context class. The other one is a Web API. I want to use Nuget to Enable-Migrations on the Web API project but I always get the "No context type was found in the assembly Pr.WebApi.
So far I've tried:
Enable-Migrations -ContextTypeName Pr.ClassLibrary.Models
Any Ssuggestions?
When theres no data to store in a database in your WebAPI-Project, you dont need Entity Framework at all in this project. If you store data over your Class library, you can use the context from the class library project.
You will use Enable-Migrations in the Package Manager Console. Make sure that the default project on the top of the package manager console is set to your class library. That is the only project you need to enable the migrations on.
Enable-Migrations should be invoked in your class library...

Xamarin.Forms using SQLite.Net.Async (the NUGET part)

I'm trying to make a connection to a SQLite database using Visual Studio 2015 the target platform is Android.
I've found this SO question but I can not get it to work. My problem is the part of installing packagde.
I want to have my database stuff placed in a Class Library so I've created a new Application, and added a Class Library to the solution.
To install SQLite I've written the following in the packagde manager console:
Install-Package SQLite.Net.Platform.XamarinAndroid -Version 2.3.0
But installs SQLLite into my application, not my Class Library.
My Class Library is very very simple:
public class SQLiteDataService
{
private SQLiteAsyncConnection connectionFlexCheckDemo;
private ISQLitePlatform sqlitePlatform;
private string FlexCheckDBNameDemo = "FlexCheckDemo.db";
private void Test()
{
var platform = new SQLitePlatformAndroid();
}
}
but it doesn't compile:
Error CS0234 The type or namespace name 'Net' does not exist in the
namespace 'SQLite' (are you missing an assembly reference?) ...
What am I doing wrong? And more important how to make it work.
You will have to install the Nuget or the dll to reference the SQLite-Async in your Shared or PCL project so that you can write your common code there like having common CRUD operations or having Unit Of Work coupled with Repository pattern. A simple example can be found here.
Then in your platform specific projects you need to have the nuget installed as well, which will get resolved and install the corresponding version based on your platform. I don't think there is a nuget available for WP, so you might have to look for other choices.
This is the nuget package for SQLite.Net.Async PCL

Categories

Resources