I wrote a small study-project to play a little bit with mongodb. I was using Spring Data to get Mongo Repository. It was quite easy to create RestController and using MongoRepository through Service (another class) retrieve info from mongodb and render it to the browser.
public interface PersonRepository extends MongoRepository<Person, Integer> {
List<Person> findByName(String name);
#Query("{'name':{$regex:?0}}")
List<Person> findByNameLike(String nameLike);
//the rest of methods
}
Now I decided to test my business logic and created the following class:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {AppConfig.class})
public class TestMongo {
private static final Logger LOG = LoggerFactory.getLogger(TestMongo.class);
#Autowired
private PersonRepository personRepository;
#Before
public void setUp() {
Technology technology1 = new Technology("Java-7");
Technology technology2 = new Technology("Java-8");
Technology technology3 = new Technology("Hibernate");
Technology technology4 = new Technology("MyBatis");
Technology technology5 = new Technology("Spring Data");
Project project1 = new Project(1, "POINT", Arrays.asList(technology1, technology3));
Project project2 = new Project(2, "Forecast", Arrays.asList(technology1, technology4));
Project project3 = new Project(3, "CPM", Arrays.asList(technology2, technology5));
Person person1 = new Person(1, "Alex", 27, Arrays.asList(project1, project3));
Person person2 = new Person(2, "Ivan", 26, Arrays.asList(project2, project3));
Person person3 = new Person(3, "Andrii", 31, Arrays.asList(project1));
personRepository.save(Arrays.asList(person1, person2, person3));
}
#Test
public void count() {
List<Person> all = personRepository.findAll();
LOG.info("There are " + all.size() + " person(s) in database");
assertThat(all.size(), equalTo(3));
}
#Test
public void findByName() {
List<Person> personList = personRepository.findByName("Ivan");
LOG.info("*******Find by name********");
LOG.info("personList {}", personList);
LOG.info("***************************");
assertThat(personList, hasSize(1));
}
//another test methods
#After
public void shutDown() {
personRepository.deleteAll();
}
}
where AppConfig.class looks as:
#Configuration
#EnableMongoRepositories
#ComponentScan
public class AppConfig {
#Bean
public MongoClient mongoClient() {
return new MongoClient("localhost", 27017);
}
#Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoClient(),"my-mongo");
}
}
And now my problem: I don't actually want to run some tests on the same database where I store my data. Moreover, I have a crucial method personRepository.deleteAll() after which all the data will just vanish.
I found embedded mongodb as a solution but once I add it to my pom.xml I don't see my installed database anymore.
So, the question is whether it's possible to have both installed and embedded mongodb on the same machine and if not how to test my MongoRepository without modifying prod data.
It looks like I found what I was missing:
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>1.50.5</version>
<scope>test</scope>
</dependency>
Simply adding scope test is launching my embedded mongodb for tests only, while I'm still using installed mongodb for prod.
Related
I have an app using Azure App Services with SQL server and SQL database that are connect to my web app on asp MVC. I've used Distributed Sql Server Cache as a table on my database and so far everything is working well and connected to each other.
Now I want to do two things:
Add entity framework to my app (I already have the database and
connection string)
Run migration – after I've published my app (If I've added for a
example new line or new
table, now I have new version)
I'm not sure how to do those things , I've looked up on many guides and couldn't find an answer. I found a post similar to mine – but using azure functions - here
. I would appreciate it if someone can help me with the steps that I need to follow (like they did in that post) to get entity framework and the migration.
Here is my code:
Program.cs-
using Microsoft.Extensions.Azure;
using Azure.Identity;
var builder = WebApplication.CreateBuilder(args);
if(!builder.Environment.IsDevelopment())
builder.Configuration.AddAzureKeyVault(new Uri(Environment.GetEnvironmentVariable("VaultUri")), new DefaultAzureCredential());
builder.Services.AddControllersWithViews();
builder.Services.AddAzureClients(clientBuilder =>
{
clientBuilder.AddBlobServiceClient(builder.Configuration["storage:blob"], preferMsi: true);
clientBuilder.AddQueueServiceClient(builder.Configuration["storage:queue"], preferMsi: true);
});
builder.Services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = builder.Configuration.GetConnectionString("db");
options.SchemaName = "dbo";
options.TableName = "_Cache";
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
Home Controller:
namespace WebAppAzure.Controllers
{
public class HomeController : Controller
{
private readonly BlobServiceClient storage;
private readonly ILogger<HomeController> logger;
private readonly IDistributedCache cache;
public HomeController(BlobServiceClient storage, ILogger<HomeController> logger,
IDistributedCache cache)
{
this.storage = storage;
this.logger = logger;
this.cache = cache;
}
public IActionResult Index()
{
var containerClient = storage.GetBlobContainerClient("public");
var blob = containerClient.GetBlobClient("image.jpeg");
var model = blob.Uri.ToString();
return View(model: model);
}
public IActionResult Privacy()
{
var stringModel = DateTime.Now.ToString();
cache.SetString("name", stringModel);
return View(model: $"SET: {stringModel}");
}
public IActionResult About()
{
var stringModel = cache.GetString("name");
return View(model: $"GET: {stringModel}");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Add entity framework to my app (I already have the database and connection string)
Use below code for add Entity framework and upload to azure app service and run migration command to migrate database.
DBcontext file in project.
using Microsoft.EntityFrameworkCore;
using WebApplication_72783922.Entity;
namespace WebApplication_72783922
{
public class DbConnectionEntity : DbContext
{
public DbConnectionEntity()
{
}
//string connectionString = Environment.GetEnvironmentVariable("ConnectionStrings:dbcon").ToString();
public DbConnectionEntity(DbContextOptions<DbConnectionEntity> options)
: base(options)
{
}
public virtual DbSet<Users> users { get; set; }
public virtual DbSet<department> Departments { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=xxxx;Initial Catalog=database;Persist Security Info=False;User ID=adminserver72783922;Password=xxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
Program.cs File code.
using Microsoft.Extensions.Azure;
using Azure.Identity;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
if (!builder.Environment.IsDevelopment())
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = "Server=xxxx;Initial Catalog=database;Persist Security Info=False;User ID=adminserver72783922;Password=xxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";
options.SchemaName = "dbo";
options.TableName = "_Cache";
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Run migration – after I’ve published my app
Enable Migration using this command on Package Manager Console enable-migrations
Then add-migration InitialCreate
Then create migrationadd-migration test-v1
update database update-database -verbose
I'm trying to set up a simple DAL that will return a List of typed objects. Pretty standard data repository stuff. I downloaded all of ABP's code from GitHub, built the DLLs for Abp.Dapper and Abp.EntityFrameworkCore and started following the instructions on this page:
https://aspnetboilerplate.com/Pages/Documents/Dapper-Integration
But I can't even get past step one of this. This code doesn't compile because it doesn't know what SampleApplicationModule is. But there's no guidance in these instructions as to what that is supposed to be.
How am I supposed to use Abp's libraries? I'm lost. Can someone please let me know the minimum number of things I need to do in order to wire up my database to Abp's library and query for a List of typed objects?
Code from Abp's Dapper Integration documentation:
[DependsOn(
typeof(AbpEntityFrameworkCoreModule),
typeof(AbpDapperModule)
)]
public class MyModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(typeof(SampleApplicationModule).GetAssembly());
}
}
if you are confused what to write for SampleApplicationModule use the below code
Module Registration
[DependsOn(
typeof(AbpEntityFrameworkModule),
typeof(AbpKernelModule),
typeof(AbpDapperModule)
)]
public class SampleApplicationModule : AbpModule
{
public override void Initialize()
{
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
}
Usage
public class SomeDomainService : ITransientDependency
{
private readonly IDapperRepository<Animal> _animalDapperRepository;
private readonly IRepository<Animal> _animalRepository;
private readonly IDapperRepository<Person> _personDapperRepository;
private readonly IRepository<Person> _personRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public SomeDomainService(
IUnitOfWorkManager unitOfWorkManager,
IRepository<Person> personRepository,
IRepository<Animal> animalRepository,
IDapperRepository<Person> personDapperRepository,
IDapperRepository<Animal> animalDapperRepository)
{
_unitOfWorkManager = unitOfWorkManager;
_personRepository = personRepository;
_animalRepository = animalRepository;
_personDapperRepository = personDapperRepository;
_animalDapperRepository = animalDapperRepository;
}
public void DoSomeStuff()
{
using (IUnitOfWorkCompleteHandle uow = _unitOfWorkManager.Begin())
{
_personRepository.Insert(new Person("Oğuzhan"));
_personRepository.Insert(new Person("Bread"));
_animalRepository.Insert(new Animal("Bird"));
_animalRepository.Insert(new Animal("Cat"));
_unitOfWorkManager.Current.SaveChanges();
Animal animal = _animalRepository.FirstOrDefault(x => x.Name == "Bird");
Person person = _personDapperRepository.Get(1);
int personCount = _personDapperRepository.Count(x => x.Name == "Oğuzhan");
List<Animal> persons = _animalDapperRepository.GetList(x => x.Name.StartsWith("O")).ToList();
uow.Complete();
}
}
}
See the related post for AbpDapper
https://github.com/aspnetboilerplate/aspnetboilerplate/pull/1854#issuecomment-284511423
PS: Abp.Dapper integration is implemented by the community.
Good I have the need to make certain queries to the database sql server working with dates, I am presented with the inconvenience that as language in the installation of sql server is in English therefore the results are defined in that language for example to perform the consult with the following function DATENAME (WEEKDAY, date) the result would be for example Tuesday, as far as my need is to show it in Spanish.
A solution that I have applied is to do this from the application, obtaining the date and perform the conversion from the application.
But I would like to know if it is possible to make this configuration globally from the application through the configuration parameters of the DataSource?
Configuration:
#Configuration
#EnableJpaRepositories(basePackages = { "com.company.app.repository" })
#ComponentScan(basePackages = { "com.company.app.repository" })
#EnableTransactionManagement
#EnableJpaAuditing
#PropertySource("classpath:/db.properties")
public class ApplicationConfig {
#Autowired
private Environment environment;
//private static final Logger LOGGER = LogManager.getLogger(ApplicationConfig.class);
#Bean
public DataSource dataSource() {
// OracleDataSource oracleDS = null;
SQLServerDataSource dataSource = null;
// oracleDS = new OracleDataSource();
dataSource = new SQLServerDataSource();
dataSource.setURL(environment.getProperty("cendb.url"));//
dataSource.setUser(environment.getProperty("cendb.user"));//
dataSource.setPassword(environment.getProperty("cendb.password"));
return dataSource;
}
#Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(false);
vendorAdapter.setShowSql(true);
vendorAdapter.setDatabasePlatform("org.hibernate.dialect.SQLServerDialect");
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.company.app.model");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
#Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
#Bean
public AuditorAware<String> createAuditorProvider() {
return new CustomAuditorAware();
}
#Bean
public AuditingEntityListener createAuditingListener() {
return new AuditingEntityListener();
}
Here I have a simple WinForm app which has a NancyFx service all working fine: I use a Person object which implements the IPerson interface. The nancyModule has a ctor with a parameter of IPerson and in the post route of the nancyModule I use the this.Bind(); If I want to display the person on the form how do I do it?
using System;
using System.Windows.Forms;
using Microsoft.Owin.Hosting;
using Nancy;
using Nancy.ModelBinding;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private IDisposable dispose;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string uri = "http://localhost:8080/";
dispose = WebApp.Start<Startup1>(uri);
}
}
public interface IPerson
{
String Name { get; set; }
}
public class Person : IPerson
{
public String Name { get; set; }
}
public class nancyModule : NancyModule
{
public nancyModule(IPerson person)
{
Post["/data"] = _ =>
{
person = this.Bind<Person>();
//HOW DO I DISPLAY THE person ON THE FORM UI
return HttpStatusCode.OK;
};
}
}
}
If you want to display the person data on the form then you need to call your REST API from your Win Forms application. Grab the response and output the results. Simply put, this is how you can achieve this.
I haven't used async and await keywords which ideally you would but
for brevity I have omitted this.
Firstly, I removed the dependency of IPerson from your module as this isn't a dependency as such but an output from your POST. With that minor adjustment, it looks like this:
If you still feel strongly about IPerson being a dependency then simply leave it and the code will still work as expected.
public class PersonModule : NancyModule
{
public PersonModule()
{
this.Post["/data"] = args => this.AddPerson();
}
private Negotiator AddPerson()
{
var person = this.Bind<Person>();
return this.Negotiate
.WithStatusCode(HttpStatusCode.Created)
.WithContentType("application/json")
.WithModel(person);
}
}
Now from your Win Forms application simply call the API via the HttpClient, like this:
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var person = new Person { Name = "Foo Bar" };
var serializer = new JavaScriptSerializer();
var response = client.PostAsync(
"http://localhost:8080/data",
new StringContent(serializer.Serialize(person), Encoding.UTF8, "application/json")).Result;
var result = new JavaScriptSerializer().Deserialize<Person>(response.Content.ReadAsStringAsync().Result);
TextBox1.Text = result.Forename;
}
Purest's out there will mention 3rd party libraries such as Json.NET
and Service Stack which allows for easier serialization and
deserialization but again for simplicity in this example I am using
out of the box features.
I'm using (and new to) RequestFactory in GWT 2.5, with JDO entities with a one-to-many relationship, on AppEngine datastore. I've just started using the GWT RequestFactoryEditorDriver to display/edit my objects.
The Driver traverses my objects fine, and displays them correctly. However, when I try to edit a value on the "related" objects, the change doesn't get persisted to the datastore.
When I change b.name on my UI and click "save", I notice only A's persist() call is called. B's persist() is never called. How do I make the editorDriver fire on both ARequest as well as BRequest request contexts? (since what I want is for B's InstanceRequest<AProxy,Void> persist() to be called when my edits are to B objects only.)
Also, AFAICT, if I have an editor on BProxy, any object b that is being shown by the editor (and following the Editor Contract) should automatically be "context.edit(b)"ed by the Driver to make it mutable. However, in my case "context" is an ARequest, not a BRequest.
Do I have to make a ValueAwareEditor like mentioned here: GWT Editor framework
and create a fresh BRequest inside the flush() call and fire it, so that changes to B separately persist in a BRequest before the ARequest is fired?
editorDriver.getPaths() gives me:
"bs"
Also, the driver definitely sees the change to B's property, as editorDriver.isChanged() returns true before I fire() the context.
There are no errors on my client-side or server-side logs, and the Annotation Processor runs with no warnings.
Here's how I setup my driver:
editorDriver = GWT.create(Driver.class);
editorDriver.initialize(rf, view.getAEditor());
final ARequest aRequest = rf.ARequest();
final Request<List<AProxy>> aRequest = aRequest.findAByUser(loginInfo.getUserId());
String[] paths = editorDriver.getPaths();
aRequest.with(paths).fire(new Receiver<List<AProxy>>() {
#Override
public void onSuccess(List<AProxy> response) {
AProxy a = response.get(0);
ARequest aRequest2 = rf.aRequest();
editorDriver.edit(a, aRequest2);
aRequest2.persist().using(a);
}
});
This is how my entities look:
public abstract class PersistentEntity {
public Void persist() {
PersistenceManager pm = getPersistenceManager();
try {
pm.makePersistent(this);
} finally {
pm.close();
}
return null;
}
public Void remove() {
PersistenceManager pm = getPersistenceManager();
try {
pm.deletePersistent(this);
} finally {
pm.close();
}
return null;
}
}
#PersistenceCapable(identityType = IdentityType.APPLICATION)
#Version(strategy=VersionStrategy.VERSION_NUMBER, column="VERSION",
extensions={#Extension(vendorName="datanucleus", key="field-name", value="version")})
public class A extends PersistentEntity {
... (Id, version omitted for brevity)
#Persistent
private String name;
#Persistent
private List<B> bs;
public String getName() {
return name;
}
...
public void setName(String name) {
this.name = name;
}
public List<B> getBs() {
return bs;
}
public void setBs(List<B> bs) {
this.bs = bs;
}
}
... (same annotations as above omitted for brevity)
public class B extends PersistentEntity {
... (Id, version omitted for brevity)
#Persistent
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here are the proxies:
#ProxyFor(A.class)
public interface AProxy extends EntityProxy {
String getName();
List<BProxy> getBs();
void setName(String name);
void setBs(List<BProxy> bs);
}
#ProxyFor(B.class)
public interface BProxy extends EntityProxy {
String getName();
void setName(String name);
}
Here are my service stubs:
#Service(A.class)
public interface ARequest extends RequestContext {
Request<List<A>> findAByUser(String userId);
InstanceRequest<AProxy, Void> persist();
InstanceRequest<AProxy, Void> remove();
}
#Service(B.class)
public interface BRequest extends RequestContext {
Request<List<A>> findB(String key);
InstanceRequest<BProxy, Void> persist();
InstanceRequest<BProxy, Void> remove();
}
Edit:
I've now changed my ARequest interface and service implementation to support a "saveAndReturn" method, so that I can recursively "persist" "a" on the server side:
Request<UserSandboxProxy> saveAndReturn(AProxy aProxy);
I find now that when I "flush" my RequestFactoryEditorDriver, the client-side context object has my new "b.name" value. However, if I call "context.fire()" and inspect my "saveAndReturn" method on the server side, the resulting server-side object "a", just before I "persist" it, doesn't contain the change to "b.name" on any item of the List.
Why could this be happening? How do I debug why this client-information doesn't go across the wire, to the server?
Options I've considered, tried and ruled out:
1) Ensuring the APT has been run, and there are no warnings/errors on Proxy or Service interfaces
2) Ensuring that my proxies does have a valid setter in AProxy for the List
You have to use a session-per-request pattern for RequestFactory to work properly. More details here: https://code.google.com/p/google-web-toolkit/issues/detail?id=7827